2
2
import {
3
3
executeOnUIRuntimeSync ,
4
4
makeShareableCloneRecursive ,
5
+ makeSynchronizable ,
6
+ SynchronizableRef ,
5
7
runOnUI ,
6
8
shareableMappingCache ,
7
9
} from 'react-native-worklets' ;
@@ -94,10 +96,14 @@ function hideInternalValueProp<Value>(mutable: PartialMutable<Value>) {
94
96
} ) ;
95
97
}
96
98
97
- export function makeMutableUI < Value > ( initial : Value ) : Mutable < Value > {
99
+ export function makeMutableUI < Value > (
100
+ initial : Value ,
101
+ isDirtySynchronizable ?: SynchronizableRef < number >
102
+ ) : Mutable < Value > {
98
103
'worklet' ;
99
104
const listeners = new Map < number , Listener < Value > > ( ) ;
100
105
let value = initial ;
106
+ let isDirty = false ;
101
107
102
108
const mutable : PartialMutable < Value > = {
103
109
get value ( ) {
@@ -114,6 +120,10 @@ export function makeMutableUI<Value>(initial: Value): Mutable<Value> {
114
120
listeners . forEach ( ( listener ) => {
115
121
listener ( newValue ) ;
116
122
} ) ;
123
+ if ( isDirtySynchronizable && ! isDirty && value !== initial ) {
124
+ isDirty = true ;
125
+ isDirtySynchronizable . setBlocking ( 1 ) ;
126
+ }
117
127
} ,
118
128
modify : ( modifier , forceUpdate = true ) => {
119
129
valueSetter (
@@ -140,20 +150,26 @@ export function makeMutableUI<Value>(initial: Value): Mutable<Value> {
140
150
}
141
151
142
152
function makeMutableNative < Value > ( initial : Value ) : Mutable < Value > {
153
+ const isDirtySynchronizable = makeSynchronizable ( 0 ) ;
154
+
143
155
const handle = makeShareableCloneRecursive ( {
144
156
__init : ( ) => {
145
157
'worklet' ;
146
- return makeMutableUI ( initial ) ;
158
+ return makeMutableUI ( initial , isDirtySynchronizable ) ;
147
159
} ,
148
160
} ) ;
149
161
150
162
const mutable : PartialMutable < Value > = {
151
163
get value ( ) : Value {
152
164
checkInvalidReadDuringRender ( ) ;
153
- const uiValueGetter = executeOnUIRuntimeSync ( ( sv : Mutable < Value > ) => {
154
- return sv . value ;
155
- } ) ;
156
- return uiValueGetter ( mutable as Mutable < Value > ) ;
165
+ if ( isDirtySynchronizable . getBlocking ( ) ) {
166
+ const uiValueGetter = executeOnUIRuntimeSync ( ( sv : Mutable < Value > ) => {
167
+ return sv . value ;
168
+ } ) ;
169
+ return uiValueGetter ( mutable as Mutable < Value > ) ;
170
+ } else {
171
+ return initial ;
172
+ }
157
173
} ,
158
174
set value ( newValue ) {
159
175
checkInvalidWriteDuringRender ( ) ;
0 commit comments