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