Skip to content

fix: loosen proxy signal creation heuristics #11109

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 5 commits into from
Apr 10, 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/early-months-whisper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: loosen proxy signal creation heuristics
12 changes: 3 additions & 9 deletions packages/svelte/src/internal/client/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
import { add_owner, check_ownership, strip_owner } from './dev/ownership.js';
import { mutable_source, source, set } from './reactivity/sources.js';
import { STATE_SYMBOL } from './constants.js';
import { updating_derived } from './reactivity/deriveds.js';
import { UNINITIALIZED } from '../../constants.js';

/**
Expand Down Expand Up @@ -207,13 +206,8 @@ const state_proxy_handler = {
const metadata = target[STATE_SYMBOL];
let s = metadata.s.get(prop);

// if we're reading a property in a reactive context, create a source,
// but only if it's an own property and not a prototype property
if (
s === undefined &&
(current_effect !== null || updating_derived) &&
(!(prop in target) || get_descriptor(target, prop)?.writable)
) {
// create a source, but only if it's an own property and not a prototype property
if (s === undefined && (!(prop in target) || get_descriptor(target, prop)?.writable)) {
s = (metadata.i ? source : mutable_source)(proxy(target[prop], metadata.i, metadata.o));
metadata.s.set(prop, s);
}
Expand Down Expand Up @@ -281,7 +275,7 @@ const state_proxy_handler = {
// we do so otherwise if we read it later, then the write won't be tracked and
// the heuristics of effects will be different vs if we had read the proxied
// object property before writing to that property.
if (s === undefined && current_effect !== null) {
if (s === undefined) {
// the read creates a signal
untrack(() => receiver[prop]);
s = metadata.s.get(prop);
Expand Down
6 changes: 4 additions & 2 deletions packages/svelte/src/legacy/legacy-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function asClassComponent(component) {

class Svelte4Component {
/** @type {any} */
#events = {};
#events;

/** @type {Record<string, any>} */
#instance;
Expand All @@ -70,7 +70,7 @@ class Svelte4Component {
// Using proxy state here isn't completely mirroring the Svelte 4 behavior, because mutations to a property
// cause fine-grained updates to only the places where that property is used, and not the entire property.
// Reactive statements and actions (the things where this matters) are handling this properly regardless, so it should be fine in practise.
const props = proxy({ ...(options.props || {}), $$events: this.#events }, false);
const props = proxy({ ...(options.props || {}), $$events: {} }, false);
this.#instance = (options.hydrate ? hydrate : mount)(options.component, {
target: options.target,
props,
Expand All @@ -79,6 +79,8 @@ class Svelte4Component {
recover: options.recover
});

this.#events = props.$$events;

for (const key of Object.keys(this.#instance)) {
if (key === '$set' || key === '$destroy' || key === '$on') continue;
define_property(this, key, {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script>
const { settings } = $props();
import { settings } from './main.svelte';
</script>

Child: {settings.showInRgb}
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<script context="module">
export const context = $state({
const context = $state({
settings: {
showInRgb: true
}
})
});

export const settings = context.settings;
</script>

<script>
import Child from './Child.svelte';

const { settings } = context
</script>

<button onclick={() => settings.showInRgb = !settings.showInRgb}>
click {settings.showInRgb}
</button>

<Child settings={settings} />
<Child />