Skip to content

Commit 4a30349

Browse files
committed
simplify
1 parent 566991b commit 4a30349

File tree

1 file changed

+15
-46
lines changed
  • packages/svelte/src/internal/client/dom/elements

1 file changed

+15
-46
lines changed

packages/svelte/src/internal/client/dom/elements/actions.js

Lines changed: 15 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { effect } from '../../reactivity/effects.js';
2-
import { set, source } from '../../reactivity/sources.js';
3-
import { deep_read_state, get, untrack } from '../../runtime.js';
1+
import { effect, render_effect } from '../../reactivity/effects.js';
2+
import { deep_read_state, untrack } from '../../runtime.js';
43

54
/**
65
* @template P
@@ -10,54 +9,24 @@ import { deep_read_state, get, untrack } from '../../runtime.js';
109
* @returns {void}
1110
*/
1211
export function action(dom, action, get_value) {
13-
/** @type {undefined | import('#client').ActionPayload<P>} */
14-
var payload = undefined;
15-
var version = source(0);
16-
var inited = false;
17-
18-
/** @type {P} */
19-
var value;
20-
21-
// The value needs to be calculated independent of the action body, as we only want to evaluate
22-
// the payload of the action once – not when the value changes.
2312
effect(() => {
24-
get(version);
13+
var payload = untrack(() => action(dom, get_value?.()) || {});
14+
var update = payload?.update;
2515

26-
if (get_value) {
27-
value = get_value();
28-
}
16+
if (get_value && update) {
17+
var inited = false;
2918

30-
if (payload !== undefined) {
31-
var update = payload.update;
32-
if (typeof update === 'function') {
33-
update(value);
34-
}
35-
}
36-
});
19+
render_effect(() => {
20+
var value = get_value();
21+
deep_read_state(value);
3722

38-
// Action could come from a prop, therefore could be a signal, therefore untrack
39-
// TODO we could take advantage of this and enable https://github.com/sveltejs/svelte/issues/6942
40-
effect(() => {
41-
untrack(() => {
42-
if (get_value) {
43-
if (payload === undefined) {
44-
payload = action(dom, value) || {};
45-
if (payload?.update) {
46-
// Action's update method is coarse-grained, i.e. when anything in the passed value changes, update.
47-
// This works in legacy mode because of mutable_source being updated as a whole, but when using $state
48-
// together with actions and mutation, it wouldn't notice the change without a deep read.
49-
effect(() => {
50-
deep_read_state(value);
51-
52-
if (inited) set(version, version.v + 1);
53-
inited = true;
54-
});
55-
}
23+
if (inited) {
24+
/** @type {Function} */ (update)(value);
5625
}
57-
} else {
58-
payload = action(dom);
59-
}
60-
});
26+
});
27+
28+
inited = true;
29+
}
6130

6231
return payload?.destroy;
6332
});

0 commit comments

Comments
 (0)