Home / Class/ BranchManager Class — svelte Architecture

BranchManager Class — svelte Architecture

Architecture documentation for the BranchManager class in branches.js from the svelte codebase.

Entity Profile

Dependency Diagram

graph TD
  60ab4ae8_779f_562b_010b_c2c26a4235d8["BranchManager"]
  ee9aaeee_d18b_8f23_1c50_ace68f975516["branches.js"]
  60ab4ae8_779f_562b_010b_c2c26a4235d8 -->|defined in| ee9aaeee_d18b_8f23_1c50_ace68f975516
  93c400b8_771c_e542_9910_46358bd40c62["constructor()"]
  60ab4ae8_779f_562b_010b_c2c26a4235d8 -->|method| 93c400b8_771c_e542_9910_46358bd40c62
  8783b6d3_ff2d_7032_cf54_f6a71da85e37["ensure()"]
  60ab4ae8_779f_562b_010b_c2c26a4235d8 -->|method| 8783b6d3_ff2d_7032_cf54_f6a71da85e37

Relationship Graph

Source Code

packages/svelte/src/internal/client/dom/blocks/branches.js lines 20–227

export class BranchManager {
	/** @type {TemplateNode} */
	anchor;

	/** @type {Map<Batch, Key>} */
	#batches = new Map();

	/**
	 * Map of keys to effects that are currently rendered in the DOM.
	 * These effects are visible and actively part of the document tree.
	 * Example:
	 * ```
	 * {#if condition}
	 * 	foo
	 * {:else}
	 * 	bar
	 * {/if}
	 * ```
	 * Can result in the entries `true->Effect` and `false->Effect`
	 * @type {Map<Key, Effect>}
	 */
	#onscreen = new Map();

	/**
	 * Similar to #onscreen with respect to the keys, but contains branches that are not yet
	 * in the DOM, because their insertion is deferred.
	 * @type {Map<Key, Branch>}
	 */
	#offscreen = new Map();

	/**
	 * Keys of effects that are currently outroing
	 * @type {Set<Key>}
	 */
	#outroing = new Set();

	/**
	 * Whether to pause (i.e. outro) on change, or destroy immediately.
	 * This is necessary for `<svelte:element>`
	 */
	#transition = true;

	/**
	 * @param {TemplateNode} anchor
	 * @param {boolean} transition
	 */
	constructor(anchor, transition = true) {
		this.anchor = anchor;
		this.#transition = transition;
	}

	#commit = () => {
		var batch = /** @type {Batch} */ (current_batch);

		// if this batch was made obsolete, bail
		if (!this.#batches.has(batch)) return;

		var key = /** @type {Key} */ (this.#batches.get(batch));

		var onscreen = this.#onscreen.get(key);

		if (onscreen) {
			// effect is already in the DOM — abort any current outro
			resume_effect(onscreen);
			this.#outroing.delete(key);
		} else {
			// effect is currently offscreen. put it in the DOM
			var offscreen = this.#offscreen.get(key);

			if (offscreen) {
				this.#onscreen.set(key, offscreen.effect);
				this.#offscreen.delete(key);

				// remove the anchor...
				/** @type {TemplateNode} */ (offscreen.fragment.lastChild).remove();

				// ...and append the fragment
				this.anchor.before(offscreen.fragment);
				onscreen = offscreen.effect;
			}
		}

Domain

Frequently Asked Questions

What is the BranchManager class?
BranchManager is a class in the svelte codebase, defined in packages/svelte/src/internal/client/dom/blocks/branches.js.
Where is BranchManager defined?
BranchManager is defined in packages/svelte/src/internal/client/dom/blocks/branches.js at line 20.

Analyze Your Own Codebase

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

Try Supermodel Free