pull/16197/head
Rich Harris 5 months ago
parent 29147fb70a
commit abba96cac0

@ -2,7 +2,9 @@
import { CLEAN, DIRTY } from '#client/constants'; import { CLEAN, DIRTY } from '#client/constants';
import { import {
flush_queued_effects, flush_queued_effects,
process_effects,
schedule_effect, schedule_effect,
set_queued_root_effects,
set_signal_status, set_signal_status,
update_effect update_effect
} from '../runtime.js'; } from '../runtime.js';
@ -52,9 +54,28 @@ export class Batch {
/** @type {Set<Effect>} */ /** @type {Set<Effect>} */
skipped_effects = new Set(); skipped_effects = new Set();
apply() { apply() {}
/**
*
* @param {Effect[]} root_effects
*/
process(root_effects) {
set_queued_root_effects([]);
var current_values = new Map(); var current_values = new Map();
for (const batch of batches) {
if (batch === this) continue;
for (const [source, previous] of batch.#previous) {
if (!this.#current.has(source)) {
current_values.set(source, source.v);
source.v = previous;
}
}
}
for (const [source, current] of this.#current) { for (const [source, current] of this.#current) {
// TODO this shouldn't be necessary, but tests fail otherwise, // TODO this shouldn't be necessary, but tests fail otherwise,
// presumably because we need a try-finally somewhere, and the // presumably because we need a try-finally somewhere, and the
@ -72,42 +93,33 @@ export class Batch {
schedule_effect(e); schedule_effect(e);
} }
for (const batch of batches) {
if (batch === this) continue;
for (const [source, previous] of batch.#previous) {
if (!this.#previous.has(source)) {
current_values.set(source, source.v);
source.v = previous;
}
}
}
this.render_effects = []; this.render_effects = [];
this.effects = []; this.effects = [];
return () => { for (const root of root_effects) {
if (this.async_effects.length === 0 && this.settled()) { process_effects(this, root);
var render_effects = this.render_effects; }
var effects = this.effects;
this.render_effects = [];
this.effects = [];
// commit changes if (this.async_effects.length === 0 && this.settled()) {
for (const fn of this.#callbacks) { var render_effects = this.render_effects;
fn(); var effects = this.effects;
}
this.#callbacks.clear(); this.render_effects = [];
this.effects = [];
flush_queued_effects(render_effects); // commit changes
flush_queued_effects(effects); for (const fn of this.#callbacks) {
} else { fn();
for (const e of this.render_effects) set_signal_status(e, CLEAN);
for (const e of this.effects) set_signal_status(e, CLEAN);
} }
this.#callbacks.clear();
flush_queued_effects(render_effects);
flush_queued_effects(effects);
} else {
for (const e of this.render_effects) set_signal_status(e, CLEAN);
for (const e of this.effects) set_signal_status(e, CLEAN);
for (const [source, value] of current_values) { for (const [source, value] of current_values) {
source.v = value; source.v = value;
} }
@ -117,7 +129,7 @@ export class Batch {
} }
this.async_effects = []; this.async_effects = [];
}; }
} }
/** /**

@ -77,6 +77,11 @@ export function set_is_destroying_effect(value) {
/** @type {Effect[]} */ /** @type {Effect[]} */
let queued_root_effects = []; let queued_root_effects = [];
/** @param {Effect[]} v */
export function set_queued_root_effects(v) {
queued_root_effects = v;
}
/** @type {Effect[]} Stack of effects, dev only */ /** @type {Effect[]} Stack of effects, dev only */
let dev_effect_stack = []; let dev_effect_stack = [];
// Handle signal reactivity tree dependencies and reactions // Handle signal reactivity tree dependencies and reactions
@ -695,16 +700,7 @@ function flush_queued_root_effects() {
infinite_loop_guard(); infinite_loop_guard();
} }
var revert = batch.apply(); batch.process(queued_root_effects);
var root_effects = queued_root_effects;
queued_root_effects = [];
for (const root of root_effects) {
process_effects(batch, root);
}
revert();
old_values.clear(); old_values.clear();
} }
@ -805,7 +801,7 @@ export function schedule_effect(signal) {
* @param {Batch} batch * @param {Batch} batch
* @param {Effect} root * @param {Effect} root
*/ */
function process_effects(batch, root) { export function process_effects(batch, root) {
root.f ^= CLEAN; root.f ^= CLEAN;
var effect = root.first; var effect = root.first;

Loading…
Cancel
Save