pull/16197/head
Rich Harris 9 months ago
parent b18247be38
commit 120b086b85

@ -134,11 +134,9 @@ export function async_derived(fn, location) {
} }
if (fork !== null) { if (fork !== null) {
fork?.enable(); fork.run(() => {
flush_sync(() => {
internal_set(signal, v); internal_set(signal, v);
}); });
fork?.disable();
} else { } else {
internal_set(signal, v); internal_set(signal, v);
} }

@ -1,5 +1,8 @@
/** @import { Effect, Source } from '#client' */ /** @import { Effect, Source } from '#client' */
import { flush_sync } from '../runtime.js';
import { internal_set } from './sources.js';
/** @type {Set<Fork>} */ /** @type {Set<Fork>} */
const forks = new Set(); const forks = new Set();
@ -29,14 +32,64 @@ export class Fork {
} }
} }
enable() { /**
active_fork = this; *
// TODO revert other forks * @param {() => void} fn
*/
flush(fn) {
var values = new Map();
for (const fork of forks) {
if (fork === this) continue;
for (const [source, previous] of fork.previous) {
if (this.previous.has(source)) continue;
values.set(source, source.v);
source.v = previous;
// internal_set(source, previous);
}
} }
disable() { try {
fn();
} finally {
for (const [source, value] of values) {
// internal_set(source, value);
source.v = value;
}
}
}
remove() {
forks.delete(this);
for (var fork of forks) {
if (fork.id < this.id) {
// other fork is older than this
for (var source of this.previous.keys()) {
fork.previous.delete(source);
}
} else {
// other fork is newer than this
for (var source of fork.previous.keys()) {
if (this.previous.has(source)) {
fork.previous.set(source, source.v);
}
}
}
}
}
/**
* @param {() => void} fn
*/
run(fn) {
active_fork = this;
flush_sync(fn);
active_fork = null; active_fork = null;
// TODO restore state
} }
increment() { increment() {
@ -52,7 +105,12 @@ export class Fork {
} }
static ensure() { static ensure() {
return (active_fork ??= new Fork()); if (active_fork === null) {
active_fork = new Fork();
forks.add(active_fork); // TODO figure out where we remove this
}
return active_fork;
} }
static unset() { static unset() {

@ -703,10 +703,12 @@ function flush_queued_root_effects(root_effects) {
effect.f ^= CLEAN; effect.f ^= CLEAN;
} }
var fork = /** @type {Fork} */ (active_fork);
var collected_effects = process_effects(effect); var collected_effects = process_effects(effect);
if (/** @type {Fork} */ (active_fork).settled()) { if (fork.settled()) {
flush_queued_effects(collected_effects); flush_queued_effects(collected_effects);
fork.remove();
} }
} }
} finally { } finally {

Loading…
Cancel
Save