Skip to content
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/smart-parents-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: reuse existing proxy when object has multiple references
22 changes: 16 additions & 6 deletions packages/svelte/src/internal/client/proxy/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
object_keys
} from '../utils.js';

/** @typedef {{ s: Map<string | symbol, import('../types.js').SourceSignal<any>>; v: import('../types.js').SourceSignal<number>; a: boolean, i: boolean }} Metadata */
/** @typedef {{ s: Map<string | symbol, import('../types.js').SourceSignal<any>>; v: import('../types.js').SourceSignal<number>; a: boolean, i: boolean, p: StateObject }} Metadata */
/** @typedef {Record<string | symbol, any> & { [STATE_SYMBOL]: Metadata }} StateObject */

export const STATE_SYMBOL = Symbol('$state');
Expand All @@ -35,15 +35,23 @@ const is_frozen = Object.isFrozen;
* @returns {T}
*/
export function proxy(value, immutable = true) {
if (typeof value === 'object' && value != null && !is_frozen(value) && !(STATE_SYMBOL in value)) {
if (typeof value === 'object' && value != null && !is_frozen(value)) {
if (STATE_SYMBOL in value) {
return /** @type {T} */ (value[STATE_SYMBOL].p);
}

const prototype = get_prototype_of(value);

// TODO handle Map and Set as well
if (prototype === object_prototype || prototype === array_prototype) {
define_property(value, STATE_SYMBOL, { value: init(value, immutable), writable: false });
const proxy = new Proxy(value, handler);
define_property(value, STATE_SYMBOL, {
value: init(value, proxy, immutable),
writable: false
});

// @ts-expect-error not sure how to fix this
return new Proxy(value, handler);
return proxy;
}
}

Expand Down Expand Up @@ -102,15 +110,17 @@ export function unstate(value) {

/**
* @param {StateObject} value
* @param {StateObject} proxy
* @param {boolean} immutable
* @returns {Metadata}
*/
function init(value, immutable) {
function init(value, proxy, immutable) {
return {
s: new Map(),
v: source(0),
a: is_array(value),
i: immutable
i: immutable,
p: proxy
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { test } from '../../test';

export default test({
html: `
<button>0</button>
<button>0</button>
`,

async test({ assert, target }) {
const [btn1, btn2] = target.querySelectorAll('button');

await btn1?.click();
assert.htmlEqual(
target.innerHTML,
`
<button>1</button>
<button>1</button>
`
);

await btn2?.click();
assert.htmlEqual(
target.innerHTML,
`
<button>2</button>
<button>2</button>
`
);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script>
let obj = { count: 0 };

let a = $state(obj);
let b = $state(obj);
</script>

<button onclick={() => a.count += 1}>
{a.count}
</button>

<button onclick={() => b.count += 1}>
{b.count}
</button>