rename forks to batches

pull/16197/head
Rich Harris 7 months ago
parent 5000aae094
commit d465537dd0

@ -1,7 +1,7 @@
/** @import { Effect, TemplateNode, Value } from '#client' */ /** @import { Effect, TemplateNode, Value } from '#client' */
/** @import { Fork } from '../../reactivity/batch.js' */ /** @import { Batch } from '../../reactivity/batch.js' */
import { async_derived } from '../../reactivity/deriveds.js'; import { async_derived } from '../../reactivity/deriveds.js';
import { active_fork } from '../../reactivity/batch.js'; import { current_batch } from '../../reactivity/batch.js';
import { active_effect, schedule_effect } from '../../runtime.js'; import { active_effect, schedule_effect } from '../../runtime.js';
import { capture } from './boundary.js'; import { capture } from './boundary.js';
@ -13,7 +13,7 @@ import { capture } from './boundary.js';
export function async(node, expressions, fn) { export function async(node, expressions, fn) {
// TODO handle hydration // TODO handle hydration
var fork = /** @type {Fork} */ (active_fork); var batch = /** @type {Batch} */ (current_batch);
var effect = /** @type {Effect} */ (active_effect); var effect = /** @type {Effect} */ (active_effect);
var restore = capture(); var restore = capture();
@ -22,7 +22,7 @@ export function async(node, expressions, fn) {
restore(); restore();
fn(node, ...result); fn(node, ...result);
fork.run(() => { batch.run(() => {
schedule_effect(effect); schedule_effect(effect);
}); });
}); });

@ -24,7 +24,7 @@ import { queue_boundary_micro_task } from '../task.js';
import * as e from '../../../shared/errors.js'; import * as e from '../../../shared/errors.js';
import { DEV } from 'esm-env'; import { DEV } from 'esm-env';
import { from_async_derived, set_from_async_derived } from '../../reactivity/deriveds.js'; import { from_async_derived, set_from_async_derived } from '../../reactivity/deriveds.js';
import { Fork } from '../../reactivity/batch.js'; import { Batch } from '../../reactivity/batch.js';
/** /**
* @typedef {{ * @typedef {{
@ -115,7 +115,7 @@ export class Boundary {
// boundary, and hydrate accordingly // boundary, and hydrate accordingly
queueMicrotask(() => { queueMicrotask(() => {
this.#main_effect = this.#run(() => { this.#main_effect = this.#run(() => {
Fork.ensure(); Batch.ensure();
return branch(() => this.#children(this.#anchor)); return branch(() => this.#children(this.#anchor));
}); });

@ -1,5 +1,5 @@
/** @import { EachItem, EachState, Effect, MaybeSource, Source, TemplateNode, TransitionManager, Value } from '#client' */ /** @import { EachItem, EachState, Effect, MaybeSource, Source, TemplateNode, TransitionManager, Value } from '#client' */
/** @import { Fork } from '../../reactivity/batch.js'; */ /** @import { Batch } from '../../reactivity/batch.js'; */
import { import {
EACH_INDEX_REACTIVE, EACH_INDEX_REACTIVE,
EACH_IS_ANIMATED, EACH_IS_ANIMATED,
@ -40,7 +40,7 @@ import { queue_micro_task } from '../task.js';
import { active_effect, get } from '../../runtime.js'; import { active_effect, get } from '../../runtime.js';
import { DEV } from 'esm-env'; import { DEV } from 'esm-env';
import { derived_safe_equal } from '../../reactivity/deriveds.js'; import { derived_safe_equal } from '../../reactivity/deriveds.js';
import { active_fork } from '../../reactivity/batch.js'; import { current_batch } from '../../reactivity/batch.js';
/** /**
* The row of a keyed each block that is currently updating. We track this * The row of a keyed each block that is currently updating. We track this
@ -269,7 +269,7 @@ export function each(node, flags, get_collection, get_key, render_fn, fallback_f
} else { } else {
if (should_defer_append()) { if (should_defer_append()) {
var keys = new Set(); var keys = new Set();
var fork = /** @type {Fork} */ (active_fork); var batch = /** @type {Batch} */ (current_batch);
for (i = 0; i < length; i += 1) { for (i = 0; i < length; i += 1) {
value = array[i]; value = array[i];
@ -305,11 +305,11 @@ export function each(node, flags, get_collection, get_key, render_fn, fallback_f
for (const [key, item] of state.items) { for (const [key, item] of state.items) {
if (!keys.has(key)) { if (!keys.has(key)) {
fork.skipped_effects.add(item.e); batch.skipped_effects.add(item.e);
} }
} }
fork.add_callback(commit); batch.add_callback(commit);
} else { } else {
commit(); commit();
} }

@ -1,5 +1,5 @@
/** @import { Effect, TemplateNode } from '#client' */ /** @import { Effect, TemplateNode } from '#client' */
/** @import { Fork } from '../../reactivity/batch.js'; */ /** @import { Batch } from '../../reactivity/batch.js'; */
import { EFFECT_TRANSPARENT } from '#client/constants'; import { EFFECT_TRANSPARENT } from '#client/constants';
import { import {
hydrate_next, hydrate_next,
@ -12,7 +12,7 @@ import {
import { block, branch, pause_effect, resume_effect } from '../../reactivity/effects.js'; import { block, branch, pause_effect, resume_effect } from '../../reactivity/effects.js';
import { HYDRATION_START_ELSE, UNINITIALIZED } from '../../../../constants.js'; import { HYDRATION_START_ELSE, UNINITIALIZED } from '../../../../constants.js';
import { create_text, should_defer_append } from '../operations.js'; import { create_text, should_defer_append } from '../operations.js';
import { active_fork } from '../../reactivity/batch.js'; import { current_batch } from '../../reactivity/batch.js';
// TODO reinstate https://github.com/sveltejs/svelte/pull/15250 // TODO reinstate https://github.com/sveltejs/svelte/pull/15250
@ -126,15 +126,15 @@ export function if_block(node, fn, elseif = false) {
} }
if (defer) { if (defer) {
var fork = /** @type {Fork} */ (active_fork); var batch = /** @type {Batch} */ (current_batch);
const skipped = condition ? alternate_effect : consequent_effect; const skipped = condition ? alternate_effect : consequent_effect;
if (skipped !== null) { if (skipped !== null) {
// TODO need to do this for other kinds of blocks // TODO need to do this for other kinds of blocks
fork.skipped_effects.add(skipped); batch.skipped_effects.add(skipped);
} }
fork.add_callback(commit); batch.add_callback(commit);
} else { } else {
commit(); commit();
} }

@ -1,12 +1,12 @@
/** @import { Effect, TemplateNode } from '#client' */ /** @import { Effect, TemplateNode } from '#client' */
/** @import { Fork } from '../../reactivity/batch.js'; */ /** @import { Batch } from '../../reactivity/batch.js'; */
import { UNINITIALIZED } from '../../../../constants.js'; import { UNINITIALIZED } from '../../../../constants.js';
import { block, branch, pause_effect } from '../../reactivity/effects.js'; import { block, branch, pause_effect } from '../../reactivity/effects.js';
import { not_equal, safe_not_equal } from '../../reactivity/equality.js'; import { not_equal, safe_not_equal } from '../../reactivity/equality.js';
import { is_runes } from '../../context.js'; import { is_runes } from '../../context.js';
import { hydrate_next, hydrate_node, hydrating } from '../hydration.js'; import { hydrate_next, hydrate_node, hydrating } from '../hydration.js';
import { create_text, should_defer_append } from '../operations.js'; import { create_text, should_defer_append } from '../operations.js';
import { active_fork } from '../../reactivity/batch.js'; import { current_batch } from '../../reactivity/batch.js';
/** /**
* @template V * @template V
@ -66,7 +66,7 @@ export function key_block(node, get_key, render_fn) {
pending_effect = branch(() => render_fn(target)); pending_effect = branch(() => render_fn(target));
if (defer) { if (defer) {
/** @type {Fork} */ (active_fork).add_callback(commit); /** @type {Batch} */ (current_batch).add_callback(commit);
} else { } else {
commit(); commit();
} }

@ -1,8 +1,8 @@
/** @import { TemplateNode, Dom, Effect } from '#client' */ /** @import { TemplateNode, Dom, Effect } from '#client' */
/** @import { Fork } from '../../reactivity/batch.js'; */ /** @import { Batch } from '../../reactivity/batch.js'; */
import { EFFECT_TRANSPARENT } from '#client/constants'; import { EFFECT_TRANSPARENT } from '#client/constants';
import { block, branch, pause_effect } from '../../reactivity/effects.js'; import { block, branch, pause_effect } from '../../reactivity/effects.js';
import { active_fork } from '../../reactivity/batch.js'; import { current_batch } from '../../reactivity/batch.js';
import { hydrate_next, hydrate_node, hydrating } from '../hydration.js'; import { hydrate_next, hydrate_node, hydrating } from '../hydration.js';
import { create_text, should_defer_append } from '../operations.js'; import { create_text, should_defer_append } from '../operations.js';
@ -68,7 +68,7 @@ export function component(node, get_component, render_fn) {
} }
if (defer) { if (defer) {
/** @type {Fork} */ (active_fork).add_callback(commit); /** @type {Batch} */ (current_batch).add_callback(commit);
} else { } else {
commit(); commit();
} }

@ -5,24 +5,24 @@ import { flushSync } from '../runtime.js';
import { raf } from '../timing.js'; import { raf } from '../timing.js';
import { internal_set, mark_reactions, pending } from './sources.js'; import { internal_set, mark_reactions, pending } from './sources.js';
/** @type {Set<Fork>} */ /** @type {Set<Batch>} */
const forks = new Set(); const batches = new Set();
/** @type {Fork | null} */ /** @type {Batch | null} */
export let active_fork = null; export let current_batch = null;
export function remove_active_fork() { export function remove_current_batch() {
active_fork = null; current_batch = null;
} }
/** Update `$effect.pending()` */ /** Update `$effect.pending()` */
function update_pending() { function update_pending() {
// internal_set(pending, forks.size > 0); // internal_set(pending, batches.size > 0);
} }
let uid = 1; let uid = 1;
export class Fork { export class Batch {
id = uid++; id = uid++;
/** @type {Map<Source, any>} */ /** @type {Map<Source, any>} */
@ -40,8 +40,8 @@ export class Fork {
pending = 0; pending = 0;
apply() { apply() {
if (forks.size === 1) { if (batches.size === 1) {
// if this is the latest (and only) fork, we have nothing to do // if this is the latest (and only) batch, we have nothing to do
return noop; return noop;
} }
@ -56,10 +56,10 @@ export class Fork {
source.v = current; source.v = current;
} }
for (const fork of forks) { for (const batch of batches) {
if (fork === this) continue; if (batch === this) continue;
for (const [source, previous] of fork.previous) { for (const [source, previous] of batch.previous) {
if (!current_values.has(source)) { if (!current_values.has(source)) {
// mark_reactions(source, DIRTY); // mark_reactions(source, DIRTY);
current_values.set(source, source.v); current_values.set(source, source.v);
@ -88,19 +88,19 @@ export class Fork {
} }
remove() { remove() {
forks.delete(this); batches.delete(this);
for (var fork of forks) { for (var batch of batches) {
if (fork.id < this.id) { if (batch.id < this.id) {
// other fork is older than this // other batch is older than this
for (var source of this.previous.keys()) { for (var source of this.previous.keys()) {
fork.previous.delete(source); batch.previous.delete(source);
} }
} else { } else {
// other fork is newer than this // other batch is newer than this
for (var source of fork.previous.keys()) { for (var source of batch.previous.keys()) {
if (this.previous.has(source)) { if (this.previous.has(source)) {
fork.previous.set(source, source.v); batch.previous.set(source, source.v);
} }
} }
} }
@ -113,7 +113,7 @@ export class Fork {
* @param {() => void} fn * @param {() => void} fn
*/ */
run(fn) { run(fn) {
active_fork = this; current_batch = this;
fn(); fn();
} }
@ -143,15 +143,15 @@ export class Fork {
} }
static ensure() { static ensure() {
if (active_fork === null) { if (current_batch === null) {
if (forks.size === 0) { if (batches.size === 0) {
raf.tick(update_pending); raf.tick(update_pending);
} }
active_fork = new Fork(); current_batch = new Batch();
forks.add(active_fork); batches.add(current_batch);
} }
return active_fork; return current_batch;
} }
} }

@ -1,5 +1,5 @@
/** @import { Derived, Effect, Source } from '#client' */ /** @import { Derived, Effect, Source } from '#client' */
/** @import { Fork } from './batch.js'; */ /** @import { Batch } from './batch.js'; */
import { DEV } from 'esm-env'; import { DEV } from 'esm-env';
import { import {
CLEAN, CLEAN,
@ -32,7 +32,7 @@ import { tracing_mode_flag } from '../../flags/index.js';
import { capture } from '../dom/blocks/boundary.js'; import { capture } from '../dom/blocks/boundary.js';
import { component_context } from '../context.js'; import { component_context } from '../context.js';
import { UNINITIALIZED } from '../../../constants.js'; import { UNINITIALIZED } from '../../../constants.js';
import { active_fork } from './batch.js'; import { current_batch } from './batch.js';
/** @type {Effect | null} */ /** @type {Effect | null} */
export let from_async_derived = null; export let from_async_derived = null;
@ -124,14 +124,14 @@ export function async_derived(fn, location) {
var restore = capture(); var restore = capture();
var fork = /** @type {Fork} */ (active_fork); var batch = /** @type {Batch} */ (current_batch);
var ran = boundary.ran; var ran = boundary.ran;
if (should_suspend) { if (should_suspend) {
if (!ran) { if (!ran) {
boundary.increment(); boundary.increment();
} else { } else {
fork.increment(); batch.increment();
} }
} }
@ -148,11 +148,11 @@ export function async_derived(fn, location) {
if (!ran) { if (!ran) {
boundary.decrement(); boundary.decrement();
} else { } else {
fork.decrement(); batch.decrement();
} }
} }
fork.run(() => { batch.run(() => {
internal_set(signal, v); internal_set(signal, v);
}); });

@ -41,7 +41,7 @@ import { get_next_sibling } from '../dom/operations.js';
import { async_derived, derived } from './deriveds.js'; import { async_derived, derived } from './deriveds.js';
import { capture } from '../dom/blocks/boundary.js'; import { capture } from '../dom/blocks/boundary.js';
import { component_context, dev_current_component_function } from '../context.js'; import { component_context, dev_current_component_function } from '../context.js';
import { active_fork, Fork } from './batch.js'; import { current_batch, Batch } from './batch.js';
/** /**
* @param {'$effect' | '$effect.pre' | '$inspect'} rune * @param {'$effect' | '$effect.pre' | '$inspect'} rune
@ -234,7 +234,7 @@ export function inspect_effect(fn) {
* @returns {() => void} * @returns {() => void}
*/ */
export function effect_root(fn) { export function effect_root(fn) {
Fork.ensure(); Batch.ensure();
const effect = create_effect(ROOT_EFFECT, fn, true); const effect = create_effect(ROOT_EFFECT, fn, true);
return () => { return () => {
@ -248,7 +248,7 @@ export function effect_root(fn) {
* @returns {(options?: { outro?: boolean }) => Promise<void>} * @returns {(options?: { outro?: boolean }) => Promise<void>}
*/ */
export function component_root(fn) { export function component_root(fn) {
Fork.ensure(); Batch.ensure();
const effect = create_effect(ROOT_EFFECT, fn, true); const effect = create_effect(ROOT_EFFECT, fn, true);
return (options = {}) => { return (options = {}) => {
@ -343,7 +343,7 @@ export function template_effect(fn, sync = [], async = [], d = derived) {
var parent = /** @type {Effect} */ (active_effect); var parent = /** @type {Effect} */ (active_effect);
if (async.length > 0) { if (async.length > 0) {
var fork = /** @type {Fork} */ (active_fork); var batch = /** @type {Batch} */ (current_batch);
var restore = capture(); var restore = capture();
Promise.all(async.map((expression) => async_derived(expression))).then((result) => { Promise.all(async.map((expression) => async_derived(expression))).then((result) => {
@ -355,7 +355,7 @@ export function template_effect(fn, sync = [], async = [], d = derived) {
var effect = create_template_effect(fn, [...sync.map(d), ...result]); var effect = create_template_effect(fn, [...sync.map(d), ...result]);
fork.run(() => { batch.run(() => {
schedule_effect(effect); schedule_effect(effect);
}); });
}); });

@ -34,7 +34,7 @@ import * as e from '../errors.js';
import { legacy_mode_flag, tracing_mode_flag } from '../../flags/index.js'; import { legacy_mode_flag, tracing_mode_flag } from '../../flags/index.js';
import { get_stack } from '../dev/tracing.js'; import { get_stack } from '../dev/tracing.js';
import { component_context, is_runes } from '../context.js'; import { component_context, is_runes } from '../context.js';
import { Fork } from './batch.js'; import { Batch } from './batch.js';
import { proxy } from '../proxy.js'; import { proxy } from '../proxy.js';
import { execute_derived } from './deriveds.js'; import { execute_derived } from './deriveds.js';
@ -169,8 +169,8 @@ export function internal_set(source, value) {
source.v = value; source.v = value;
const fork = Fork.ensure(); const batch = Batch.ensure();
fork.capture(source, old_value); batch.capture(source, old_value);
if (DEV && tracing_mode_flag) { if (DEV && tracing_mode_flag) {
source.updated = get_stack('UpdatedAt'); source.updated = get_stack('UpdatedAt');

@ -50,7 +50,7 @@ import {
import { Boundary } from './dom/blocks/boundary.js'; import { Boundary } from './dom/blocks/boundary.js';
import * as w from './warnings.js'; import * as w from './warnings.js';
import { is_firefox } from './dom/operations.js'; import { is_firefox } from './dom/operations.js';
import { active_fork, Fork, remove_active_fork } from './reactivity/batch.js'; import { current_batch, Batch, remove_current_batch } from './reactivity/batch.js';
// Used for DEV time error handling // Used for DEV time error handling
/** @param {WeakSet<Error>} value */ /** @param {WeakSet<Error>} value */
@ -683,7 +683,7 @@ function infinite_loop_guard() {
function flush_queued_root_effects() { function flush_queued_root_effects() {
var was_updating_effect = is_updating_effect; var was_updating_effect = is_updating_effect;
var fork = /** @type {Fork} */ (active_fork); var batch = /** @type {Batch} */ (current_batch);
try { try {
var flush_count = 0; var flush_count = 0;
@ -694,7 +694,7 @@ function flush_queued_root_effects() {
infinite_loop_guard(); infinite_loop_guard();
} }
var revert = fork.apply(); var revert = batch.apply();
/** @type {Effect[]} */ /** @type {Effect[]} */
var async_effects = []; var async_effects = [];
@ -720,8 +720,8 @@ function flush_queued_root_effects() {
process_effects(root, async_effects, render_effects, effects); process_effects(root, async_effects, render_effects, effects);
} }
if (async_effects.length === 0 && fork.pending === 0) { if (async_effects.length === 0 && batch.pending === 0) {
fork.commit(); batch.commit();
flush_queued_effects(render_effects); flush_queued_effects(render_effects);
flush_queued_effects(effects); flush_queued_effects(effects);
} }
@ -791,18 +791,18 @@ export function schedule_effect(signal) {
if (!is_flushing) { if (!is_flushing) {
is_flushing = true; is_flushing = true;
queueMicrotask(() => { queueMicrotask(() => {
if (active_fork === null) { if (current_batch === null) {
// a flushSync happened in the meantime // a flushSync happened in the meantime
return; return;
} }
flush_queued_root_effects(); flush_queued_root_effects();
if (active_fork?.pending === 0) { if (current_batch?.pending === 0) {
active_fork.remove(); current_batch.remove();
} }
remove_active_fork(); remove_current_batch();
}); });
} }
@ -843,14 +843,14 @@ export function schedule_effect(signal) {
*/ */
function process_effects(root, async_effects, render_effects, effects) { function process_effects(root, async_effects, render_effects, effects) {
var effect = root.first; var effect = root.first;
var fork = /** @type {Fork} */ (active_fork); var batch = /** @type {Batch} */ (current_batch);
while (effect !== null) { while (effect !== null) {
var flags = effect.f; var flags = effect.f;
var is_branch = (flags & BRANCH_EFFECT) !== 0; var is_branch = (flags & BRANCH_EFFECT) !== 0;
var is_skippable_branch = is_branch && (flags & CLEAN) !== 0; var is_skippable_branch = is_branch && (flags & CLEAN) !== 0;
var skip = is_skippable_branch || (flags & INERT) !== 0 || fork.skipped_effects.has(effect); var skip = is_skippable_branch || (flags & INERT) !== 0 || batch.skipped_effects.has(effect);
if (!skip) { if (!skip) {
if ((flags & EFFECT_ASYNC) !== 0) { if ((flags & EFFECT_ASYNC) !== 0) {
@ -867,7 +867,7 @@ function process_effects(root, async_effects, render_effects, effects) {
} }
} else if ((flags & RENDER_EFFECT) !== 0) { } else if ((flags & RENDER_EFFECT) !== 0) {
if (is_branch) { if (is_branch) {
// TODO clean branch later, if fork is settled // TODO clean branch later, if batch is settled
// current_effect.f ^= CLEAN; // current_effect.f ^= CLEAN;
} else { } else {
render_effects.push(effect); render_effects.push(effect);
@ -904,7 +904,7 @@ function process_effects(root, async_effects, render_effects, effects) {
export function flushSync(fn) { export function flushSync(fn) {
var result; var result;
Fork.ensure(); Batch.ensure();
if (fn) { if (fn) {
is_flushing = true; is_flushing = true;
@ -920,11 +920,11 @@ export function flushSync(fn) {
flush_tasks(); flush_tasks();
} }
if (active_fork?.pending === 0) { if (current_batch?.pending === 0) {
active_fork.remove(); current_batch.remove();
} }
remove_active_fork(); remove_current_batch();
return /** @type {T} */ (result); return /** @type {T} */ (result);
} }

Loading…
Cancel
Save