Skip to content

chore: simplify legacy props #12353

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 2 commits into from
Jul 8, 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
44 changes: 16 additions & 28 deletions packages/svelte/src/legacy/legacy-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,50 +71,37 @@ class Svelte4Component {
*/
constructor(options) {
var sources = new Map();
var add_source = (/** @type {string | symbol} */ key) => {
var s = mutable_source(0);

/**
* @param {string | symbol} key
* @param {unknown} value
*/
var add_source = (key, value) => {
var s = mutable_source(value);
sources.set(key, s);
return s;
};

// Replicate coarse-grained props through a proxy that has a version source for
// each property, which is increment on updates to the property itself. Do not
// use our $state proxy because that one has fine-grained reactivity.
const props = new Proxy(
{ ...(options.props || {}), $$events: {} },
{
get(target, prop, receiver) {
var value = Reflect.get(target, prop, receiver);
var s = sources.get(prop);
if (s === undefined) {
s = add_source(prop);
}
get(s);
return value;
get(target, prop) {
return get(sources.get(prop) ?? add_source(prop, Reflect.get(target, prop)));
},
has(target, prop) {
var value = Reflect.has(target, prop);
var s = sources.get(prop);
if (s !== undefined) {
get(s);
}
return value;
get(sources.get(prop) ?? add_source(prop, Reflect.get(target, prop)));
return Reflect.has(target, prop);
},
set(target, prop, value) {
var s = sources.get(prop);
// @ts-ignore
var prev_value = target[prop];
if (s === undefined) {
s = add_source(prop);
} else if (safe_not_equal(prev_value, value)) {
// Increment version
set(s, s.v + 1);
}
// @ts-ignore
target[prop] = value;
return true;
set(sources.get(prop) ?? add_source(prop, value), value);
return Reflect.set(target, prop, value);
}
}
);

this.#instance = (options.hydrate ? hydrate : mount)(options.component, {
target: options.target,
props,
Expand Down Expand Up @@ -142,6 +129,7 @@ class Svelte4Component {
this.#instance.$set = /** @param {Record<string, any>} next */ (next) => {
Object.assign(props, next);
};

this.#instance.$destroy = () => {
unmount(this.#instance);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { test } from '../../test';

export default test({
get props() {
return {};
},

html: '',

async test({ assert, component, target }) {
await component.$set({ message: 'goodbye' });

assert.htmlEqual(target.innerHTML, '<p>goodbye</p>');
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{#if 'message' in $$props}
<p>{$$props.message}</p>
{/if}
Loading