(dom, action, get_value)
| 11 | * @returns {void} |
| 12 | */ |
| 13 | export 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 | } |
no test coverage detected