@@ -10,6 +10,39 @@ const SHOULD_BE_USE_WEB = shouldBeUseWeb();
10
10
11
11
type Listener < Value > = ( newValue : Value ) => void ;
12
12
13
+ type PartialMutable < Value > = Omit < Mutable < Value > , '_value' | 'get' | 'set' > ;
14
+
15
+ /**
16
+ * Adds `get` and `set` methods to the mutable object to handle access to `value` property.
17
+ *
18
+ * React Compiler disallows modifying return values of hooks. Even though assignment to
19
+ * `value` is a setter invocation, Compiler's static analysis doesn't detect it.
20
+ * That's why we provide a second API for users using the Compiler.
21
+ */
22
+ function addCompilerSafeGetAndSet < Value > ( mutable : Mutable < Value > ) : void {
23
+ 'worklet' ;
24
+ Object . defineProperties ( mutable , {
25
+ get : {
26
+ value ( ) {
27
+ return mutable . value ;
28
+ } ,
29
+ configurable : false ,
30
+ enumerable : false ,
31
+ } ,
32
+ set : {
33
+ value ( newValue : Value | ( ( value : Value ) => Value ) ) {
34
+ if ( typeof newValue === 'function' ) {
35
+ mutable . value = ( newValue as ( value : Value ) => Value ) ( mutable . value ) ;
36
+ } else {
37
+ mutable . value = newValue ;
38
+ }
39
+ } ,
40
+ configurable : false ,
41
+ enumerable : false ,
42
+ } ,
43
+ } ) ;
44
+ }
45
+
13
46
export function makeMutableUI < Value > ( initial : Value ) : Mutable < Value > {
14
47
'worklet' ;
15
48
const listeners = new Map < number , Listener < Value > > ( ) ;
@@ -39,7 +72,7 @@ export function makeMutableUI<Value>(initial: Value): Mutable<Value> {
39
72
40
73
_animation : null ,
41
74
_isReanimatedSharedValue : true ,
42
- } as Mutable < Value > ;
75
+ } as PartialMutable < Value > as Mutable < Value > ;
43
76
44
77
/*
45
78
* _value prop should only be accessed by the valueSetter implementation
@@ -61,6 +94,8 @@ export function makeMutableUI<Value>(initial: Value): Mutable<Value> {
61
94
enumerable : false ,
62
95
} ) ;
63
96
97
+ addCompilerSafeGetAndSet ( mutable ) ;
98
+
64
99
return mutable ;
65
100
}
66
101
@@ -102,7 +137,7 @@ function makeMutableNative<Value>(initial: Value): Mutable<Value> {
102
137
} ,
103
138
104
139
_isReanimatedSharedValue : true ,
105
- } as Omit < Mutable < Value > , '_value' > as Mutable < Value > ;
140
+ } as PartialMutable < Value > as Mutable < Value > ;
106
141
107
142
Object . defineProperty ( mutable , '_value' , {
108
143
// This way of defining the property makes it hidden for
@@ -122,6 +157,8 @@ function makeMutableNative<Value>(initial: Value): Mutable<Value> {
122
157
enumerable : false ,
123
158
} ) ;
124
159
160
+ addCompilerSafeGetAndSet ( mutable ) ;
161
+
125
162
shareableMappingCache . set ( mutable , handle ) ;
126
163
return mutable ;
127
164
}
@@ -153,7 +190,7 @@ function makeMutableWeb<Value>(initial: Value): Mutable<Value> {
153
190
} ,
154
191
155
192
_isReanimatedSharedValue : true ,
156
- } as Omit < Mutable < Value > , '_value' > as Mutable < Value > ;
193
+ } as PartialMutable < Value > as Mutable < Value > ;
157
194
158
195
Object . defineProperty ( mutable , '_value' , {
159
196
get ( ) : Value {
@@ -169,6 +206,8 @@ function makeMutableWeb<Value>(initial: Value): Mutable<Value> {
169
206
enumerable : false ,
170
207
} ) ;
171
208
209
+ addCompilerSafeGetAndSet ( mutable ) ;
210
+
172
211
return mutable ;
173
212
}
174
213
0 commit comments