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/gold-eels-lay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: depend on reads of deriveds created within reaction (async mode)
4 changes: 2 additions & 2 deletions packages/svelte/src/internal/client/reactivity/deriveds.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import * as w from '../warnings.js';
import { async_effect, destroy_effect } from './effects.js';
import { inspect_effects, internal_set, set_inspect_effects, source } from './sources.js';
import { get_stack } from '../dev/tracing.js';
import { tracing_mode_flag } from '../../flags/index.js';
import { async_mode_flag, tracing_mode_flag } from '../../flags/index.js';
import { Boundary } from '../dom/blocks/boundary.js';
import { component_context } from '../context.js';
import { UNINITIALIZED } from '../../../constants.js';
Expand Down Expand Up @@ -231,7 +231,7 @@ export function async_derived(fn, location) {
export function user_derived(fn) {
const d = derived(fn);

push_reaction_value(d);
if (!async_mode_flag) push_reaction_value(d);

return d;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
// In non-async mode we're not reacting to deriveds read in the same context they're defined in
skip_no_async: true,
test({ assert, target, logs }) {
const [a, b] = target.querySelectorAll('button');

flushSync(() => a?.click());
assert.htmlEqual(
target.innerHTML,
`
<button>a</button>
<button>b</button>
<p>1/0</p
`
);

flushSync(() => a?.click());
assert.htmlEqual(
target.innerHTML,
`
<button>a</button>
<button>b</button>
<p>2/0</p
`
);

flushSync(() => b?.click());
assert.htmlEqual(
target.innerHTML,
`
<button>a</button>
<button>b</button>
<p>2/1</p
`
);

flushSync(() => b?.click());
assert.htmlEqual(
target.innerHTML,
`
<button>a</button>
<button>b</button>
<p>2/2</p
`
);

assert.deepEqual(logs, [
// init
'a',
'b',
'effect a',
'effect b',
// click a
'a',
'effect a',
// click a
'a',
'effect a',
// click b
'a',
'b',
'effect a',
'effect b',
// click b
'a',
'b',
'effect a',
'effect b'
]);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<script>
let object = $state.raw({ a: 0, b: 0 });

function a() {
console.log('a');
return object.a;
}

function b() {
console.log('b');
let double = $derived(object.b)
return double;
}

$effect(() => {
object.a;
console.log('effect a');
})

$effect(() => {
const b = $derived(object.b);
b;
console.log('effect b');
})
</script>

<button onclick={() => object = { ...object, a: object.a + 1 }}>a</button>
<button onclick={() => object = { ...object, b: object.b + 1 }}>b</button>

<p>{a()}/{b()}</p>
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ export default test({
target.innerHTML,
`
<button>increment</button>
<p>1/2</p
<p>1/2</p>
<p>1/2</p>
`
);

assert.deepEqual(logs, [0, 0]);
assert.deepEqual(logs, [0, 0, 0, 0]);
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@
$effect(() => {
foo = new Foo();
});

let bar = $derived(new Foo());
</script>

<button onclick={() => foo.increment()}>increment</button>
<button onclick={() => {foo.increment(); bar.increment()}}>increment</button>

{#if foo}
<p>{foo.value}/{foo.double}</p>
{/if}

<p>{bar.value}/{bar.double}</p>
Loading