Home / Function/ action() — svelte Function Reference

action() — svelte Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  fe849ab3_05c7_5165_9402_4485495c5927["action()"]
  8a7e1359_80ee_d99b_c857_a4077af52e5a["actions.js"]
  fe849ab3_05c7_5165_9402_4485495c5927 -->|defined in| 8a7e1359_80ee_d99b_c857_a4077af52e5a
  a985ae40_8ef8_7ef2_adad_116fbf97e70c["effect()"]
  fe849ab3_05c7_5165_9402_4485495c5927 -->|calls| a985ae40_8ef8_7ef2_adad_116fbf97e70c
  a814b193_e12a_4037_c3c8_dfd45f3bd0bb["untrack()"]
  fe849ab3_05c7_5165_9402_4485495c5927 -->|calls| a814b193_e12a_4037_c3c8_dfd45f3bd0bb
  7494b934_a3b8_689e_91b6_8435e26461c5["render_effect()"]
  fe849ab3_05c7_5165_9402_4485495c5927 -->|calls| 7494b934_a3b8_689e_91b6_8435e26461c5
  14da5ffa_bc19_aee6_36b3_bbba24db7f02["deep_read_state()"]
  fe849ab3_05c7_5165_9402_4485495c5927 -->|calls| 14da5ffa_bc19_aee6_36b3_bbba24db7f02
  b3727276_e6f9_12a7_bfa7_3faf14f08373["safe_not_equal()"]
  fe849ab3_05c7_5165_9402_4485495c5927 -->|calls| b3727276_e6f9_12a7_bfa7_3faf14f08373
  style fe849ab3_05c7_5165_9402_4485495c5927 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/svelte/src/internal/client/dom/elements/actions.js lines 13–43

export function action(dom, action, get_value) {
	effect(() => {
		var payload = untrack(() => action(dom, get_value?.()) || {});

		if (get_value && payload?.update) {
			var inited = false;
			/** @type {P} */
			var prev = /** @type {any} */ ({}); // initialize with something so it's never equal on first run

			render_effect(() => {
				var value = get_value();

				// Action's update method is coarse-grained, i.e. when anything in the passed value changes, update.
				// This works in legacy mode because of mutable_source being updated as a whole, but when using $state
				// together with actions and mutation, it wouldn't notice the change without a deep read.
				deep_read_state(value);

				if (inited && safe_not_equal(prev, value)) {
					prev = value;
					/** @type {Function} */ (payload.update)(value);
				}
			});

			inited = true;
		}

		if (payload?.destroy) {
			return () => /** @type {Function} */ (payload.destroy)();
		}
	});
}

Domain

Subdomains

Frequently Asked Questions

What does action() do?
action() is a function in the svelte codebase, defined in packages/svelte/src/internal/client/dom/elements/actions.js.
Where is action() defined?
action() is defined in packages/svelte/src/internal/client/dom/elements/actions.js at line 13.
What does action() call?
action() calls 5 function(s): deep_read_state, effect, render_effect, safe_not_equal, untrack.

Analyze Your Own Codebase

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

Try Supermodel Free