From 0f05cb1cfa7ab95f95cf2ea035df03ced429ae3b Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sat, 25 Jan 2025 15:30:07 -0500 Subject: [PATCH] stale --- packages/svelte/src/internal/client/fork.js | 33 ++++++++++++++++----- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/packages/svelte/src/internal/client/fork.js b/packages/svelte/src/internal/client/fork.js index 99a856c500..0bc547eeff 100644 --- a/packages/svelte/src/internal/client/fork.js +++ b/packages/svelte/src/internal/client/fork.js @@ -40,22 +40,39 @@ export function decrement_fork(fork) { queue_micro_task(() => { if (fork.pending === 0) { - // TODO if the state that was originally set inside the - // fork callback was updated in the meantime, reject - // the fork. Also need to handle a case like - // `{#if a || b}` where the fork makes `a` - // truthy but `b` became truthy in the - // meantime — requires a 'rebase' - fork.callback(); + if (is_valid(fork)) { + // TODO we also need to handle a case like + // `{#if a || b}` where the fork makes `a` + // truthy but `b` became truthy in the + // meantime — requires a 'rebase' + fork.callback(); + } else { + fork.callback(new Error('stale fork')); + } } }); } +/** + * @param {Fork} fork + */ +function is_valid(fork) { + for (const [source, source_fork] of fork.sources) { + if (source.wv > source_fork.wv) { + return false; + } + } + + return true; +} + /** * @param {Fork} fork */ function apply_fork(fork) { - // TODO check the fork is still valid and error otherwise + if (!is_valid(fork)) { + throw new Error('stale fork'); + } for (const [source, saved] of fork.sources) { source.v = saved.next_v;