don't commit stale batches

pull/15844/head
Rich Harris 2 months ago
parent 39a7f08ead
commit 60eaa2862e

@ -87,6 +87,12 @@ export class Batch {
*/
#deferred = null;
/**
* True if an async effect inside this batch resolved and
* its parent branch was already deleted
*/
#neutered = false;
/**
* Async effects (created inside `async_derived`) encountered during processing.
* These run after the rest of the batch has updated, since they should
@ -278,10 +284,14 @@ export class Batch {
current_batch = null;
}
neuter() {
this.#neutered = true;
}
flush() {
if (queued_root_effects.length > 0) {
this.flush_effects();
} else {
} else if (!this.#neutered) {
this.#commit();
}

@ -9,7 +9,8 @@ import {
EFFECT_PRESERVED,
MAYBE_DIRTY,
STALE_REACTION,
UNOWNED
UNOWNED,
DESTROYED
} from '#client/constants';
import {
active_reaction,
@ -144,6 +145,10 @@ export function async_derived(fn, location) {
const handler = (value, error = undefined) => {
prev = null;
if ((parent.f & DESTROYED) !== 0) {
batch.neuter();
}
current_async_effect = null;
if (!pending) batch.activate();

@ -0,0 +1,52 @@
import { tick } from 'svelte';
import { test } from '../../test';
export default test({
async test({ assert, target }) {
await tick();
const [increment, shift] = target.querySelectorAll('button');
assert.htmlEqual(
target.innerHTML,
`
<button>increment</button>
<button>shift</button>
<p>0</p>
`
);
increment.click();
await tick();
increment.click();
await tick();
increment.click();
await tick();
shift.click();
await tick();
assert.htmlEqual(
target.innerHTML,
`
<button>increment</button>
<button>shift</button>
<p>2</p>
`
);
shift.click();
await tick();
assert.htmlEqual(
target.innerHTML,
`
<button>increment</button>
<button>shift</button>
<p>delayed: 3</p>
`
);
}
});

@ -0,0 +1,26 @@
<script lang="ts">
let count = $state(0);
let deferreds = [];
function push() {
const deferred = Promise.withResolvers();
deferreds.push(deferred);
return deferred.promise;
}
</script>
<button onclick={() => count += 1}>increment</button>
<button onclick={() => deferreds.shift()?.resolve(count)}>shift</button>
<svelte:boundary>
{#if count % 2}
<p>delayed: {await push()}</p>
{:else}
<p>{await count}</p>
{/if}
{#snippet pending()}
<p>loading...</p>
{/snippet}
</svelte:boundary>
Loading…
Cancel
Save