Skip to content

fix: handle deletions of state proxy properties #13008

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/beige-lamps-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: always return true from `deleteProperty` trap
5 changes: 5 additions & 0 deletions .changeset/green-windows-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: handle deletions of previously-unread state proxy properties
14 changes: 7 additions & 7 deletions packages/svelte/src/internal/client/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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) {
if (s === undefined) {
if (prop in target) {
sources.set(prop, source(UNINITIALIZED));
}
} else {
set(s, UNINITIALIZED);
}

if (exists) {
update_version(version);
}

return exists;
return true;
},

get(target, prop, receiver) {
Expand Down
11 changes: 11 additions & 0 deletions packages/svelte/src/internal/client/proxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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