pull/17004/head
Rich Harris 5 days ago
parent 54cd91f055
commit 1aa3db4641

@ -850,17 +850,18 @@ export function clear() {
/**
* @param {() => void} fn
* @returns {Promise<{ commit: () => void, discard: () => void }>}
* @returns {{ commit: () => void, discard: () => void }}
*/
export function fork(fn) {
/** @type {Promise<{ commit: () => void, discard: () => void }>} */
const promise = new Promise((fulfil) => {
// TODO does qmt guarantee this will run outside a batch?
// because it needs to
queue_micro_task(async () => {
if (current_batch !== null) {
throw new Error('cannot fork here'); // TODO better error
}
const batch = Batch.ensure();
batch.is_fork = true;
const promise = batch.fork_settled();
flushSync(fn);
const deferred_inspect_effects = inspect_effects;
@ -869,10 +870,8 @@ export function fork(fn) {
source.v = value;
}
await batch.fork_settled();
fulfil({
commit: () => {
return {
commit: async () => {
if (!batches.has(batch)) {
throw new Error('Cannot commit this batch'); // TODO better error
}
@ -884,6 +883,8 @@ export function fork(fn) {
}
}
await promise;
for (const [source, value] of batch.current) {
source.v = value;
}
@ -906,9 +907,5 @@ export function fork(fn) {
discard: () => {
batches.delete(batch);
}
});
});
});
return promise;
};
}

Loading…
Cancel
Save