Skip to content

Fix/@sveltejs/svelte/16114 #16263

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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/light-rivers-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

Store deriveds old value before updating them for consistency with directly assigned sources when reading in teardown functions
23 changes: 22 additions & 1 deletion packages/svelte/src/internal/client/reactivity/deriveds.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
import { equals, safe_equals } from './equality.js';
import * as e from '../errors.js';
import { destroy_effect } from './effects.js';
import { inspect_effects, set_inspect_effects } from './sources.js';
import { inspect_effects, old_values, set_inspect_effects } from './sources.js';
import { get_stack } from '../dev/tracing.js';
import { tracing_mode_flag } from '../../flags/index.js';
import { component_context } from '../context.js';
Expand Down Expand Up @@ -175,6 +175,27 @@ export function update_derived(derived) {
var value = execute_derived(derived);

if (!derived.equals(value)) {
// store old value before updating
// so that user_effect teardown functions
// can access the previous value.
// this is needed because derived updates happen early during
// effects dependency resolution (before cleanup),
// unlike direct state/derived updates, and this
// can happen also during template_effect execution
// that also happens before user_effect teardown.
//
// store old value only if not inside a teardown function
// because we only need to save the old values before
// the cleanup is triggered othewise accessing
// a derived during cleanup will return the incorrect
// value in case the derived wasn't in the deps of the effect,
// or the teardown was executed because the component was
// destroyed.
if (!is_destroying_effect) {
var old_value = derived.v;
old_values.set(derived, old_value);
}

derived.v = value;
derived.wv = increment_write_version();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script>
import { onDestroy } from 'svelte';

let { checked = $bindable(), count = $bindable() } = $props();

onDestroy(() => {
console.log(`${count} ${checked}`);
});
</script>

<p>Count: {count}</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
async test({ assert, logs, target }) {
/** @type {HTMLButtonElement | null} */
const increment_btn = target.querySelector('#increment');
/** @type {HTMLButtonElement | null} */
const toggle_btn = target.querySelector('#toggle');

for (const p of target.querySelectorAll('p')) {
assert.equal(p.innerHTML, 'Count: 0');
}

// Click increment: count=1
flushSync(() => {
increment_btn?.click();
});

for (const p of target.querySelectorAll('p')) {
assert.equal(p.innerHTML, 'Count: 1');
}

// Click increment again: count=2, components with count < 2 should unmount and log old values
flushSync(() => {
increment_btn?.click();
});

for (const p of target.querySelectorAll('p')) {
assert.equal(p.innerHTML, 'Count: 2');
}

// Toggle show to hide components that depend on show
flushSync(() => {
toggle_btn?.click();
});

// Should log old values during cleanup from the six components guarded by `count < 2`:
// 1. Component with bind: "1 true"
// 2. Component with spread: "1 true"
// 3. Component with normal props: "1 true"
// 4. Runes dynamic component with bind: "1 true"
// 5. Runes dynamic component with spread: "1 true"
// 6. Runes dynamic component with normal props: "1 true"
// Then from the four components guarded by `show`:
// 7. Component with bind (show): "2 true" (old value of checked)
// 8. Runes dynamic component with bind (show): "2 true"
// 9. Runes dynamic component with spread (show): "2 true"
// 10. Runes dynamic component with normal props (show): "2 true"
assert.deepEqual(logs, [
'1 true',
'1 true',
'1 true',
'1 true',
'1 true',
'1 true',
'2 true',
'2 true',
'2 true',
'2 true'
]);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<script>
import Component from './Component.svelte';

let show = $state(true);
let count = $state(0);
let spread = $derived({ checked: show, count });

// Runes dynamic component pattern
let DynComponent1 = $derived(count < 2 ? Component : undefined);
let DynComponent2 = $derived(show ? Component : undefined);
</script>

<button id="increment" onclick={() => count++}>Increment count</button>
<button id="toggle" onclick={() => (show = !show)}>Toggle show</button>

<!-- count with bind -->
{#if count < 2}
<Component bind:count bind:checked={show} />
{/if}

<!-- spread syntax -->
{#if count < 2}
<Component {...spread} />
{/if}

<!-- normal prop -->
{#if count < 2}
<Component {count} checked={show} />
{/if}

<!-- prop only accessed in destroy -->
{#if show}
<Component bind:count bind:checked={show} />
{/if}

<!-- runes dynamic component pattern -->
<DynComponent1 bind:count bind:checked={show} />

<!-- runes dynamic component pattern with spread -->
<DynComponent1 {...spread} />

<!-- runes dynamic component pattern with normal props -->
<DynComponent1 {count} checked={show} />

<!-- runes dynamic component pattern (show) -->
<DynComponent2 bind:count bind:checked={show} />

<!-- runes dynamic component pattern with spread (show) -->
<DynComponent2 {...spread} />

<!-- runes dynamic component pattern with normal props (show) -->
<DynComponent2 {count} checked={show} />
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script>
import { onDestroy } from 'svelte';

let { checked = $bindable(), count = $bindable() } = $props();

onDestroy(() => {
console.log(`${count} ${checked}`);
});
</script>

<p>Count: {count}</p>

<button onclick={() => count--}>Decrement count</button>
<button onclick={() => (checked = !checked)}>Toggle checked</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
async test({ assert, logs, target }) {
/** @type {HTMLButtonElement | null} */
const increment_btn = target.querySelector('#increment');
/** @type {HTMLButtonElement | null} */
const toggle_btn = target.querySelector('#toggle');

for (const p of target.querySelectorAll('p')) {
assert.equal(p.innerHTML, 'Count: 0');
}

// Click increment: count=1
flushSync(() => {
increment_btn?.click();
});

for (const p of target.querySelectorAll('p')) {
assert.equal(p.innerHTML, 'Count: 1');
}

// Click increment again: count=2, components with count < 2 should unmount and log old values
flushSync(() => {
increment_btn?.click();
});

for (const p of target.querySelectorAll('p')) {
assert.equal(p.innerHTML, 'Count: 2');
}

// Toggle show to hide components that depend on show
flushSync(() => {
toggle_btn?.click();
});

// Should log old values during cleanup from the six components guarded by `count < 2`:
// 1. Component with bind: "1 true"
// 2. Component with spread: "1 true"
// 3. Component with normal props: "1 true"
// 4. Runes dynamic component with bind: "1 true"
// 5. Runes dynamic component with spread: "1 true"
// 6. Runes dynamic component with normal props: "1 true"
// Then from the four components guarded by `show`:
// 7. Component with bind (show): "2 true" (old value of checked)
// 8. Runes dynamic component with bind (show): "2 true"
// 9. Runes dynamic component with spread (show): "2 true"
// 10. Runes dynamic component with normal props (show): "2 true"
assert.deepEqual(logs, [
'1 true',
'1 true',
'1 true',
'1 true',
'1 true',
'1 true',
'2 true',
'2 true',
'2 true',
'2 true'
]);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<script>
import Component from './Component.svelte';

let show = $state(true);
let count = $state(0);
let spread = $derived({ checked: show, count });

// Runes dynamic component pattern
let DynComponent1 = $derived(count < 2 ? Component : undefined);
let DynComponent2 = $derived(show ? Component : undefined);
</script>

<button id="increment" onclick={() => count++}>Increment count</button>
<button id="toggle" onclick={() => (show = !show)}>Toggle show</button>

<!-- count with bind -->
{#if count < 2}
<Component bind:count bind:checked={show} />
{/if}

<!-- spread syntax -->
{#if count < 2}
<Component {...spread} />
{/if}

<!-- normal prop -->
{#if count < 2}
<Component {count} checked={show} />
{/if}

<!-- prop only accessed in destroy -->
{#if show}
<Component bind:count bind:checked={show} />
{/if}

<!-- runes dynamic component pattern -->
<DynComponent1 bind:count bind:checked={show} />

<!-- runes dynamic component pattern with spread -->
<DynComponent1 {...spread} />

<!-- runes dynamic component pattern with normal props -->
<DynComponent1 {count} checked={show} />

<!-- runes dynamic component pattern (show) -->
<DynComponent2 bind:count bind:checked={show} />

<!-- runes dynamic component pattern with spread (show) -->
<DynComponent2 {...spread} />

<!-- runes dynamic component pattern with normal props (show) -->
<DynComponent2 {count} checked={show} />
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
async test({ assert, logs, target }) {
/** @type {HTMLButtonElement | null} */
const increment_btn = target.querySelector('#increment');
/** @type {HTMLButtonElement | null} */
const overwrite_btn = target.querySelector('#overwrite');

// Initial state: count=1, derived_value=1

// Click to increment count: count=2, derived_value=4
flushSync(() => {
increment_btn?.click();
});

// Click to increment count: count=3, derived_value=9
flushSync(() => {
increment_btn?.click();
});

// Click to overwrite derived_value: count=3, derived_value=7
flushSync(() => {
overwrite_btn?.click();
});

// Should log old value during cleanup (4) and new value during setup (9)
assert.deepEqual(logs, [
'$effect: 1',
'$effect teardown: 1',
'$effect: 4',
'$effect teardown: 4',
'$effect: 9',
'$effect teardown: 9',
'$effect: 7'
]);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<script>
let count = $state(1);
let derived_value = $derived(count * count);

$effect(() => {
console.log(`$effect: ${derived_value}`);
return () => {
console.log(`$effect teardown: ${derived_value}`);
};
});
</script>

<button id="increment" onclick={() => count++}>Increment s</button>
<button id="overwrite" onclick={() => (derived_value = 7)}>Overwrite derived_value</button>

<p>count: {count}</p>
<p>derived_value: {derived_value}</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script>
import { onDestroy } from 'svelte';

let { checked, count } = $props();

onDestroy(() => {
console.log(`${count} ${checked}`);
});
</script>

<p>Count: {count}</p>
Loading