discard fork if obsolete

entangle-batches-2
Simon Holthausen 2 months ago
parent 485dda33ad
commit 373bbcaee9
No known key found for this signature in database

@ -1111,6 +1111,16 @@ export class Batch {
for (var fork = first_batch; fork !== null; fork = fork.#next) { for (var fork = first_batch; fork !== null; fork = fork.#next) {
if (!fork.is_fork || fork.fork_effects === null) continue; if (!fork.is_fork || fork.fork_effects === null) continue;
for (const signal of this.current.keys()) {
fork.current.delete(signal);
fork.previous.delete(signal);
}
if (fork.current.size === 0) {
fork.discard();
continue;
}
for (const [effect, version] of fork.fork_effects) { for (const [effect, version] of fork.fork_effects) {
if ( if (
version === (effect_versions.get(effect) ?? 0) || version === (effect_versions.get(effect) ?? 0) ||
@ -1742,19 +1752,23 @@ export function fork(fn) {
batch.apply(); batch.apply();
var committed = false; var committed = false;
var discarded = false;
var settled = batch.settled(); var settled = batch.settled();
flushSync(fn); flushSync(fn);
return { return {
commit: async () => { commit: async () => {
if (committed) { // Differentiate between a fork that was implicitly discarded (because all its updates are obsolete,
await settled; // in which case we don't want to error), and a fork that was explicitly discarded (called fork.discard(),
return; // in which case we do want to error).
if (discarded) {
e.fork_discarded();
} }
if (!batch.linked) { if (committed || !batch.linked) {
e.fork_discarded(); await settled;
return;
} }
committed = true; committed = true;
@ -1798,6 +1812,8 @@ export function fork(fn) {
await settled; await settled;
}, },
discard: () => { discard: () => {
discarded = true;
// cause any MAYBE_DIRTY deriveds to update // cause any MAYBE_DIRTY deriveds to update
// if they depend on things thath changed // if they depend on things thath changed
// inside the discarded fork // inside the discarded fork

Loading…
Cancel
Save