-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathWorkletExample.tsx
More file actions
276 lines (230 loc) · 6.51 KB
/
WorkletExample.tsx
File metadata and controls
276 lines (230 loc) · 6.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/* eslint-disable react-native/no-inline-styles */
/* global _WORKLET */
import Animated, {
runOnJS,
runOnUI,
useAnimatedGestureHandler,
useAnimatedRef,
useAnimatedScrollHandler,
useAnimatedStyle,
useDerivedValue,
useFrameCallback,
useScrollViewOffset,
useSharedValue,
} from 'react-native-reanimated';
import { Button, Text, View } from 'react-native';
import {
Gesture,
GestureDetector,
PanGestureHandler,
} from 'react-native-gesture-handler';
import React from 'react';
declare global {
const _WORKLET: boolean;
}
function RunOnUIDemo() {
const someWorklet = (x: number) => {
'worklet';
console.log(_WORKLET, x); // _WORKLET should be true
};
const handlePress = () => {
runOnUI(someWorklet)(Math.random());
};
return <Button onPress={handlePress} title="runOnUI demo" />;
}
function RunOnUIRunOnJSDemo() {
const someFunction = (x: number) => {
console.log(_WORKLET, x); // _WORKLET should be false
};
const someWorklet = (x: number) => {
'worklet';
console.log(_WORKLET, x); // _WORKLET should be true
runOnJS(someFunction)(x);
};
const handlePress = () => {
runOnUI(someWorklet)(Math.random());
};
return <Button onPress={handlePress} title="runOnUI + runOnJS demo" />;
}
function UseDerivedValueRunOnJSDemo() {
const sv = useSharedValue(0);
const someFunction = (x: number) => {
console.log(_WORKLET, x); // _WORKLET should be false
};
useDerivedValue(() => {
console.log(_WORKLET, sv.value);
runOnJS(someFunction)(sv.value);
});
const handlePress = () => {
sv.value = 1 + Math.random();
};
return (
<Button onPress={handlePress} title="useDerivedValue + runOnJS demo" />
);
}
function ThrowErrorDemo() {
const handlePress = () => {
throw new Error('Hello world from React Native JS!');
};
return <Button onPress={handlePress} title="Throw error on JS" />;
}
function ThrowErrorWorkletDemo() {
const someWorklet = () => {
'worklet';
throw new Error('Hello world from worklet!');
};
const handlePress = () => {
runOnUI(someWorklet)();
};
return <Button onPress={handlePress} title="Throw error from worklet" />;
}
function ThrowErrorNestedWorkletDemo() {
const innerWorklet = () => {
'worklet';
throw new Error('Hello world from nested worklet!');
};
const outerWorklet = () => {
'worklet';
innerWorklet();
};
const handlePress = () => {
runOnUI(outerWorklet)();
};
return (
<Button onPress={handlePress} title="Throw error from nested worklet" />
);
}
function ThrowErrorFromUseAnimatedStyleDemo() {
const sv = useSharedValue(0);
useAnimatedStyle(() => {
if (!_WORKLET || sv.value === 0) {
return {}; // prevent throwing error on first render or from JS context
}
throw new Error('Hello world from useAnimatedStyle!');
});
const handlePress = () => {
sv.value = 1 + Math.random();
};
return (
<Button onPress={handlePress} title="Throw error from useAnimatedStyle" />
);
}
function ThrowErrorFromUseDerivedValueDemo() {
const sv = useSharedValue(0);
useDerivedValue(() => {
if (!_WORKLET || sv.value === 0) {
return {}; // prevent throwing error on first render or from JS context
}
throw new Error('Hello world from useDerivedValue!');
}, [sv]);
const handlePress = () => {
sv.value = 1 + Math.random();
};
return (
<Button onPress={handlePress} title="Throw error from useDerivedValue" />
);
}
function ThrowErrorFromUseFrameCallbackDemo() {
const sv = useSharedValue(false);
useFrameCallback(() => {
if (sv.value) {
sv.value = false;
throw new Error('Hello world from useFrameCallback!');
}
}, true);
const handlePress = () => {
sv.value = true;
};
return (
<Button onPress={handlePress} title="Throw error from useFrameCallback" />
);
}
function ThrowErrorFromGestureDetectorDemo() {
const gesture = Gesture.Pan().onChange(() => {
throw Error('Hello world from GestureDetector callback!');
});
return (
<GestureDetector gesture={gesture}>
<View style={{ width: 100, height: 100, backgroundColor: 'tomato' }}>
<Text>GestureDetector</Text>
</View>
</GestureDetector>
);
}
function ThrowErrorFromUseAnimatedGestureHandlerDemo() {
const gestureHandler = useAnimatedGestureHandler({
onActive: () => {
throw Error('Hello world from useAnimatedGestureHandler');
},
});
return (
<PanGestureHandler onGestureEvent={gestureHandler}>
<Animated.View
style={{ width: 100, height: 100, backgroundColor: 'gold' }}>
<Text>PanGestureHandler + useAnimatedGestureHandler</Text>
</Animated.View>
</PanGestureHandler>
);
}
function ThrowErrorFromUseAnimatedScrollHandlerDemo() {
const scrollHandler = useAnimatedScrollHandler(() => {
throw Error('Hello world from useAnimatedScrollHandler');
});
return (
<View style={{ height: 100 }}>
<Animated.ScrollView scrollEventThrottle={16} onScroll={scrollHandler}>
<View
style={{
width: 100,
height: 500,
backgroundColor: 'lime',
}}>
<Text>useAnimatedScrollHandler</Text>
</View>
</Animated.ScrollView>
</View>
);
}
function ThrowErrorFromUseScrollViewOffsetDemo() {
const aref = useAnimatedRef<Animated.ScrollView>();
const offset = useScrollViewOffset(aref);
useAnimatedStyle(() => {
if (_WORKLET && offset.value > 0) {
throw Error('Hello world from useScrollViewOffset');
}
return {};
});
return (
<View style={{ height: 100 }}>
<Animated.ScrollView scrollEventThrottle={16} ref={aref}>
<View
style={{
width: 100,
height: 500,
backgroundColor: 'cyan',
}}>
<Text>useScrollViewOffset + useAnimatedStyle</Text>
</View>
</Animated.ScrollView>
</View>
);
}
export default function WorkletExample() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<RunOnUIDemo />
<RunOnUIRunOnJSDemo />
<UseDerivedValueRunOnJSDemo />
<ThrowErrorDemo />
<ThrowErrorWorkletDemo />
<ThrowErrorNestedWorkletDemo />
<ThrowErrorFromUseAnimatedStyleDemo />
<ThrowErrorFromUseDerivedValueDemo />
<ThrowErrorFromUseFrameCallbackDemo />
<ThrowErrorFromGestureDetectorDemo />
<ThrowErrorFromUseAnimatedGestureHandlerDemo />
<ThrowErrorFromUseAnimatedScrollHandlerDemo />
<ThrowErrorFromUseScrollViewOffsetDemo />
</View>
);
}