mirror of https://github.com/sveltejs/svelte
fix: depend on reads of deriveds created within reaction (async mode) (#16823)
* fix: depend on reads of deriveds created within reaction (async mode) As part of https://github.com/sveltejs/kit/pull/14481 we discovered that deriveds created within reactions and reading from them in that same reaction is actually useful in some cases, as such a use case we couldn't imagine yet in #15564 has appeared. We think it's ultimately better to rerun on those cases, so we're going to make this change in async mode (that way the behavior doesn't change unless you have enabled the experimental flag) * fix testspull/16825/head
parent
7d9962a572
commit
5e6fed6bab
@ -0,0 +1,5 @@
|
||||
---
|
||||
'svelte': patch
|
||||
---
|
||||
|
||||
fix: depend on reads of deriveds created within reaction (async mode)
|
@ -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'
|
||||
]);
|
||||
}
|
||||
});
|
@ -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>
|
Loading…
Reference in new issue