MCPcopy
hub / github.com/sveltejs/svelte / toStore

Function toStore

packages/svelte/src/store/index-client.js:47–93  ·  view source on GitHub ↗
(get, set)

Source from the content-addressed store, hash-verified

45 * @returns {Writable<V> | Readable<V>}
46 */
47export function toStore(get, set) {
48 var effect = active_effect;
49 var reaction = active_reaction;
50 var init_value = get();
51
52 const store = writable(init_value, (set) => {
53 // If the value has changed before we call subscribe, then
54 // we need to treat the value as already having run
55 var ran = init_value !== get();
56
57 // TODO do we need a different implementation on the server?
58 var teardown;
59 // Apply the reaction and effect at the time of toStore being called
60 var previous_reaction = active_reaction;
61 var previous_effect = active_effect;
62 set_active_reaction(reaction);
63 set_active_effect(effect);
64
65 try {
66 teardown = effect_root(() => {
67 render_effect(() => {
68 const value = get();
69 if (ran) set(value);
70 });
71 });
72 } finally {
73 set_active_reaction(previous_reaction);
74 set_active_effect(previous_effect);
75 }
76
77 ran = true;
78
79 return teardown;
80 });
81
82 if (set) {
83 return {
84 set,
85 update: (fn) => set(fn(get())),
86 subscribe: store.subscribe
87 };
88 }
89
90 return {
91 subscribe: store.subscribe
92 };
93}
94
95/**
96 * @template V

Callers 1

test.tsFile · 0.90

Calls 8

getFunction · 0.90
writableFunction · 0.90
set_active_reactionFunction · 0.90
set_active_effectFunction · 0.90
effect_rootFunction · 0.90
render_effectFunction · 0.90
setFunction · 0.50
fnFunction · 0.50

Tested by

no test coverage detected