-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
core.ts
382 lines (347 loc) · 10.3 KB
/
core.ts
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/* global _WORKLET _getCurrentTime _frameTimestamp _eventTimestamp _setGlobalConsole */
import NativeReanimatedModule from './NativeReanimated';
import { Platform } from 'react-native';
import { nativeShouldBeMock, shouldBeUseWeb, isWeb } from './PlatformChecker';
import {
BasicWorkletFunction,
ComplexWorkletFunction,
SharedValue,
AnimationObject,
AnimatableValue,
Timestamp,
} from './commonTypes';
import { Descriptor } from './hook/commonTypes';
import JSReanimated from './js-reanimated/JSReanimated';
if (global._setGlobalConsole === undefined) {
// it can happen when Reanimated plugin wasn't added, but the user uses the only API from version 1
global._setGlobalConsole = () => {
// noop
};
}
export type ReanimatedConsole = Pick<
Console,
'debug' | 'log' | 'warn' | 'info' | 'error'
>;
export type WorkletValue =
| (() => AnimationObject)
| AnimationObject
| AnimatableValue
| Descriptor;
interface WorkletValueSetterContext {
_animation?: AnimationObject | null;
_value?: AnimatableValue | Descriptor;
value?: AnimatableValue;
_setValue?: (val: AnimatableValue | Descriptor) => void;
}
const testWorklet: BasicWorkletFunction<void> = () => {
'worklet';
};
export const checkPluginState: (throwError: boolean) => boolean = (
throwError = true
) => {
if (!testWorklet.__workletHash && !shouldBeUseWeb()) {
if (throwError) {
throw new Error(
"Reanimated 2 failed to create a worklet, maybe you forgot to add Reanimated's babel plugin?"
);
}
return false;
}
return true;
};
export const isConfigured: (throwError?: boolean) => boolean = (
throwError = false
) => {
return checkPluginState(throwError);
};
export const isConfiguredCheck: () => void = () => {
if (!isConfigured(true)) {
throw new Error(
'If you want to use Reanimated 2 then go through our installation steps https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/installation'
);
}
};
function pushFrame(frame: (timestamp: Timestamp) => void): void {
(NativeReanimatedModule as JSReanimated).pushFrame(frame);
}
export function requestFrame(frame: (timestamp: Timestamp) => void): void {
'worklet';
if (NativeReanimatedModule.native) {
requestAnimationFrame(frame);
} else {
pushFrame(frame);
}
}
global._WORKLET = false;
global._log = function (s: string) {
console.log(s);
};
export function runOnUI<A extends any[], R>(
worklet: ComplexWorkletFunction<A, R>
): (...args: A) => void {
return makeShareable(worklet);
}
export function makeShareable<T>(value: T): T {
isConfiguredCheck();
return NativeReanimatedModule.makeShareable(value);
}
export function getViewProp<T>(viewTag: string, propName: string): Promise<T> {
if (global._IS_FABRIC) {
throw new Error(
'[react-native-reanimated] `getViewProp` is not supported on Fabric yet'
);
}
return new Promise((resolve, reject) => {
return NativeReanimatedModule.getViewProp(
viewTag,
propName,
(result: T) => {
if (typeof result === 'string' && result.substr(0, 6) === 'error:') {
reject(result);
} else {
resolve(result);
}
}
);
});
}
let _getTimestamp: () => number;
if (nativeShouldBeMock()) {
_getTimestamp = () => {
return (NativeReanimatedModule as JSReanimated).getTimestamp();
};
} else {
_getTimestamp = () => {
'worklet';
if (_frameTimestamp) {
return _frameTimestamp;
}
if (_eventTimestamp) {
return _eventTimestamp;
}
return _getCurrentTime();
};
}
export function getTimestamp(): number {
'worklet';
if (Platform.OS === 'web') {
return (NativeReanimatedModule as JSReanimated).getTimestamp();
}
return _getTimestamp();
}
function workletValueSetter<T extends WorkletValue>(
this: WorkletValueSetterContext,
value: T
): void {
'worklet';
const previousAnimation = this._animation;
if (previousAnimation) {
previousAnimation.cancelled = true;
this._animation = null;
}
if (
typeof value === 'function' ||
(value !== null &&
typeof value === 'object' &&
(value as AnimationObject).onFrame !== undefined)
) {
const animation: AnimationObject =
typeof value === 'function'
? (value as () => AnimationObject)()
: (value as AnimationObject);
// prevent setting again to the same value
// and triggering the mappers that treat this value as an input
// this happens when the animation's target value(stored in animation.current until animation.onStart is called) is set to the same value as a current one(this._value)
// built in animations that are not higher order(withTiming, withSpring) hold target value in .current
if (this._value === animation.current && !animation.isHigherOrder) {
animation.callback && animation.callback(true);
return;
}
// animated set
const initializeAnimation = (timestamp: number) => {
animation.onStart(animation, this.value, timestamp, previousAnimation);
};
initializeAnimation(getTimestamp());
const step = (timestamp: number) => {
if (animation.cancelled) {
animation.callback && animation.callback(false /* finished */);
return;
}
const finished = animation.onFrame(animation, timestamp);
animation.finished = true;
animation.timestamp = timestamp;
this._value = animation.current;
if (finished) {
animation.callback && animation.callback(true /* finished */);
} else {
requestAnimationFrame(step);
}
};
this._animation = animation;
if (_frameTimestamp) {
// frame
step(_frameTimestamp);
} else {
requestAnimationFrame(step);
}
} else {
// prevent setting again to the same value
// and triggering the mappers that treat this value as an input
if (this._value === value) {
return;
}
this._value = value as Descriptor | AnimatableValue;
}
}
// We cannot use pushFrame
// so we use own implementation for js
function workletValueSetterJS<T extends WorkletValue>(
this: WorkletValueSetterContext,
value: T
): void {
const previousAnimation = this._animation;
if (previousAnimation) {
previousAnimation.cancelled = true;
this._animation = null;
}
if (
typeof value === 'function' ||
(value !== null &&
typeof value === 'object' &&
(value as AnimationObject).onFrame)
) {
// animated set
const animation: AnimationObject =
typeof value === 'function'
? (value as () => AnimationObject)()
: (value as AnimationObject);
let initializeAnimation: ((timestamp: number) => void) | null = (
timestamp: number
) => {
animation.onStart(animation, this.value, timestamp, previousAnimation);
};
const step = (timestamp: number) => {
if (animation.cancelled) {
animation.callback && animation.callback(false /* finished */);
return;
}
if (initializeAnimation) {
initializeAnimation(timestamp);
initializeAnimation = null; // prevent closure from keeping ref to previous animation
}
const finished = animation.onFrame(animation, timestamp);
animation.timestamp = timestamp;
this._setValue && this._setValue(animation.current as AnimatableValue);
if (finished) {
animation.callback && animation.callback(true /* finished */);
} else {
requestFrame(step);
}
};
this._animation = animation;
requestFrame(step);
} else {
this._setValue && this._setValue(value as AnimatableValue | Descriptor);
}
}
export function makeMutable<T>(value: T): SharedValue<T> {
isConfiguredCheck();
return NativeReanimatedModule.makeMutable(value);
}
export function makeRemote<T>(object = {}): T {
isConfiguredCheck();
return NativeReanimatedModule.makeRemote(object);
}
export function startMapper(
mapper: () => void,
inputs: any[] = [],
outputs: any[] = [],
updater: () => void = () => {
// noop
},
viewDescriptors: Descriptor[] | SharedValue<Descriptor[]> = []
): number {
isConfiguredCheck();
return NativeReanimatedModule.startMapper(
mapper,
inputs,
outputs,
updater,
viewDescriptors
);
}
export function stopMapper(mapperId: number): void {
NativeReanimatedModule.stopMapper(mapperId);
}
export interface RunOnJSFunction<A extends any[], R> {
__callAsync?: (...args: A) => void;
(...args: A): R;
}
export function runOnJS<A extends any[], R>(
fun: RunOnJSFunction<A, R>
): () => void {
'worklet';
if (!_WORKLET) {
return fun;
}
if (!fun.__callAsync) {
throw new Error(
"Attempting to call runOnJS with an object that is not a host function. Using runOnJS is only possible with methods that are defined on the main React-Native Javascript thread and that aren't marked as worklets"
);
} else {
return fun.__callAsync;
}
}
NativeReanimatedModule.installCoreFunctions(
NativeReanimatedModule.native
? (workletValueSetter as <T>(value: T) => void)
: (workletValueSetterJS as <T>(value: T) => void)
);
if (!isWeb() && isConfigured()) {
const capturableConsole = console;
runOnUI(() => {
'worklet';
const console = {
debug: runOnJS(capturableConsole.debug),
log: runOnJS(capturableConsole.log),
warn: runOnJS(capturableConsole.warn),
error: runOnJS(capturableConsole.error),
info: runOnJS(capturableConsole.info),
};
_setGlobalConsole(console);
})();
}
type FeaturesConfig = {
enableLayoutAnimations: boolean;
setByUser: boolean;
};
let featuresConfig: FeaturesConfig = {
enableLayoutAnimations: false,
setByUser: false,
};
export function enableLayoutAnimations(
flag: boolean,
isCallByUser = true
): void {
if (isCallByUser) {
featuresConfig = {
enableLayoutAnimations: flag,
setByUser: true,
};
NativeReanimatedModule.enableLayoutAnimations(flag);
} else if (
!featuresConfig.setByUser &&
featuresConfig.enableLayoutAnimations !== flag
) {
featuresConfig.enableLayoutAnimations = flag;
NativeReanimatedModule.enableLayoutAnimations(flag);
}
}
export function configureProps(uiProps: string[], nativeProps: string[]): void {
if (!nativeShouldBeMock()) {
NativeReanimatedModule.configureProps(uiProps, nativeProps);
}
}
export function jestResetJsReanimatedModule() {
(NativeReanimatedModule as JSReanimated).jestResetModule();
}