Skip to content

Commit 66a1846

Browse files
authored
chore: simplify legacy props (#12353)
* chore: simplify legacy props * add test
1 parent ee47696 commit 66a1846

File tree

3 files changed

+34
-28
lines changed

3 files changed

+34
-28
lines changed

packages/svelte/src/legacy/legacy-client.js

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -71,50 +71,37 @@ class Svelte4Component {
7171
*/
7272
constructor(options) {
7373
var sources = new Map();
74-
var add_source = (/** @type {string | symbol} */ key) => {
75-
var s = mutable_source(0);
74+
75+
/**
76+
* @param {string | symbol} key
77+
* @param {unknown} value
78+
*/
79+
var add_source = (key, value) => {
80+
var s = mutable_source(value);
7681
sources.set(key, s);
7782
return s;
7883
};
84+
7985
// Replicate coarse-grained props through a proxy that has a version source for
8086
// each property, which is increment on updates to the property itself. Do not
8187
// use our $state proxy because that one has fine-grained reactivity.
8288
const props = new Proxy(
8389
{ ...(options.props || {}), $$events: {} },
8490
{
85-
get(target, prop, receiver) {
86-
var value = Reflect.get(target, prop, receiver);
87-
var s = sources.get(prop);
88-
if (s === undefined) {
89-
s = add_source(prop);
90-
}
91-
get(s);
92-
return value;
91+
get(target, prop) {
92+
return get(sources.get(prop) ?? add_source(prop, Reflect.get(target, prop)));
9393
},
9494
has(target, prop) {
95-
var value = Reflect.has(target, prop);
96-
var s = sources.get(prop);
97-
if (s !== undefined) {
98-
get(s);
99-
}
100-
return value;
95+
get(sources.get(prop) ?? add_source(prop, Reflect.get(target, prop)));
96+
return Reflect.has(target, prop);
10197
},
10298
set(target, prop, value) {
103-
var s = sources.get(prop);
104-
// @ts-ignore
105-
var prev_value = target[prop];
106-
if (s === undefined) {
107-
s = add_source(prop);
108-
} else if (safe_not_equal(prev_value, value)) {
109-
// Increment version
110-
set(s, s.v + 1);
111-
}
112-
// @ts-ignore
113-
target[prop] = value;
114-
return true;
99+
set(sources.get(prop) ?? add_source(prop, value), value);
100+
return Reflect.set(target, prop, value);
115101
}
116102
}
117103
);
104+
118105
this.#instance = (options.hydrate ? hydrate : mount)(options.component, {
119106
target: options.target,
120107
props,
@@ -142,6 +129,7 @@ class Svelte4Component {
142129
this.#instance.$set = /** @param {Record<string, any>} next */ (next) => {
143130
Object.assign(props, next);
144131
};
132+
145133
this.#instance.$destroy = () => {
146134
unmount(this.#instance);
147135
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
get props() {
5+
return {};
6+
},
7+
8+
html: '',
9+
10+
async test({ assert, component, target }) {
11+
await component.$set({ message: 'goodbye' });
12+
13+
assert.htmlEqual(target.innerHTML, '<p>goodbye</p>');
14+
}
15+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{#if 'message' in $$props}
2+
<p>{$$props.message}</p>
3+
{/if}

0 commit comments

Comments
 (0)