Skip to content

Commit cd9f093

Browse files
committed
fix: ensure async batches get noticed of new effects
If you have a value X that causes a pending batch, and while that batch is pending cause creation of another batch which creates a new branch and therefore new effects which also read that value X, those template effects are not correctly rescheduled once the pending batch resolves because the pending batch does not know about them. As a result, the shown values of X get out of sync (once shows the old, once the new value). This partially fixes that by telling the pending batch about the newly created effects. Partially fixes #17099, but not completely because effects that are only indirectly relying on the source that change update, but those that directly rely on the source do not. In this example that means if you do `{x}` in the template it does not work, because it directly invokes the source x, but if I do e.g. `{JSON.stringify(x)}` it does work because the intermediate derived is marked as outdated and therefore reruns, reinvoking the source getter in the process. I assume it's because `MAYBE_DIRTY` for the effect isn't enough, since the version check logic determines that the effect's dependencies aren't newer. I believe we can only fix that by changing the strategy and going back to rerunning `mark_reactions` on changed sources in a batch similar to how it was before #16487 but without running into the infinite loop problem, possibly by keeping track of which of these reactions are async effects that have already run.
1 parent b7625fd commit cd9f093

File tree

5 files changed

+190
-6
lines changed

5 files changed

+190
-6
lines changed

.changeset/many-jobs-speak.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: ensure async batches get noticed of new effects

packages/svelte/src/internal/client/reactivity/batch.js

Lines changed: 104 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,65 @@ export class Batch {
228228
} else if (async_mode_flag && (flags & RENDER_EFFECT) !== 0) {
229229
target.render_effects.push(effect);
230230
} else if (is_dirty(effect)) {
231-
if ((effect.f & BLOCK_EFFECT) !== 0) target.block_effects.push(effect);
232-
update_effect(effect);
231+
if ((effect.f & BLOCK_EFFECT) !== 0) {
232+
target.block_effects.push(effect);
233+
234+
let branches;
235+
let has_multiple_batches = batches.size > 1;
236+
if (has_multiple_batches) {
237+
branches = new Set();
238+
let b = effect.first;
239+
240+
while (b !== null) {
241+
if ((b.f & BRANCH_EFFECT) !== 0) {
242+
branches.add(b);
243+
}
244+
b = b.next;
245+
}
246+
}
247+
248+
update_effect(effect);
249+
250+
let new_branches;
251+
if (has_multiple_batches) {
252+
new_branches = new Set();
253+
let b = effect.first;
254+
255+
while (b !== null) {
256+
const next = b.next;
257+
if (!(/** @type {Set<Effect>} */ (branches).has(b))) {
258+
new_branches.add(b);
259+
}
260+
b = next;
261+
}
262+
263+
const new_target = {
264+
parent: null,
265+
effect,
266+
effects: [],
267+
render_effects: [],
268+
block_effects: []
269+
};
270+
271+
// Traverse any new branches added due to running the block effect and collect their effects...
272+
if (new_branches.size > 0) {
273+
for (const b of new_branches) {
274+
this.#traverse_new_effects(b, new_target);
275+
}
276+
}
277+
278+
// ...then defer those effects in other batches, as they could have changed values that these effects depend on
279+
for (const b of batches) {
280+
if (b !== this) {
281+
b.#defer_effects(new_target.render_effects, false);
282+
b.#defer_effects(new_target.effects, false);
283+
b.#defer_effects(new_target.block_effects, false);
284+
}
285+
}
286+
}
287+
} else {
288+
update_effect(effect);
289+
}
233290
}
234291

235292
var child = effect.first;
@@ -261,16 +318,57 @@ export class Batch {
261318
}
262319
}
263320

321+
/**
322+
* Traverse the newly created effect tree, adding effects to the appropriate lists
323+
* @param {Effect} root
324+
* @param {EffectTarget} target
325+
*/
326+
#traverse_new_effects(root, target) {
327+
var effect = root.first;
328+
329+
while (effect !== null) {
330+
var flags = effect.f;
331+
if (effect.fn !== null) {
332+
if ((flags & EFFECT) !== 0) {
333+
target.effects.push(effect);
334+
} else if ((flags & RENDER_EFFECT) !== 0) {
335+
target.render_effects.push(effect);
336+
} else if ((effect.f & BLOCK_EFFECT) !== 0) {
337+
target.block_effects.push(effect);
338+
}
339+
340+
var child = effect.first;
341+
342+
if (child !== null) {
343+
effect = child;
344+
continue;
345+
}
346+
}
347+
348+
var parent = effect.parent;
349+
effect = effect.next;
350+
351+
while (effect === null && parent !== null && parent !== root) {
352+
effect = parent.next;
353+
parent = parent.parent;
354+
}
355+
}
356+
}
357+
264358
/**
265359
* @param {Effect[]} effects
360+
* @param {boolean} use_status
266361
*/
267-
#defer_effects(effects) {
362+
#defer_effects(effects, use_status = true) {
268363
for (const e of effects) {
269-
const target = (e.f & DIRTY) !== 0 ? this.#dirty_effects : this.#maybe_dirty_effects;
364+
const target =
365+
(e.f & DIRTY) !== 0 && use_status ? this.#dirty_effects : this.#maybe_dirty_effects;
270366
target.push(e);
271367

272-
// mark as clean so they get scheduled if they depend on pending async state
273-
set_signal_status(e, CLEAN);
368+
if (use_status) {
369+
// mark as clean so they get scheduled if they depend on pending async state
370+
set_signal_status(e, CLEAN);
371+
}
274372
}
275373
}
276374

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script>
2+
let { x } = $props();
3+
console.log(x);
4+
$effect(() => console.log('$effect: '+ x))
5+
</script>
6+
7+
{x}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { tick } from 'svelte';
2+
import { test } from '../../test';
3+
4+
export default test({
5+
async test({ assert, target, logs }) {
6+
const [x, y, resolve] = target.querySelectorAll('button');
7+
8+
x.click();
9+
await tick();
10+
assert.deepEqual(logs, ['universe']);
11+
12+
y.click();
13+
await tick();
14+
assert.deepEqual(logs, ['universe', 'world', '$effect: world']);
15+
assert.htmlEqual(
16+
target.innerHTML,
17+
`
18+
<button>x</button>
19+
<button>y++</button>
20+
<button>resolve</button>
21+
world
22+
`
23+
);
24+
25+
resolve.click();
26+
await tick();
27+
assert.deepEqual(logs, [
28+
'universe',
29+
'world',
30+
'$effect: world',
31+
'$effect: universe',
32+
'$effect: universe'
33+
]);
34+
assert.htmlEqual(
35+
target.innerHTML,
36+
`
37+
<button>x</button>
38+
<button>y++</button>
39+
<button>resolve</button>
40+
universe
41+
universe
42+
universe
43+
`
44+
);
45+
}
46+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<script>
2+
import Child from './Child.svelte';
3+
4+
let x = $state('world');
5+
let y = $state(0);
6+
let deferred = [];
7+
8+
function delay(s) {
9+
const d = Promise.withResolvers();
10+
deferred.push(() => d.resolve(s))
11+
return d.promise;
12+
}
13+
</script>
14+
15+
<button onclick={() => x = 'universe'}>x</button>
16+
17+
<button onclick={() => y++}>y++</button>
18+
19+
<button onclick={() => deferred.shift()()}>resolve</button>
20+
21+
{#if x === 'universe'}
22+
{await delay(x)}
23+
<Child {x} />
24+
{/if}
25+
26+
{#if y > 0}
27+
<Child {x} />
28+
{/if}

0 commit comments

Comments
 (0)