1
1
'use strict' ;
2
2
import { shouldBeUseWeb } from './PlatformChecker' ;
3
3
import type { Mutable } from './commonTypes' ;
4
- import { makeShareableCloneRecursive } from './shareables' ;
5
4
import { shareableMappingCache } from './shareableMappingCache' ;
5
+ import { makeShareableCloneRecursive } from './shareables' ;
6
6
import { executeOnUIRuntimeSync , runOnUI } from './threads' ;
7
7
import { valueSetter } from './valueSetter' ;
8
8
9
9
const SHOULD_BE_USE_WEB = shouldBeUseWeb ( ) ;
10
10
11
11
type Listener < Value > = ( newValue : Value ) => void ;
12
12
13
- export function makeUIMutable < Value > ( initial : Value ) : Mutable < Value > {
13
+ export function makeMutableUI < Value > ( initial : Value ) : Mutable < Value > {
14
14
'worklet' ;
15
-
16
15
const listeners = new Map < number , Listener < Value > > ( ) ;
17
16
let value = initial ;
18
17
19
- const self : Mutable < Value > = {
20
- set value ( newValue ) {
21
- valueSetter ( self , newValue ) ;
22
- } ,
18
+ const mutable : Mutable < Value > = {
23
19
get value ( ) {
24
20
return value ;
25
21
} ,
22
+ set value ( newValue ) {
23
+ valueSetter ( mutable , newValue ) ;
24
+ } ,
25
+
26
26
/**
27
27
* _value prop should only be accessed by the valueSetter implementation
28
28
* which may make the decision about updating the mutable value depending
29
29
* on the provided new value. All other places should only attempt to modify
30
30
* the mutable by assigning to value prop directly.
31
31
*/
32
+ get _value ( ) : Value {
33
+ return value ;
34
+ } ,
32
35
set _value ( newValue : Value ) {
33
36
value = newValue ;
34
37
listeners . forEach ( ( listener ) => {
35
38
listener ( newValue ) ;
36
39
} ) ;
37
40
} ,
38
- get _value ( ) : Value {
39
- return value ;
40
- } ,
41
+
41
42
modify : ( modifier , forceUpdate = true ) => {
42
43
valueSetter (
43
- self ,
44
+ mutable ,
44
45
modifier !== undefined ? modifier ( value ) : value ,
45
46
forceUpdate
46
47
) ;
@@ -51,94 +52,110 @@ export function makeUIMutable<Value>(initial: Value): Mutable<Value> {
51
52
removeListener : ( id : number ) => {
52
53
listeners . delete ( id ) ;
53
54
} ,
55
+
54
56
_animation : null ,
55
57
_isReanimatedSharedValue : true ,
56
58
} ;
57
- return self ;
59
+ return mutable ;
58
60
}
59
61
60
- export function makeMutable < Value > ( initial : Value ) : Mutable < Value > {
61
- let value : Value = initial ;
62
+ function makeMutableNative < Value > ( initial : Value ) : Mutable < Value > {
62
63
const handle = makeShareableCloneRecursive ( {
63
64
__init : ( ) => {
64
65
'worklet' ;
65
- return makeUIMutable ( initial ) ;
66
+ return makeMutableUI ( initial ) ;
66
67
} ,
67
68
} ) ;
68
- // listeners can only work on JS thread on Web and jest environments
69
- const listeners = SHOULD_BE_USE_WEB
70
- ? new Map < number , Listener < Value > > ( )
71
- : undefined ;
69
+
72
70
const mutable : Mutable < Value > = {
73
- set value ( newValue ) {
74
- if ( SHOULD_BE_USE_WEB ) {
75
- valueSetter ( mutable , newValue ) ;
76
- } else {
77
- runOnUI ( ( ) => {
78
- mutable . value = newValue ;
79
- } ) ( ) ;
80
- }
81
- } ,
82
71
get value ( ) : Value {
83
- if ( SHOULD_BE_USE_WEB ) {
84
- return value ;
85
- }
86
72
const uiValueGetter = executeOnUIRuntimeSync ( ( sv : Mutable < Value > ) => {
87
73
return sv . value ;
88
74
} ) ;
89
75
return uiValueGetter ( mutable ) ;
90
76
} ,
91
- set _value ( newValue : Value ) {
92
- if ( ! SHOULD_BE_USE_WEB ) {
93
- throw new Error (
94
- '[Reanimated] Setting `_value` directly is only possible on the UI runtime. Perhaps you want to assign to `value` instead?'
95
- ) ;
96
- }
97
- value = newValue ;
98
- listeners ! . forEach ( ( listener ) => {
99
- listener ( newValue ) ;
100
- } ) ;
77
+ set value ( newValue ) {
78
+ runOnUI ( ( ) => {
79
+ mutable . value = newValue ;
80
+ } ) ( ) ;
101
81
} ,
82
+
102
83
get _value ( ) : Value {
103
- if ( SHOULD_BE_USE_WEB ) {
104
- return value ;
105
- }
106
84
throw new Error (
107
85
'[Reanimated] Reading from `_value` directly is only possible on the UI runtime. Perhaps you passed an Animated Style to a non-animated component?'
108
86
) ;
109
87
} ,
88
+ set _value ( _newValue : Value ) {
89
+ throw new Error (
90
+ '[Reanimated] Setting `_value` directly is only possible on the UI runtime. Perhaps you want to assign to `value` instead?'
91
+ ) ;
92
+ } ,
93
+
94
+ modify : ( modifier , forceUpdate = true ) => {
95
+ runOnUI ( ( ) => {
96
+ mutable . modify ( modifier , forceUpdate ) ;
97
+ } ) ( ) ;
98
+ } ,
99
+ addListener : ( ) => {
100
+ throw new Error (
101
+ '[Reanimated] Adding listeners is only possible on the UI runtime.'
102
+ ) ;
103
+ } ,
104
+ removeListener : ( ) => {
105
+ throw new Error (
106
+ '[Reanimated] Removing listeners is only possible on the UI runtime.'
107
+ ) ;
108
+ } ,
109
+
110
+ _isReanimatedSharedValue : true ,
111
+ } ;
112
+
113
+ shareableMappingCache . set ( mutable , handle ) ;
114
+ return mutable ;
115
+ }
116
+
117
+ function makeMutableWeb < Value > ( initial : Value ) : Mutable < Value > {
118
+ let value : Value = initial ;
119
+ const listeners = new Map < number , Listener < Value > > ( ) ;
120
+
121
+ const mutable : Mutable < Value > = {
122
+ get value ( ) : Value {
123
+ return value ;
124
+ } ,
125
+ set value ( newValue ) {
126
+ valueSetter ( mutable , newValue ) ;
127
+ } ,
128
+
129
+ get _value ( ) : Value {
130
+ return value ;
131
+ } ,
132
+ set _value ( newValue : Value ) {
133
+ value = newValue ;
134
+ listeners . forEach ( ( listener ) => {
135
+ listener ( newValue ) ;
136
+ } ) ;
137
+ } ,
110
138
111
139
modify : ( modifier , forceUpdate = true ) => {
112
- if ( ! SHOULD_BE_USE_WEB ) {
113
- runOnUI ( ( ) => {
114
- mutable . modify ( modifier , forceUpdate ) ;
115
- } ) ( ) ;
116
- } else {
117
- valueSetter (
118
- mutable ,
119
- modifier !== undefined ? modifier ( mutable . value ) : mutable . value ,
120
- forceUpdate
121
- ) ;
122
- }
140
+ valueSetter (
141
+ mutable ,
142
+ modifier !== undefined ? modifier ( mutable . value ) : mutable . value ,
143
+ forceUpdate
144
+ ) ;
123
145
} ,
124
146
addListener : ( id : number , listener : Listener < Value > ) => {
125
- if ( ! SHOULD_BE_USE_WEB ) {
126
- throw new Error (
127
- '[Reanimated] Adding listeners is only possible on the UI runtime.'
128
- ) ;
129
- }
130
- listeners ! . set ( id , listener ) ;
147
+ listeners . set ( id , listener ) ;
131
148
} ,
132
149
removeListener : ( id : number ) => {
133
- if ( ! SHOULD_BE_USE_WEB ) {
134
- throw new Error (
135
- '[Reanimated] Removing listeners is only possible on the UI runtime.'
136
- ) ;
137
- }
138
- listeners ! . delete ( id ) ;
150
+ listeners . delete ( id ) ;
139
151
} ,
152
+
140
153
_isReanimatedSharedValue : true ,
141
154
} ;
142
- shareableMappingCache . set ( mutable , handle ) ;
155
+
143
156
return mutable ;
144
157
}
158
+
159
+ export const makeMutable = SHOULD_BE_USE_WEB
160
+ ? makeMutableWeb
161
+ : makeMutableNative ;
0 commit comments