Home / Function/ batches() — svelte Function Reference

batches() — svelte Function Reference

Architecture documentation for the batches() function in batch.js from the svelte codebase.

Entity Profile

Dependency Diagram

graph TD
  dd2db25f_ec55_d3be_61b9_c180c4e4cbc8["batches()"]
  517c145b_769f_b163_6854_d8f2a4412e11["Batch"]
  dd2db25f_ec55_d3be_61b9_c180c4e4cbc8 -->|defined in| 517c145b_769f_b163_6854_d8f2a4412e11
  a199a3f7_5497_5329_2c7c_fc4555548da6["clear()"]
  dd2db25f_ec55_d3be_61b9_c180c4e4cbc8 -->|calls| a199a3f7_5497_5329_2c7c_fc4555548da6
  a08b6cc5_af73_1be4_d02f_3113cf8a8305["get()"]
  dd2db25f_ec55_d3be_61b9_c180c4e4cbc8 -->|calls| a08b6cc5_af73_1be4_d02f_3113cf8a8305
  63ee8247_ada4_9f1d_e139_0c1167cd5b1c["set()"]
  dd2db25f_ec55_d3be_61b9_c180c4e4cbc8 -->|calls| 63ee8247_ada4_9f1d_e139_0c1167cd5b1c
  75a146bb_c458_4453_bba5_066eec39b494["mark_effects()"]
  dd2db25f_ec55_d3be_61b9_c180c4e4cbc8 -->|calls| 75a146bb_c458_4453_bba5_066eec39b494
  58e25076_0e70_68a7_dbf9_e13ef9845ff2["apply()"]
  dd2db25f_ec55_d3be_61b9_c180c4e4cbc8 -->|calls| 58e25076_0e70_68a7_dbf9_e13ef9845ff2
  a0b8f840_863f_a966_d259_b866f80703d1["deactivate()"]
  dd2db25f_ec55_d3be_61b9_c180c4e4cbc8 -->|calls| a0b8f840_863f_a966_d259_b866f80703d1
  style dd2db25f_ec55_d3be_61b9_c180c4e4cbc8 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/svelte/src/internal/client/reactivity/batch.js lines 373–450

	#commit() {
		// If there are other pending batches, they now need to be 'rebased' —
		// in other words, we re-run block/async effects with the newly
		// committed state, unless the batch in question has a more
		// recent value for a given source
		if (batches.size > 1) {
			this.previous.clear();

			var previous_batch_values = batch_values;
			var is_earlier = true;

			for (const batch of batches) {
				if (batch === this) {
					is_earlier = false;
					continue;
				}

				/** @type {Source[]} */
				const sources = [];

				for (const [source, value] of this.current) {
					if (batch.current.has(source)) {
						if (is_earlier && value !== batch.current.get(source)) {
							// bring the value up to date
							batch.current.set(source, value);
						} else {
							// same value or later batch has more recent value,
							// no need to re-run these effects
							continue;
						}
					}

					sources.push(source);
				}

				if (sources.length === 0) {
					continue;
				}

				// Re-run async/block effects that depend on distinct values changed in both batches
				const others = [...batch.current.keys()].filter((s) => !this.current.has(s));
				if (others.length > 0) {
					// Avoid running queued root effects on the wrong branch
					var prev_queued_root_effects = queued_root_effects;
					queued_root_effects = [];

					/** @type {Set<Value>} */
					const marked = new Set();
					/** @type {Map<Reaction, boolean>} */
					const checked = new Map();
					for (const source of sources) {
						mark_effects(source, others, marked, checked);
					}

					if (queued_root_effects.length > 0) {
						current_batch = batch;
						batch.apply();

						for (const root of queued_root_effects) {
							batch.#traverse_effect_tree(root, [], []);
						}

						// TODO do we need to do anything with the dummy effect arrays?

						batch.deactivate();
					}

					queued_root_effects = prev_queued_root_effects;
				}
			}

			current_batch = null;
			batch_values = previous_batch_values;
		}

		this.committed = true;
		batches.delete(this);
	}

Domain

Subdomains

Frequently Asked Questions

What does batches() do?
batches() is a function in the svelte codebase, defined in packages/svelte/src/internal/client/reactivity/batch.js.
Where is batches() defined?
batches() is defined in packages/svelte/src/internal/client/reactivity/batch.js at line 373.
What does batches() call?
batches() calls 6 function(s): apply, clear, deactivate, get, mark_effects, set.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free