1
1
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' ;
4
4
5
5
/**
6
6
* @template P
7
7
* @param {Element } dom
8
8
* @param {(dom: Element, value?: P) => import('#client').ActionPayload<P> } action
9
- * @param {() => P } [value_fn ]
9
+ * @param {() => P } [get_value ]
10
10
* @returns {void }
11
11
*/
12
- export function action ( dom , action , value_fn ) {
12
+ export function action ( dom , action , get_value ) {
13
13
/** @type {undefined | import('#client').ActionPayload<P> } */
14
14
var payload = undefined ;
15
15
var version = source ( 0 ) ;
16
- /**
17
- * @type { P }
18
- */
16
+ var inited = false ;
17
+
18
+ /** @type { P } */
19
19
var value ;
20
- var init = false ;
21
20
22
21
// The value needs to be calculated independent of the action body, as we only want to evaluate
23
22
// the payload of the action once – not when the value changes.
24
23
effect ( ( ) => {
25
24
get ( version ) ;
26
- if ( value_fn ) {
27
- value = value_fn ( ) ;
25
+
26
+ if ( get_value ) {
27
+ value = get_value ( ) ;
28
28
}
29
+
29
30
if ( payload !== undefined ) {
30
31
var update = payload . update ;
31
32
if ( typeof update === 'function' ) {
@@ -38,7 +39,7 @@ export function action(dom, action, value_fn) {
38
39
// TODO we could take advantage of this and enable https://github.com/sveltejs/svelte/issues/6942
39
40
effect ( ( ) => {
40
41
untrack ( ( ) => {
41
- if ( value_fn ) {
42
+ if ( get_value ) {
42
43
if ( payload === undefined ) {
43
44
payload = action ( dom , value ) || { } ;
44
45
if ( payload ?. update ) {
@@ -47,29 +48,17 @@ export function action(dom, action, value_fn) {
47
48
// together with actions and mutation, it wouldn't notice the change without a deep read.
48
49
effect ( ( ) => {
49
50
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 ;
56
54
} ) ;
57
55
}
58
56
}
59
57
} else {
60
58
payload = action ( dom ) ;
61
59
}
62
60
} ) ;
63
- } ) ;
64
61
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 ;
74
63
} ) ;
75
64
}
0 commit comments