fix: handle deletions of state proxy properties (#13008)

pull/13015/head
Rich Harris 4 weeks ago committed by GitHub
parent 96117fa44d
commit cb4f82f811
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: always return true from `deleteProperty` trap

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: handle deletions of previously-unread state proxy properties

@ -19,7 +19,7 @@ import * as e from './errors.js';
* @param {T} value
* @param {ProxyMetadata | null} [parent]
* @param {Source<T>} [prev] dev mode only
* @returns {ProxyStateObject<T> | T}
* @returns {T}
*/
export function proxy(value, parent = null, prev) {
// if non-proxyable, or is already a proxy, return `value`
@ -91,17 +91,17 @@ export function proxy(value, parent = null, prev) {
deleteProperty(target, prop) {
var s = sources.get(prop);
var exists = s !== undefined ? s.v !== UNINITIALIZED : prop in target;
if (s !== undefined) {
set(s, UNINITIALIZED);
if (s === undefined) {
if (prop in target) {
sources.set(prop, source(UNINITIALIZED));
}
if (exists) {
} else {
set(s, UNINITIALIZED);
update_version(version);
}
return exists;
return true;
},
get(target, prop, receiver) {

@ -85,3 +85,14 @@ test('does not re-proxy proxies', () => {
assert.equal(inner.count, 1);
assert.equal(outer.inner.count, 1);
});
test('deletes a property', () => {
const state = proxy({ a: 1, b: 2 } as { a?: number; b?: number; c?: number });
delete state.a;
assert.equal(JSON.stringify(state), '{"b":2}');
delete state.a;
// deleting a non-existent property should succeed
delete state.c;
});

Loading…
Cancel
Save