fix: settle discarded batch (#18290)

Without this the promise would never resolve
pull/18292/head
Simon H 4 months ago committed by GitHub
parent e6110d31c5
commit 871e590c59
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: settle discarded batch

@ -633,6 +633,7 @@ export class Batch {
this.#fork_commit_callbacks.clear();
this.#unlink();
this.#deferred?.resolve();
}
/**

@ -0,0 +1,37 @@
import { tick } from 'svelte';
import { test } from '../../test';
export default test({
async test({ assert, target, logs }) {
await tick();
const [increment, pop] = target.querySelectorAll('button');
increment.click();
await tick();
increment.click();
await tick();
pop.click();
await tick();
assert.deepEqual(logs, ['settled 2', 'settled 2']);
assert.htmlEqual(
target.innerHTML,
`
2
<button>increment</button>
<button>pop</button>
`
);
pop.click();
await tick();
assert.deepEqual(logs, ['settled 2', 'settled 2']);
assert.htmlEqual(
target.innerHTML,
`
2
<button>increment</button>
<button>pop</button>
`
);
}
});

@ -0,0 +1,22 @@
<script>
import { settled } from "svelte";
let count = $state(0);
let queued = [];
function push(v) {
if (!v) return v;
return new Promise((resolve) => {
queued.push(() => resolve(v));
});
}
</script>
{await push(count)}
<button onclick={async () => {
count++;
await settled();
console.log('settled ' + count);
}}>increment</button>
<button onclick={() => queued.pop()?.()}>pop</button>
Loading…
Cancel
Save