require users to discard forks

pull/17004/head
Rich Harris 1 week ago
parent 4f35c019fc
commit 2664bb4162

@ -149,7 +149,7 @@ This restriction only applies when using the `experimental.async` option, which
### fork_discarded ### fork_discarded
``` ```
Cannot commit a fork that was already committed or discarded (including as a result of a different fork being committed) Cannot commit a fork that was already committed or discarded
``` ```
### fork_timing ### fork_timing

@ -114,7 +114,7 @@ This restriction only applies when using the `experimental.async` option, which
## fork_discarded ## fork_discarded
> Cannot commit a fork that was already committed or discarded (including as a result of a different fork being committed) > Cannot commit a fork that was already committed or discarded
## fork_timing ## fork_timing

@ -262,34 +262,34 @@ export function flush_sync_in_effect() {
} }
/** /**
* Cannot create a fork inside an effect or when state changes are pending * Cannot commit a fork that was already committed or discarded
* @returns {never} * @returns {never}
*/ */
export function fork_timing() { export function fork_discarded() {
if (DEV) { if (DEV) {
const error = new Error(`fork_timing\nCannot create a fork inside an effect or when state changes are pending\nhttps://svelte.dev/e/fork_timing`); const error = new Error(`fork_discarded\nCannot commit a fork that was already committed or discarded\nhttps://svelte.dev/e/fork_discarded`);
error.name = 'Svelte error'; error.name = 'Svelte error';
throw error; throw error;
} else { } else {
throw new Error(`https://svelte.dev/e/fork_timing`); throw new Error(`https://svelte.dev/e/fork_discarded`);
} }
} }
/** /**
* Cannot commit a fork that was already committed or discarded (including as a result of a different fork being committed) * Cannot create a fork inside an effect or when state changes are pending
* @returns {never} * @returns {never}
*/ */
export function fork_discarded() { export function fork_timing() {
if (DEV) { if (DEV) {
const error = new Error(`fork_discarded\nCannot commit a fork that was already committed or discarded (including as a result of a different fork being committed)\nhttps://svelte.dev/e/fork_discarded`); const error = new Error(`fork_timing\nCannot create a fork inside an effect or when state changes are pending\nhttps://svelte.dev/e/fork_timing`);
error.name = 'Svelte error'; error.name = 'Svelte error';
throw error; throw error;
} else { } else {
throw new Error(`https://svelte.dev/e/fork_discarded`); throw new Error(`https://svelte.dev/e/fork_timing`);
} }
} }

@ -880,6 +880,9 @@ export function eager(fn) {
* state changes will be reverted after the fork is initialised, then reapplied * state changes will be reverted after the fork is initialised, then reapplied
* if and when the fork is eventually committed. * if and when the fork is eventually committed.
* *
* When it becomes clear that a fork will _not_ be committed (e.g. because the
* user navigated elsewhere), it must be discarded to avoid leaking memory.
*
* @param {() => void} fn * @param {() => void} fn
* @returns {{ commit: () => void, discard: () => void }} * @returns {{ commit: () => void, discard: () => void }}
*/ */
@ -912,11 +915,6 @@ export function fork(fn) {
batch.is_fork = false; batch.is_fork = false;
// delete all other forks
for (const b of batches) {
if (b.is_fork) batches.delete(b);
}
// apply changes // apply changes
for (const [source, value] of batch.current) { for (const [source, value] of batch.current) {
source.v = value; source.v = value;

@ -460,6 +460,9 @@ declare module 'svelte' {
* state changes will be reverted after the fork is initialised, then reapplied * state changes will be reverted after the fork is initialised, then reapplied
* if and when the fork is eventually committed. * if and when the fork is eventually committed.
* *
* When it becomes clear that a fork will _not_ be committed (e.g. because the
* user navigated elsewhere), it must be discarded to avoid leaking memory.
*
* */ * */
export function fork(fn: () => void): { export function fork(fn: () => void): {
commit: () => void; commit: () => void;

Loading…
Cancel
Save