mirror of https://github.com/sveltejs/svelte
fix: wrap each block expression in derived to encapsulte effects (#14967)
* fix: wrap each block expression in derived to encapsulte effects * add test * Update .changeset/tender-apples-scream.md --------- Co-authored-by: Rich Harris <rich.harris@vercel.com>pull/14796/merge
parent
dc8ae825b8
commit
dbe5818560
@ -0,0 +1,5 @@
|
||||
---
|
||||
'svelte': patch
|
||||
---
|
||||
|
||||
fix: wrap each block expression in derived to encapsulate effects
|
@ -0,0 +1,16 @@
|
||||
import { flushSync } from 'svelte';
|
||||
import { test } from '../../test';
|
||||
|
||||
export default test({
|
||||
async test({ assert, target, logs }) {
|
||||
const [btn1] = target.querySelectorAll('button');
|
||||
|
||||
btn1.click();
|
||||
flushSync();
|
||||
|
||||
await Promise.resolve();
|
||||
await Promise.resolve();
|
||||
|
||||
assert.deepEqual(logs, ['cleanup']);
|
||||
}
|
||||
});
|
@ -0,0 +1,46 @@
|
||||
<script>
|
||||
import { createSubscriber } from 'svelte/reactivity';
|
||||
|
||||
class MyStore {
|
||||
#subscribe;
|
||||
#data = $state([
|
||||
['a', [1, 2]],
|
||||
['b', [3, 4]]
|
||||
]);
|
||||
#id;
|
||||
|
||||
constructor(options) {
|
||||
options?.someBoolean;
|
||||
this.#id = options?.id;
|
||||
this.#subscribe = createSubscriber(() => {
|
||||
debugger
|
||||
return () => {
|
||||
console.log('cleanup');
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
get data() {
|
||||
this.#subscribe();
|
||||
return this.#data;
|
||||
}
|
||||
set data(v) {
|
||||
this.#data = v;
|
||||
}
|
||||
}
|
||||
|
||||
let storeOptions = $state({
|
||||
someBoolean: false,
|
||||
id: 0
|
||||
});
|
||||
|
||||
let myStore = $derived(new MyStore(storeOptions));
|
||||
</script>
|
||||
|
||||
<button
|
||||
onclick={() => {
|
||||
storeOptions.someBoolean = !storeOptions.someBoolean;
|
||||
}}>+</button
|
||||
>
|
||||
|
||||
{#each myStore.data as _}{/each}
|
Loading…
Reference in new issue