Skip to content

Commit 566991b

Browse files
committed
tweaks
1 parent bc6f1ce commit 566991b

File tree

1 file changed

+16
-27
lines changed
  • packages/svelte/src/internal/client/dom/elements

1 file changed

+16
-27
lines changed
Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
11
import { effect } from '../../reactivity/effects.js';
2-
import { source } from '../../reactivity/sources.js';
3-
import { deep_read_state, get, untrack, update } from '../../runtime.js';
2+
import { set, source } from '../../reactivity/sources.js';
3+
import { deep_read_state, get, untrack } from '../../runtime.js';
44

55
/**
66
* @template P
77
* @param {Element} dom
88
* @param {(dom: Element, value?: P) => import('#client').ActionPayload<P>} action
9-
* @param {() => P} [value_fn]
9+
* @param {() => P} [get_value]
1010
* @returns {void}
1111
*/
12-
export function action(dom, action, value_fn) {
12+
export function action(dom, action, get_value) {
1313
/** @type {undefined | import('#client').ActionPayload<P>} */
1414
var payload = undefined;
1515
var version = source(0);
16-
/**
17-
* @type {P}
18-
*/
16+
var inited = false;
17+
18+
/** @type {P} */
1919
var value;
20-
var init = false;
2120

2221
// The value needs to be calculated independent of the action body, as we only want to evaluate
2322
// the payload of the action once – not when the value changes.
2423
effect(() => {
2524
get(version);
26-
if (value_fn) {
27-
value = value_fn();
25+
26+
if (get_value) {
27+
value = get_value();
2828
}
29+
2930
if (payload !== undefined) {
3031
var update = payload.update;
3132
if (typeof update === 'function') {
@@ -38,7 +39,7 @@ export function action(dom, action, value_fn) {
3839
// TODO we could take advantage of this and enable https://github.com/sveltejs/svelte/issues/6942
3940
effect(() => {
4041
untrack(() => {
41-
if (value_fn) {
42+
if (get_value) {
4243
if (payload === undefined) {
4344
payload = action(dom, value) || {};
4445
if (payload?.update) {
@@ -47,29 +48,17 @@ export function action(dom, action, value_fn) {
4748
// together with actions and mutation, it wouldn't notice the change without a deep read.
4849
effect(() => {
4950
deep_read_state(value);
50-
if (init) {
51-
untrack(() => {
52-
update(version, 1);
53-
});
54-
}
55-
init = true;
51+
52+
if (inited) set(version, version.v + 1);
53+
inited = true;
5654
});
5755
}
5856
}
5957
} else {
6058
payload = action(dom);
6159
}
6260
});
63-
});
6461

65-
effect(() => {
66-
if (payload !== undefined) {
67-
var destroy = payload.destroy;
68-
if (typeof destroy === 'function') {
69-
return () => {
70-
/** @type {Function} */ (destroy)();
71-
};
72-
}
73-
}
62+
return payload?.destroy;
7463
});
7564
}

0 commit comments

Comments
 (0)