Home / Function/ EachBlock() — svelte Function Reference

EachBlock() — svelte Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  7271af10_6d98_1164_127d_943c5a469db9["EachBlock()"]
  f0f2f8c6_dafa_048c_81e7_9a2e89e2dd23["EachBlock.js"]
  7271af10_6d98_1164_127d_943c5a469db9 -->|defined in| f0f2f8c6_dafa_048c_81e7_9a2e89e2dd23
  66243dec_e2be_a66d_3992_3ea42cf964b5["build_expression()"]
  7271af10_6d98_1164_127d_943c5a469db9 -->|calls| 66243dec_e2be_a66d_3992_3ea42cf964b5
  b1380aab_0ea6_e12d_3df0_c3526fef2b75["get_value()"]
  7271af10_6d98_1164_127d_943c5a469db9 -->|calls| b1380aab_0ea6_e12d_3df0_c3526fef2b75
  5ff7d39f_78e8_c057_8c8e_279f6e72149e["add()"]
  7271af10_6d98_1164_127d_943c5a469db9 -->|calls| 5ff7d39f_78e8_c057_8c8e_279f6e72149e
  cea9d793_c35f_96ef_2911_32d1fa423c0e["collect_parent_each_blocks()"]
  7271af10_6d98_1164_127d_943c5a469db9 -->|calls| cea9d793_c35f_96ef_2911_32d1fa423c0e
  c254e734_2224_c309_f1f8_bb064e80b1af["extract_paths()"]
  7271af10_6d98_1164_127d_943c5a469db9 -->|calls| c254e734_2224_c309_f1f8_bb064e80b1af
  575bfb79_8777_5076_eda3_be015882482a["add_svelte_meta()"]
  7271af10_6d98_1164_127d_943c5a469db9 -->|calls| 575bfb79_8777_5076_eda3_be015882482a
  c8d80c9d_ab6c_13c0_c85e_a9b120f82f45["blockers()"]
  7271af10_6d98_1164_127d_943c5a469db9 -->|calls| c8d80c9d_ab6c_13c0_c85e_a9b120f82f45
  style 7271af10_6d98_1164_127d_943c5a469db9 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/svelte/src/compiler/phases/3-transform/client/visitors/EachBlock.js lines 22–355

export function EachBlock(node, context) {
	const each_node_meta = node.metadata;

	// expression should be evaluated in the parent scope, not the scope
	// created by the each block itself
	const parent_scope_state = {
		...context.state,
		scope: /** @type {Scope} */ (context.state.scope.parent)
	};

	const collection = build_expression(
		{
			...context,
			state: parent_scope_state
		},
		node.expression,
		node.metadata.expression
	);

	if (!each_node_meta.is_controlled) {
		context.state.template.push_comment();
	}

	let flags = 0;

	if (node.metadata.keyed && node.index) {
		flags |= EACH_INDEX_REACTIVE;
	}

	const key_is_item =
		node.key?.type === 'Identifier' &&
		node.context?.type === 'Identifier' &&
		node.context?.name === node.key.name;

	// if the each block expression references a store subscription, we need
	// to use mutable stores internally
	let uses_store;

	for (const binding of node.metadata.expression.dependencies) {
		if (binding.kind === 'store_sub') {
			uses_store = true;
			break;
		}
	}

	for (const binding of node.metadata.expression.dependencies) {
		// if the expression doesn't reference any external state, we don't need to
		// create a source for the item. TODO cover more cases (e.g. `x.filter(y)`
		// should also qualify if `y` doesn't reference state, and non-state
		// bindings should also be fine
		if (binding.scope.function_depth >= context.state.scope.function_depth) {
			continue;
		}

		if (!context.state.analysis.runes || !key_is_item || uses_store) {
			flags |= EACH_ITEM_REACTIVE;
			break;
		}
	}

	if (context.state.analysis.runes && !uses_store) {
		flags |= EACH_ITEM_IMMUTABLE;
	}

	// Since `animate:` can only appear on elements that are the sole child of a keyed each block,
	// we can determine at compile time whether the each block is animated or not (in which
	// case it should measure animated elements before and after reconciliation).
	if (
		node.key &&
		node.body.nodes.some((child) => {
			if (child.type !== 'RegularElement' && child.type !== 'SvelteElement') return false;
			return child.attributes.some((attr) => attr.type === 'AnimateDirective');
		})
	) {
		flags |= EACH_IS_ANIMATED;
	}

	if (each_node_meta.is_controlled) {
		flags |= EACH_IS_CONTROLLED;
	}

Domain

Subdomains

Frequently Asked Questions

What does EachBlock() do?
EachBlock() is a function in the svelte codebase, defined in packages/svelte/src/compiler/phases/3-transform/client/visitors/EachBlock.js.
Where is EachBlock() defined?
EachBlock() is defined in packages/svelte/src/compiler/phases/3-transform/client/visitors/EachBlock.js at line 22.
What does EachBlock() call?
EachBlock() calls 7 function(s): add, add_svelte_meta, blockers, build_expression, collect_parent_each_blocks, extract_paths, get_value.

Analyze Your Own Codebase

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

Try Supermodel Free