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' ;
4
3
5
4
/**
6
5
* @template P
@@ -10,54 +9,24 @@ import { deep_read_state, get, untrack } from '../../runtime.js';
10
9
* @returns {void }
11
10
*/
12
11
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.
23
12
effect ( ( ) => {
24
- get ( version ) ;
13
+ var payload = untrack ( ( ) => action ( dom , get_value ?. ( ) ) || { } ) ;
14
+ var update = payload ?. update ;
25
15
26
- if ( get_value ) {
27
- value = get_value ( ) ;
28
- }
16
+ if ( get_value && update ) {
17
+ var inited = false ;
29
18
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 ) ;
37
22
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 ) ;
56
25
}
57
- } else {
58
- payload = action ( dom ) ;
59
- }
60
- } ) ;
26
+ } ) ;
27
+
28
+ inited = true ;
29
+ }
61
30
62
31
return payload ?. destroy ;
63
32
} ) ;
0 commit comments