Home / Function/ toStore() — svelte Function Reference

toStore() — svelte Function Reference

Architecture documentation for the toStore() function in index-client.js from the svelte codebase.

Entity Profile

Dependency Diagram

graph TD
  d677216e_46d5_c70b_1d0e_82e2fcf6a340["toStore()"]
  063b7444_1ece_6350_07c7_3c77919f3844["index-client.js"]
  d677216e_46d5_c70b_1d0e_82e2fcf6a340 -->|defined in| 063b7444_1ece_6350_07c7_3c77919f3844
  ba16ff3e_8a18_d3c4_15e9_96304b539f51["writable()"]
  d677216e_46d5_c70b_1d0e_82e2fcf6a340 -->|calls| ba16ff3e_8a18_d3c4_15e9_96304b539f51
  311ef9f4_9b68_c178_c1db_3b8696f7d964["set_active_reaction()"]
  d677216e_46d5_c70b_1d0e_82e2fcf6a340 -->|calls| 311ef9f4_9b68_c178_c1db_3b8696f7d964
  55623862_10b7_5361_e30b_34ec6941f1a7["set_active_effect()"]
  d677216e_46d5_c70b_1d0e_82e2fcf6a340 -->|calls| 55623862_10b7_5361_e30b_34ec6941f1a7
  cf74814c_38ff_1817_80eb_cbc1bb490472["effect_root()"]
  d677216e_46d5_c70b_1d0e_82e2fcf6a340 -->|calls| cf74814c_38ff_1817_80eb_cbc1bb490472
  7494b934_a3b8_689e_91b6_8435e26461c5["render_effect()"]
  d677216e_46d5_c70b_1d0e_82e2fcf6a340 -->|calls| 7494b934_a3b8_689e_91b6_8435e26461c5
  4cf4c423_76ea_40eb_1692_69aae30c30f1["get()"]
  d677216e_46d5_c70b_1d0e_82e2fcf6a340 -->|calls| 4cf4c423_76ea_40eb_1692_69aae30c30f1
  style d677216e_46d5_c70b_1d0e_82e2fcf6a340 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/svelte/src/store/index-client.js lines 47–93

export function toStore(get, set) {
	var effect = active_effect;
	var reaction = active_reaction;
	var init_value = get();

	const store = writable(init_value, (set) => {
		// If the value has changed before we call subscribe, then
		// we need to treat the value as already having run
		var ran = init_value !== get();

		// TODO do we need a different implementation on the server?
		var teardown;
		// Apply the reaction and effect at the time of toStore being called
		var previous_reaction = active_reaction;
		var previous_effect = active_effect;
		set_active_reaction(reaction);
		set_active_effect(effect);

		try {
			teardown = effect_root(() => {
				render_effect(() => {
					const value = get();
					if (ran) set(value);
				});
			});
		} finally {
			set_active_reaction(previous_reaction);
			set_active_effect(previous_effect);
		}

		ran = true;

		return teardown;
	});

	if (set) {
		return {
			set,
			update: (fn) => set(fn(get())),
			subscribe: store.subscribe
		};
	}

	return {
		subscribe: store.subscribe
	};
}

Subdomains

Frequently Asked Questions

What does toStore() do?
toStore() is a function in the svelte codebase, defined in packages/svelte/src/store/index-client.js.
Where is toStore() defined?
toStore() is defined in packages/svelte/src/store/index-client.js at line 47.
What does toStore() call?
toStore() calls 6 function(s): effect_root, get, render_effect, set_active_effect, set_active_reaction, writable.

Analyze Your Own Codebase

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

Try Supermodel Free