Skip to content

Commit aabd333

Browse files
authored
fix: ensure subscriptions are picked up correctly by deriveds (#16466)
Increment the version to ensure any dependent deriveds are marked dirty when the subscription is picked up again later. If we didn't do this then the comparison of write versions would determine that the derived has a later version than the subscriber, and it would not be re-run. Fixes #16311 Fixes #15888
1 parent ce4a99e commit aabd333

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

.changeset/fresh-penguins-impress.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 subscriptions are picked up correctly by deriveds

packages/svelte/src/reactivity/create-subscriber.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ export function createSubscriber(start) {
8282
if (subscribers === 0) {
8383
stop?.();
8484
stop = undefined;
85+
// Increment the version to ensure any dependent deriveds are marked dirty when the subscription is picked up again later.
86+
// If we didn't do this then the comparison of write versions would determine that the derived has a later version than
87+
// the subscriber, and it would not be re-run.
88+
increment(version);
8589
}
8690
});
8791
};
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { flushSync } from 'svelte';
2+
import { test } from '../../test';
3+
4+
export default test({
5+
test: ({ assert, target }) => {
6+
const [loading, increment] = target.querySelectorAll('button');
7+
8+
assert.htmlEqual(
9+
target.innerHTML,
10+
`
11+
<div>$value: 0</div>
12+
<div>valueFromStore.current: 0</div>
13+
<div>valueDerivedCurrent: 0</div>
14+
<button>Loading</button>
15+
<button>Increment</button>
16+
`
17+
);
18+
19+
loading.click();
20+
flushSync();
21+
assert.htmlEqual(
22+
target.innerHTML,
23+
`
24+
<div>$value: Loading...</div>
25+
<div>valueFromStore.current: Loading...</div>
26+
<div>valueDerivedCurrent: Loading...</div>
27+
<button>Loading</button>
28+
<button>Increment</button>
29+
`
30+
);
31+
32+
increment.click();
33+
flushSync();
34+
assert.htmlEqual(
35+
target.innerHTML,
36+
`
37+
<div>$value: 1</div>
38+
<div>valueFromStore.current: 1</div>
39+
<div>valueDerivedCurrent: 1</div>
40+
<button>Loading</button>
41+
<button>Increment</button>
42+
`
43+
);
44+
}
45+
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<script>
2+
import { fromStore, writable } from 'svelte/store';
3+
4+
let isLoading = $state(false);
5+
6+
const value = writable(0);
7+
const valueFromStore = fromStore(value);
8+
const valueDerivedCurrent = $derived(valueFromStore.current);
9+
</script>
10+
11+
<div>
12+
$value: {isLoading ? 'Loading...' : $value}
13+
</div>
14+
15+
<div>
16+
valueFromStore.current: {isLoading ? 'Loading...' : valueFromStore.current}
17+
</div>
18+
19+
<div>
20+
valueDerivedCurrent: {isLoading ? 'Loading...' : valueDerivedCurrent}
21+
</div>
22+
23+
<button
24+
onclick={() => {
25+
isLoading = true;
26+
}}>
27+
Loading
28+
</button>
29+
30+
<button
31+
onclick={() => {
32+
$value++;
33+
isLoading = false;
34+
}}>
35+
Increment
36+
</button>

0 commit comments

Comments
 (0)