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

Function action

packages/svelte/src/internal/client/dom/elements/actions.js:13–43  ·  view source on GitHub ↗
(dom, action, get_value)

Source from the content-addressed store, hash-verified

11 * @returns {void}
12 */
13export function action(dom, action, get_value) {
14 effect(() => {
15 var payload = untrack(() => action(dom, get_value?.()) || {});
16
17 if (get_value && payload?.update) {
18 var inited = false;
19 /** @type {P} */
20 var prev = /** @type {any} */ ({}); // initialize with something so it's never equal on first run
21
22 render_effect(() => {
23 var value = get_value();
24
25 // Action's update method is coarse-grained, i.e. when anything in the passed value changes, update.
26 // This works in legacy mode because of mutable_source being updated as a whole, but when using $state
27 // together with actions and mutation, it wouldn't notice the change without a deep read.
28 deep_read_state(value);
29
30 if (inited && safe_not_equal(prev, value)) {
31 prev = value;
32 /** @type {Function} */ (payload.update)(value);
33 }
34 });
35
36 inited = true;
37 }
38
39 if (payload?.destroy) {
40 return () => /** @type {Function} */ (payload.destroy)();
41 }
42 });
43}

Callers 2

fromActionFunction · 0.85
with_tmp_dirFunction · 0.85

Calls 6

effectFunction · 0.90
untrackFunction · 0.90
render_effectFunction · 0.90
deep_read_stateFunction · 0.90
safe_not_equalFunction · 0.90
get_valueFunction · 0.85

Tested by

no test coverage detected