react-native-reanimated 3.0.0-rc.7 → 3.0.0-rc.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (177) hide show
  1. package/Common/cpp/AnimatedSensor/AnimatedSensorModule.cpp +32 -37
  2. package/Common/cpp/AnimatedSensor/AnimatedSensorModule.h +4 -3
  3. package/Common/cpp/LayoutAnimations/LayoutAnimationsManager.cpp +76 -0
  4. package/Common/cpp/LayoutAnimations/LayoutAnimationsManager.h +41 -0
  5. package/Common/cpp/NativeModules/NativeReanimatedModule.cpp +161 -165
  6. package/Common/cpp/NativeModules/NativeReanimatedModule.h +36 -34
  7. package/Common/cpp/NativeModules/NativeReanimatedModuleSpec.cpp +22 -34
  8. package/Common/cpp/NativeModules/NativeReanimatedModuleSpec.h +13 -14
  9. package/Common/cpp/SharedItems/RuntimeManager.h +1 -34
  10. package/Common/cpp/SharedItems/Shareables.cpp +142 -0
  11. package/Common/cpp/SharedItems/Shareables.h +455 -0
  12. package/Common/cpp/Tools/PlatformDepMethodsHolder.h +6 -0
  13. package/Common/cpp/Tools/RuntimeDecorator.cpp +54 -26
  14. package/Common/cpp/Tools/RuntimeDecorator.h +11 -2
  15. package/Common/cpp/Tools/Scheduler.cpp +10 -0
  16. package/Common/cpp/Tools/SingleInstanceChecker.h +1 -1
  17. package/android/src/main/cpp/LayoutAnimations.cpp +6 -16
  18. package/android/src/main/cpp/LayoutAnimations.h +4 -4
  19. package/android/src/main/cpp/NativeProxy.cpp +50 -63
  20. package/ios/native/NativeProxy.mm +26 -42
  21. package/lib/commonjs/createAnimatedComponent.js +7 -11
  22. package/lib/commonjs/createAnimatedComponent.js.map +1 -1
  23. package/lib/commonjs/reanimated2/NativeReanimated/NativeReanimated.js +24 -25
  24. package/lib/commonjs/reanimated2/NativeReanimated/NativeReanimated.js.map +1 -1
  25. package/lib/commonjs/reanimated2/ViewDescriptorsSet.js +27 -55
  26. package/lib/commonjs/reanimated2/ViewDescriptorsSet.js.map +1 -1
  27. package/lib/commonjs/reanimated2/WorkletEventHandler.js +5 -3
  28. package/lib/commonjs/reanimated2/WorkletEventHandler.js.map +1 -1
  29. package/lib/commonjs/reanimated2/animation/util.js.map +1 -1
  30. package/lib/commonjs/reanimated2/commonTypes.js +7 -0
  31. package/lib/commonjs/reanimated2/commonTypes.js.map +1 -1
  32. package/lib/commonjs/reanimated2/core.js +111 -209
  33. package/lib/commonjs/reanimated2/core.js.map +1 -1
  34. package/lib/commonjs/reanimated2/globals.d.js.map +1 -1
  35. package/lib/commonjs/reanimated2/hook/useAnimatedKeyboard.js +15 -9
  36. package/lib/commonjs/reanimated2/hook/useAnimatedKeyboard.js.map +1 -1
  37. package/lib/commonjs/reanimated2/hook/useAnimatedRef.js +18 -8
  38. package/lib/commonjs/reanimated2/hook/useAnimatedRef.js.map +1 -1
  39. package/lib/commonjs/reanimated2/hook/useAnimatedSensor.js +27 -36
  40. package/lib/commonjs/reanimated2/hook/useAnimatedSensor.js.map +1 -1
  41. package/lib/commonjs/reanimated2/hook/useAnimatedStyle.js +24 -50
  42. package/lib/commonjs/reanimated2/hook/useAnimatedStyle.js.map +1 -1
  43. package/lib/commonjs/reanimated2/hook/useSharedValue.js +3 -2
  44. package/lib/commonjs/reanimated2/hook/useSharedValue.js.map +1 -1
  45. package/lib/commonjs/reanimated2/hook/utils.js.map +1 -1
  46. package/lib/commonjs/reanimated2/interpolateColor.js.map +1 -1
  47. package/lib/commonjs/reanimated2/jestUtils.js +8 -33
  48. package/lib/commonjs/reanimated2/jestUtils.js.map +1 -1
  49. package/lib/commonjs/reanimated2/js-reanimated/JSReanimated.js +16 -100
  50. package/lib/commonjs/reanimated2/js-reanimated/JSReanimated.js.map +1 -1
  51. package/lib/commonjs/reanimated2/js-reanimated/index.js +5 -0
  52. package/lib/commonjs/reanimated2/js-reanimated/index.js.map +1 -1
  53. package/lib/commonjs/reanimated2/layoutReanimation/animationBuilder/commonTypes.js.map +1 -1
  54. package/lib/commonjs/reanimated2/layoutReanimation/animationBuilder/index.js.map +1 -1
  55. package/lib/commonjs/reanimated2/layoutReanimation/animationsManager.js +91 -0
  56. package/lib/commonjs/reanimated2/layoutReanimation/animationsManager.js.map +1 -0
  57. package/lib/commonjs/reanimated2/layoutReanimation/index.js +1 -1
  58. package/lib/commonjs/reanimated2/layoutReanimation/index.js.map +1 -1
  59. package/lib/commonjs/reanimated2/mappers.js +186 -0
  60. package/lib/commonjs/reanimated2/mappers.js.map +1 -0
  61. package/lib/commonjs/reanimated2/mutables.js +173 -0
  62. package/lib/commonjs/reanimated2/mutables.js.map +1 -0
  63. package/lib/commonjs/reanimated2/shareables.js +153 -0
  64. package/lib/commonjs/reanimated2/shareables.js.map +1 -0
  65. package/lib/commonjs/reanimated2/threads.js +58 -0
  66. package/lib/commonjs/reanimated2/threads.js.map +1 -0
  67. package/lib/commonjs/reanimated2/time.js +55 -0
  68. package/lib/commonjs/reanimated2/time.js.map +1 -0
  69. package/lib/commonjs/reanimated2/valueSetter.js +81 -0
  70. package/lib/commonjs/reanimated2/valueSetter.js.map +1 -0
  71. package/lib/module/createAnimatedComponent.js +7 -12
  72. package/lib/module/createAnimatedComponent.js.map +1 -1
  73. package/lib/module/reanimated2/NativeReanimated/NativeReanimated.js +24 -25
  74. package/lib/module/reanimated2/NativeReanimated/NativeReanimated.js.map +1 -1
  75. package/lib/module/reanimated2/ViewDescriptorsSet.js +27 -53
  76. package/lib/module/reanimated2/ViewDescriptorsSet.js.map +1 -1
  77. package/lib/module/reanimated2/WorkletEventHandler.js +4 -3
  78. package/lib/module/reanimated2/WorkletEventHandler.js.map +1 -1
  79. package/lib/module/reanimated2/animation/util.js.map +1 -1
  80. package/lib/module/reanimated2/commonTypes.js +7 -0
  81. package/lib/module/reanimated2/commonTypes.js.map +1 -1
  82. package/lib/module/reanimated2/core.js +66 -188
  83. package/lib/module/reanimated2/core.js.map +1 -1
  84. package/lib/module/reanimated2/globals.d.js.map +1 -1
  85. package/lib/module/reanimated2/hook/useAnimatedKeyboard.js +16 -6
  86. package/lib/module/reanimated2/hook/useAnimatedKeyboard.js.map +1 -1
  87. package/lib/module/reanimated2/hook/useAnimatedRef.js +17 -8
  88. package/lib/module/reanimated2/hook/useAnimatedRef.js.map +1 -1
  89. package/lib/module/reanimated2/hook/useAnimatedSensor.js +29 -34
  90. package/lib/module/reanimated2/hook/useAnimatedSensor.js.map +1 -1
  91. package/lib/module/reanimated2/hook/useAnimatedStyle.js +26 -52
  92. package/lib/module/reanimated2/hook/useAnimatedStyle.js.map +1 -1
  93. package/lib/module/reanimated2/hook/useSharedValue.js +3 -2
  94. package/lib/module/reanimated2/hook/useSharedValue.js.map +1 -1
  95. package/lib/module/reanimated2/hook/utils.js.map +1 -1
  96. package/lib/module/reanimated2/interpolateColor.js.map +1 -1
  97. package/lib/module/reanimated2/jestUtils.js +8 -31
  98. package/lib/module/reanimated2/jestUtils.js.map +1 -1
  99. package/lib/module/reanimated2/js-reanimated/JSReanimated.js +16 -95
  100. package/lib/module/reanimated2/js-reanimated/JSReanimated.js.map +1 -1
  101. package/lib/module/reanimated2/js-reanimated/index.js +5 -0
  102. package/lib/module/reanimated2/js-reanimated/index.js.map +1 -1
  103. package/lib/module/reanimated2/layoutReanimation/animationBuilder/commonTypes.js.map +1 -1
  104. package/lib/module/reanimated2/layoutReanimation/animationBuilder/index.js.map +1 -1
  105. package/lib/module/reanimated2/layoutReanimation/animationsManager.js +84 -0
  106. package/lib/module/reanimated2/layoutReanimation/animationsManager.js.map +1 -0
  107. package/lib/module/reanimated2/layoutReanimation/index.js +1 -1
  108. package/lib/module/reanimated2/layoutReanimation/index.js.map +1 -1
  109. package/lib/module/reanimated2/mappers.js +173 -0
  110. package/lib/module/reanimated2/mappers.js.map +1 -0
  111. package/lib/module/reanimated2/mutables.js +149 -0
  112. package/lib/module/reanimated2/mutables.js.map +1 -0
  113. package/lib/module/reanimated2/shareables.js +133 -0
  114. package/lib/module/reanimated2/shareables.js.map +1 -0
  115. package/lib/module/reanimated2/threads.js +45 -0
  116. package/lib/module/reanimated2/threads.js.map +1 -0
  117. package/lib/module/reanimated2/time.js +37 -0
  118. package/lib/module/reanimated2/time.js.map +1 -0
  119. package/lib/module/reanimated2/valueSetter.js +66 -0
  120. package/lib/module/reanimated2/valueSetter.js.map +1 -0
  121. package/package.json +1 -1
  122. package/plugin.js +31 -15
  123. package/src/createAnimatedComponent.tsx +8 -24
  124. package/src/reanimated2/NativeReanimated/NativeReanimated.ts +34 -53
  125. package/src/reanimated2/ViewDescriptorsSet.ts +30 -69
  126. package/src/reanimated2/WorkletEventHandler.ts +4 -11
  127. package/src/reanimated2/animation/util.ts +1 -0
  128. package/src/reanimated2/commonTypes.ts +29 -4
  129. package/src/reanimated2/core.ts +103 -230
  130. package/src/reanimated2/globals.d.ts +26 -26
  131. package/src/reanimated2/hook/useAnimatedKeyboard.ts +18 -9
  132. package/src/reanimated2/hook/useAnimatedRef.ts +27 -9
  133. package/src/reanimated2/hook/useAnimatedSensor.ts +33 -31
  134. package/src/reanimated2/hook/useAnimatedStyle.ts +26 -64
  135. package/src/reanimated2/hook/useSharedValue.ts +6 -3
  136. package/src/reanimated2/hook/utils.ts +1 -1
  137. package/src/reanimated2/interpolateColor.ts +2 -2
  138. package/src/reanimated2/jestUtils.ts +14 -29
  139. package/src/reanimated2/js-reanimated/JSReanimated.ts +19 -93
  140. package/src/reanimated2/js-reanimated/index.ts +4 -0
  141. package/src/reanimated2/layoutReanimation/animationBuilder/commonTypes.ts +7 -0
  142. package/src/reanimated2/layoutReanimation/animationBuilder/index.ts +1 -0
  143. package/src/reanimated2/layoutReanimation/animationsManager.ts +94 -0
  144. package/src/reanimated2/layoutReanimation/index.ts +1 -1
  145. package/src/reanimated2/mappers.ts +179 -0
  146. package/src/reanimated2/mutables.ts +151 -0
  147. package/src/reanimated2/shareables.ts +121 -0
  148. package/src/reanimated2/threads.ts +46 -0
  149. package/src/reanimated2/time.ts +30 -0
  150. package/src/reanimated2/valueSetter.ts +64 -0
  151. package/Common/cpp/LayoutAnimations/LayoutAnimationsProxy.cpp +0 -127
  152. package/Common/cpp/LayoutAnimations/LayoutAnimationsProxy.h +0 -56
  153. package/Common/cpp/Registries/MapperRegistry.cpp +0 -147
  154. package/Common/cpp/Registries/MapperRegistry.h +0 -29
  155. package/Common/cpp/Registries/WorkletsCache.cpp +0 -45
  156. package/Common/cpp/Registries/WorkletsCache.h +0 -24
  157. package/Common/cpp/SharedItems/FrozenObject.cpp +0 -35
  158. package/Common/cpp/SharedItems/FrozenObject.h +0 -36
  159. package/Common/cpp/SharedItems/HostFunctionHandler.h +0 -28
  160. package/Common/cpp/SharedItems/MutableValue.cpp +0 -134
  161. package/Common/cpp/SharedItems/MutableValue.h +0 -54
  162. package/Common/cpp/SharedItems/MutableValueSetterProxy.cpp +0 -47
  163. package/Common/cpp/SharedItems/MutableValueSetterProxy.h +0 -25
  164. package/Common/cpp/SharedItems/RemoteObject.cpp +0 -45
  165. package/Common/cpp/SharedItems/RemoteObject.h +0 -35
  166. package/Common/cpp/SharedItems/ShareableValue.cpp +0 -529
  167. package/Common/cpp/SharedItems/ShareableValue.h +0 -63
  168. package/Common/cpp/SharedItems/ValueWrapper.h +0 -183
  169. package/Common/cpp/Tools/JSIStoreValueUser.cpp +0 -55
  170. package/Common/cpp/Tools/JSIStoreValueUser.h +0 -37
  171. package/Common/cpp/Tools/Mapper.cpp +0 -86
  172. package/Common/cpp/Tools/Mapper.h +0 -51
  173. package/lib/commonjs/reanimated2/layoutReanimation/LayoutAnimationRepository.js +0 -58
  174. package/lib/commonjs/reanimated2/layoutReanimation/LayoutAnimationRepository.js.map +0 -1
  175. package/lib/module/reanimated2/layoutReanimation/LayoutAnimationRepository.js +0 -52
  176. package/lib/module/reanimated2/layoutReanimation/LayoutAnimationRepository.js.map +0 -1
  177. package/src/reanimated2/layoutReanimation/LayoutAnimationRepository.ts +0 -49
@@ -1,7 +1,6 @@
1
1
  import { useEffect, useRef } from 'react';
2
- import { makeMutable } from '../core';
3
- import NativeReanimated from '../NativeReanimated';
4
- import { SensorValue3D, SensorValueRotation } from '../commonTypes';
2
+ import { makeMutable, registerSensor, unregisterSensor } from '../core';
3
+ import { SharedValue, Value3D, ValueRotation } from '../commonTypes';
5
4
 
6
5
  export enum SensorType {
7
6
  ACCELEROMETER = 1,
@@ -16,18 +15,40 @@ export type SensorConfig = {
16
15
  };
17
16
 
18
17
  export type AnimatedSensor = {
19
- sensor: SensorValue3D | SensorValueRotation | null;
18
+ sensor: SharedValue<Value3D | ValueRotation>;
20
19
  unregister: () => void;
21
20
  isAvailable: boolean;
22
21
  config: SensorConfig;
23
22
  };
24
23
 
24
+ function initSensorData(
25
+ sensorType: SensorType
26
+ ): SharedValue<Value3D | ValueRotation> {
27
+ if (sensorType === SensorType.ROTATION) {
28
+ return makeMutable<Value3D | ValueRotation>({
29
+ qw: 0,
30
+ qx: 0,
31
+ qy: 0,
32
+ qz: 0,
33
+ yaw: 0,
34
+ pitch: 0,
35
+ roll: 0,
36
+ });
37
+ } else {
38
+ return makeMutable<Value3D | ValueRotation>({
39
+ x: 0,
40
+ y: 0,
41
+ z: 0,
42
+ });
43
+ }
44
+ }
45
+
25
46
  export function useAnimatedSensor(
26
47
  sensorType: SensorType,
27
48
  userConfig?: SensorConfig
28
49
  ): AnimatedSensor {
29
50
  const ref = useRef<AnimatedSensor>({
30
- sensor: null,
51
+ sensor: initSensorData(sensorType),
31
52
  unregister: () => {
32
53
  // NOOP
33
54
  },
@@ -37,40 +58,21 @@ export function useAnimatedSensor(
37
58
  },
38
59
  });
39
60
 
40
- if (ref.current.sensor === null) {
41
- ref.current.config = { interval: 'auto', ...userConfig };
42
- let sensorData;
43
- if (sensorType === SensorType.ROTATION) {
44
- sensorData = {
45
- qw: 0,
46
- qx: 0,
47
- qy: 0,
48
- qz: 0,
49
- yaw: 0,
50
- pitch: 0,
51
- roll: 0,
52
- };
53
- } else {
54
- sensorData = {
55
- x: 0,
56
- y: 0,
57
- z: 0,
58
- };
59
- }
60
- ref.current.sensor = makeMutable(sensorData) as any;
61
- }
62
-
63
61
  useEffect(() => {
64
62
  ref.current.config = { interval: 'auto', ...userConfig };
65
- const id = NativeReanimated.registerSensor(
63
+ const sensorData = ref.current.sensor!;
64
+ const id = registerSensor(
66
65
  sensorType,
67
66
  ref.current.config.interval === 'auto' ? -1 : ref.current.config.interval,
68
- ref.current.sensor as any
67
+ (data) => {
68
+ 'worklet';
69
+ sensorData.value = data;
70
+ }
69
71
  );
70
72
 
71
73
  if (id !== -1) {
72
74
  // if sensor is available
73
- ref.current.unregister = () => NativeReanimated.unregisterSensor(id);
75
+ ref.current.unregister = () => unregisterSensor(id);
74
76
  ref.current.isAvailable = true;
75
77
  } else {
76
78
  // if sensor is unavailable
@@ -1,23 +1,14 @@
1
1
  /* global _frameTimestamp */
2
2
  import { MutableRefObject, useEffect, useRef } from 'react';
3
3
 
4
- import {
5
- startMapper,
6
- stopMapper,
7
- makeRemote,
8
- requestFrame,
9
- getTimestamp,
10
- makeMutable,
11
- } from '../core';
4
+ import { startMapper, stopMapper, makeRemote, getTimestamp } from '../core';
12
5
  import updateProps, { updatePropsJestWrapper } from '../UpdateProps';
13
6
  import { initialUpdaterRun } from '../animation';
14
7
  import NativeReanimatedModule from '../NativeReanimated';
15
8
  import { useSharedValue } from './useSharedValue';
16
9
  import {
17
10
  buildWorkletsHash,
18
- canApplyOptimalisation,
19
11
  getStyleWithoutAnimations,
20
- hasColorProps,
21
12
  isAnimated,
22
13
  parseColors,
23
14
  styleDiff,
@@ -61,7 +52,7 @@ interface AnimationRef {
61
52
  updater: () => AnimatedStyle;
62
53
  };
63
54
  remoteState: AnimatedState;
64
- sharableViewDescriptors: SharedValue<Descriptor[]>;
55
+ viewDescriptors: ViewDescriptorsSet;
65
56
  }
66
57
 
67
58
  function prepareAnimation(
@@ -71,13 +62,13 @@ function prepareAnimation(
71
62
  ): void {
72
63
  'worklet';
73
64
  if (Array.isArray(animatedProp)) {
74
- animatedProp.forEach((prop, index) =>
65
+ animatedProp.forEach((prop, index) => {
75
66
  prepareAnimation(
76
67
  prop,
77
68
  lastAnimation && lastAnimation[index],
78
69
  lastValue && lastValue[index]
79
- )
80
- );
70
+ );
71
+ });
81
72
  // return animatedProp;
82
73
  }
83
74
  if (typeof animatedProp === 'object' && animatedProp.onFrame) {
@@ -207,8 +198,9 @@ function styleUpdater(
207
198
  }
208
199
 
209
200
  if (hasAnimations) {
210
- const frame = (timestamp: Timestamp) => {
201
+ const frame = (_timestamp?: Timestamp) => {
211
202
  const { animations, last, isAnimationCancelled } = state;
203
+ const timestamp = _timestamp ?? getTimestamp();
212
204
  if (isAnimationCancelled) {
213
205
  state.isAnimationRunning = false;
214
206
  return;
@@ -237,7 +229,7 @@ function styleUpdater(
237
229
  }
238
230
 
239
231
  if (!allFinished) {
240
- requestFrame(frame);
232
+ requestAnimationFrame(frame);
241
233
  } else {
242
234
  state.isAnimationRunning = false;
243
235
  }
@@ -250,7 +242,7 @@ function styleUpdater(
250
242
  if (_frameTimestamp) {
251
243
  frame(_frameTimestamp);
252
244
  } else {
253
- requestFrame(frame);
245
+ requestAnimationFrame(frame);
254
246
  }
255
247
  }
256
248
  state.last = Object.assign({}, oldValues, newValues);
@@ -301,8 +293,9 @@ function jestStyleUpdater(
301
293
  }
302
294
  });
303
295
 
304
- function frame(timestamp: Timestamp) {
296
+ function frame(_timestamp?: Timestamp) {
305
297
  const { animations, last, isAnimationCancelled } = state;
298
+ const timestamp = _timestamp ?? getTimestamp();
306
299
  if (isAnimationCancelled) {
307
300
  state.isAnimationRunning = false;
308
301
  return;
@@ -337,7 +330,7 @@ function jestStyleUpdater(
337
330
  }
338
331
 
339
332
  if (!allFinished) {
340
- requestFrame(frame);
333
+ requestAnimationFrame(frame);
341
334
  } else {
342
335
  state.isAnimationRunning = false;
343
336
  }
@@ -351,7 +344,7 @@ function jestStyleUpdater(
351
344
  if (_frameTimestamp) {
352
345
  frame(_frameTimestamp);
353
346
  } else {
354
- requestFrame(frame);
347
+ requestAnimationFrame(frame);
355
348
  }
356
349
  }
357
350
  } else {
@@ -407,7 +400,6 @@ export function useAnimatedStyle<T extends AnimatedStyle>(
407
400
  adapters?: AdapterWorkletFunction | AdapterWorkletFunction[]
408
401
  ): AnimatedStyleResult {
409
402
  const viewsRef: ViewRefSet<any> = makeViewsRefSet();
410
- const viewDescriptors: ViewDescriptorsSet = makeViewDescriptorsSet();
411
403
  const initRef = useRef<AnimationRef>();
412
404
  const inputs = Object.values(updater._closure ?? {});
413
405
  const adaptersArray: AdapterWorkletFunction[] = adapters
@@ -437,24 +429,26 @@ export function useAnimatedStyle<T extends AnimatedStyle>(
437
429
  value: initialStyle,
438
430
  updater: updater,
439
431
  },
440
- remoteState: makeRemote({ last: initialStyle }),
441
- sharableViewDescriptors: makeMutable([]),
432
+ remoteState: makeRemote<AnimatedState>({
433
+ last: initialStyle,
434
+ animations: {},
435
+ isAnimationCancelled: false,
436
+ isAnimationRunning: false,
437
+ }),
438
+ viewDescriptors: makeViewDescriptorsSet(),
442
439
  };
443
- viewDescriptors.rebuildsharableViewDescriptors(
444
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
445
- initRef.current!.sharableViewDescriptors
446
- );
447
440
  }
448
- dependencies.push(initRef.current?.sharableViewDescriptors.value);
449
441
 
450
442
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
451
- const { initial, remoteState, sharableViewDescriptors } = initRef.current!;
443
+ const { initial, remoteState, viewDescriptors } = initRef.current!;
444
+ const sharableViewDescriptors = viewDescriptors.sharableViewDescriptors;
452
445
  const maybeViewRef = NativeReanimatedModule.native ? undefined : viewsRef;
453
446
 
447
+ dependencies.push(sharableViewDescriptors);
448
+
454
449
  useEffect(() => {
455
450
  let fun;
456
451
  let updaterFn = updater as BasicWorkletFunctionOptional<T>;
457
- let optimalization = updater.__optimalization;
458
452
  if (adapters) {
459
453
  updaterFn = () => {
460
454
  'worklet';
@@ -466,29 +460,7 @@ export function useAnimatedStyle<T extends AnimatedStyle>(
466
460
  };
467
461
  }
468
462
 
469
- if (canApplyOptimalisation(updaterFn) && !shouldBeUseWeb()) {
470
- if (hasColorProps(updaterFn())) {
471
- updaterFn = () => {
472
- 'worklet';
473
- const newValues = updaterFn();
474
- const oldValues = remoteState.last;
475
- const diff = styleDiff<T>(oldValues, newValues);
476
- remoteState.last = Object.assign({}, oldValues, newValues);
477
- parseColors(diff);
478
- return diff;
479
- };
480
- } else {
481
- updaterFn = () => {
482
- 'worklet';
483
- const newValues = updaterFn();
484
- const oldValues = remoteState.last;
485
- const diff = styleDiff<T>(oldValues, newValues);
486
- remoteState.last = Object.assign({}, oldValues, newValues);
487
- return diff;
488
- };
489
- }
490
- } else if (!shouldBeUseWeb()) {
491
- optimalization = 0;
463
+ if (!shouldBeUseWeb()) {
492
464
  updaterFn = () => {
493
465
  'worklet';
494
466
  const style = updaterFn();
@@ -496,9 +468,6 @@ export function useAnimatedStyle<T extends AnimatedStyle>(
496
468
  return style;
497
469
  };
498
470
  }
499
- if (typeof updater.__optimalization !== undefined) {
500
- updaterFn.__optimalization = optimalization;
501
- }
502
471
 
503
472
  if (isJest()) {
504
473
  fun = () => {
@@ -525,14 +494,7 @@ export function useAnimatedStyle<T extends AnimatedStyle>(
525
494
  );
526
495
  };
527
496
  }
528
- const mapperId = startMapper(
529
- fun,
530
- inputs,
531
- [],
532
- updaterFn,
533
- // TODO fix this
534
- sharableViewDescriptors
535
- );
497
+ const mapperId = startMapper(fun, inputs);
536
498
  return () => {
537
499
  stopMapper(mapperId);
538
500
  };
@@ -3,11 +3,14 @@ import { cancelAnimation } from '../animation';
3
3
  import { SharedValue } from '../commonTypes';
4
4
  import { makeMutable } from '../core';
5
5
 
6
- export function useSharedValue<T>(init: T): SharedValue<T> {
7
- const ref = useRef<SharedValue<T>>(makeMutable(init));
6
+ export function useSharedValue<T>(
7
+ init: T,
8
+ oneWayReadsOnly = false
9
+ ): SharedValue<T> {
10
+ const ref = useRef<SharedValue<T>>(makeMutable(init, oneWayReadsOnly));
8
11
 
9
12
  if (ref.current === null) {
10
- ref.current = makeMutable(init);
13
+ ref.current = makeMutable(init, oneWayReadsOnly);
11
14
  }
12
15
 
13
16
  useEffect(() => {
@@ -51,7 +51,7 @@ export function useHandler<T, TContext extends Context>(
51
51
  const initRef = useRef<ContextWithDependencies<TContext> | null>(null);
52
52
  if (initRef.current === null) {
53
53
  initRef.current = {
54
- context: makeRemote({}),
54
+ context: makeRemote<TContext>({} as TContext),
55
55
  savedDependencies: [],
56
56
  };
57
57
  }
@@ -141,11 +141,11 @@ export function useInterpolateConfig(
141
141
  outputRange: readonly (string | number)[],
142
142
  colorSpace = ColorSpace.RGB
143
143
  ): SharedValue<InterpolateConfig> {
144
- return useSharedValue({
144
+ return useSharedValue<InterpolateConfig>({
145
145
  inputRange,
146
146
  outputRange,
147
147
  colorSpace,
148
- cache: makeMutable(null),
148
+ cache: makeMutable<InterpolateRGB | InterpolateHSV | null>(null),
149
149
  });
150
150
  }
151
151
 
@@ -1,7 +1,5 @@
1
1
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
2
2
  // @ts-nocheck
3
- import { jestResetJsReanimatedModule } from './core';
4
-
5
3
  declare global {
6
4
  // eslint-disable-next-line @typescript-eslint/no-namespace
7
5
  namespace jest {
@@ -139,52 +137,39 @@ const compareStyle = (received, expectedStyle, config) => {
139
137
  };
140
138
 
141
139
  let frameTime = 1000 / config.fps;
142
- let requestAnimationFrameCopy;
143
- let currentTimestamp = 0;
144
-
145
- const requestAnimationFrame = (callback) => {
146
- setTimeout(callback, frameTime);
147
- };
148
140
 
149
141
  const beforeTest = () => {
150
- jestResetJsReanimatedModule();
151
- requestAnimationFrameCopy = global.requestAnimationFrame;
152
- global.requestAnimationFrame = requestAnimationFrame;
153
- global.ReanimatedDataMock = {
154
- now: () => currentTimestamp,
155
- };
156
- currentTimestamp = 0;
157
142
  jest.useFakeTimers();
158
143
  };
159
144
 
160
145
  const afterTest = () => {
146
+ jest.runOnlyPendingTimers();
161
147
  jest.useRealTimers();
162
- global.requestAnimationFrame = requestAnimationFrameCopy;
163
- };
164
-
165
- const tickTravel = () => {
166
- currentTimestamp += frameTime;
167
- jest.advanceTimersByTime(frameTime);
168
148
  };
169
149
 
170
150
  export const withReanimatedTimer = (animationTest) => {
151
+ console.warn(
152
+ 'This method is deprecated, you shoulddefine your own before and after test hooks to enable jest.useFakeTimers(). Check out the documentation for details on testing'
153
+ );
171
154
  beforeTest();
172
155
  animationTest();
173
156
  afterTest();
174
157
  };
175
158
 
176
159
  export const advanceAnimationByTime = (time = frameTime) => {
177
- for (let i = 0; i <= Math.ceil(time / frameTime); i++) {
178
- tickTravel();
179
- }
180
- jest.advanceTimersByTime(frameTime);
160
+ console.warn(
161
+ 'This method is deprecated, use jest.advanceTimersByTime directly'
162
+ );
163
+ jest.advanceTimersByTime(time);
164
+ jest.runOnlyPendingTimers();
181
165
  };
182
166
 
183
167
  export const advanceAnimationByFrame = (count) => {
184
- for (let i = 0; i <= count; i++) {
185
- tickTravel();
186
- }
187
- jest.advanceTimersByTime(frameTime);
168
+ console.warn(
169
+ 'This method is deprecated, use jest.advanceTimersByTime directly'
170
+ );
171
+ jest.advanceTimersByTime(count * frameTime);
172
+ jest.runOnlyPendingTimers();
188
173
  };
189
174
 
190
175
  export const setUpTests = (userConfig = {}) => {
@@ -1,100 +1,39 @@
1
- import MapperRegistry from './MapperRegistry';
2
- import Mapper from './Mapper';
3
- import MutableValue from './MutableValue';
4
1
  import { NativeReanimated } from '../NativeReanimated/NativeReanimated';
5
- import {
6
- Timestamp,
7
- NestedObjectValues,
8
- AnimatedKeyboardInfo,
9
- } from '../commonTypes';
2
+ import { ShareableRef } from '../commonTypes';
10
3
  import { isJest } from '../PlatformChecker';
11
4
 
12
5
  export default class JSReanimated extends NativeReanimated {
13
- _valueSetter?: <T>(value: T) => void = undefined;
14
-
15
- _renderRequested = false;
16
- _mapperRegistry: MapperRegistry<any> = new MapperRegistry(this);
17
- _frames: ((timestamp: Timestamp) => void)[] = [];
18
- timeProvider: { now: () => number };
6
+ _valueUnpacker?: <T>(value: T) => void = undefined;
19
7
 
20
8
  constructor() {
21
9
  super(false);
22
10
  if (isJest()) {
23
- this.timeProvider = { now: () => global.ReanimatedDataMock.now() };
11
+ // on node < 16 jest have problems mocking performance.now method which
12
+ // results in the tests failing, since date precision isn't that important
13
+ // for tests, we use Date.now instead
14
+ this.getTimestamp = () => Date.now();
24
15
  } else {
25
- this.timeProvider = { now: () => window.performance.now() };
26
- }
27
- }
28
-
29
- pushFrame(frame: (timestamp: Timestamp) => void): void {
30
- this._frames.push(frame);
31
- this.maybeRequestRender();
32
- }
33
-
34
- getTimestamp(): number {
35
- return this.timeProvider.now();
36
- }
37
-
38
- maybeRequestRender(): void {
39
- if (!this._renderRequested) {
40
- this._renderRequested = true;
41
-
42
- requestAnimationFrame((_timestampMs) => {
43
- this._renderRequested = false;
44
-
45
- this._onRender(this.getTimestamp());
46
- });
47
- }
48
- }
49
-
50
- _onRender(timestampMs: number): void {
51
- this._mapperRegistry.execute();
52
-
53
- const frames = [...this._frames];
54
- this._frames = [];
55
-
56
- for (let i = 0, len = frames.length; i < len; ++i) {
57
- frames[i](timestampMs);
58
- }
59
-
60
- if (this._mapperRegistry.needRunOnRender) {
61
- this._mapperRegistry.execute();
16
+ this.getTimestamp = () => window.performance.now();
62
17
  }
63
18
  }
64
19
 
65
- installCoreFunctions(valueSetter: <T>(value: T) => void): void {
66
- this._valueSetter = valueSetter;
67
- }
68
-
69
- makeShareable<T>(value: T): T {
70
- return value;
71
- }
72
-
73
- makeMutable<T>(value: T): MutableValue<T> {
74
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
75
- return new MutableValue(value, this._valueSetter!);
20
+ makeShareableClone<T>(value: T): ShareableRef<T> {
21
+ return { __hostObjectShareableJSRef: value };
76
22
  }
77
23
 
78
- makeRemote<T>(object = {}): T {
79
- return object as T;
24
+ installCoreFunctions(valueUnpacker: <T>(value: T) => T): void {
25
+ this._valueUnpacker = valueUnpacker;
80
26
  }
81
27
 
82
- startMapper(
83
- mapper: () => void,
84
- inputs: NestedObjectValues<MutableValue<unknown>>[] = [],
85
- outputs: NestedObjectValues<MutableValue<unknown>>[] = []
86
- ): number {
87
- const instance = new Mapper(this, mapper, inputs, outputs);
88
- const mapperId = this._mapperRegistry.startMapper(instance);
89
- this.maybeRequestRender();
90
- return mapperId;
28
+ scheduleOnUI<T>(worklet: ShareableRef<T>) {
29
+ // @ts-ignore web implementation has still not been updated after the rewrite, this will be addressed once the web implementation updates are ready
30
+ requestAnimationFrame(worklet);
91
31
  }
92
32
 
93
- stopMapper(mapperId: number): void {
94
- this._mapperRegistry.stopMapper(mapperId);
95
- }
96
-
97
- registerEventHandler<T>(_: string, __: (event: T) => void): string {
33
+ registerEventHandler<T>(
34
+ _eventHash: string,
35
+ _eventHandler: ShareableRef<T>
36
+ ): string {
98
37
  // noop
99
38
  return '';
100
39
  }
@@ -118,20 +57,7 @@ export default class JSReanimated extends NativeReanimated {
118
57
  // noop
119
58
  }
120
59
 
121
- jestResetModule() {
122
- if (isJest()) {
123
- /**
124
- * If someone used timers to stop animation before the end,
125
- * then _renderRequested was set as true
126
- * and any new update from another test wasn't applied.
127
- */
128
- this._renderRequested = false;
129
- } else {
130
- throw Error('This method can be only use in Jest testing.');
131
- }
132
- }
133
-
134
- subscribeForKeyboardEvents(_: AnimatedKeyboardInfo): number {
60
+ subscribeForKeyboardEvents(_: ShareableRef<number>): number {
135
61
  console.warn(
136
62
  '[Reanimated] useAnimatedKeyboard is not available on web yet.'
137
63
  );
@@ -3,6 +3,10 @@ import { AnimatedStyle, StyleProps } from '../commonTypes';
3
3
 
4
4
  const reanimatedJS = new JSReanimated();
5
5
 
6
+ global._makeShareableClone = (c) => c;
7
+ global._scheduleOnJS = setImmediate;
8
+ global._getTimestamp = reanimatedJS.getTimestamp.bind(reanimatedJS);
9
+
6
10
  interface JSReanimatedComponent {
7
11
  previousStyle: StyleProps;
8
12
  setNativeProps: (style: StyleProps) => void;
@@ -59,6 +59,13 @@ export type LayoutAnimationFunction = (
59
59
  targetValues: LayoutAnimationsValues
60
60
  ) => LayoutAnimation;
61
61
 
62
+ export type LayoutAnimationStartFunction = (
63
+ tag: number,
64
+ type: string,
65
+ yogaValues: LayoutAnimationsValues,
66
+ config: LayoutAnimationFunction
67
+ ) => void;
68
+
62
69
  export interface ILayoutAnimationBuilder {
63
70
  build: () => LayoutAnimationFunction;
64
71
  }
@@ -12,6 +12,7 @@ export type {
12
12
  IExitAnimationBuilder,
13
13
  LayoutAnimationsValues,
14
14
  LayoutAnimationFunction,
15
+ LayoutAnimationStartFunction,
15
16
  ILayoutAnimationBuilder,
16
17
  BaseLayoutAnimationConfig,
17
18
  BaseBuilderAnimationConfig,
@@ -0,0 +1,94 @@
1
+ import { runOnUI } from '../core';
2
+ import { withStyleAnimation } from '../animation/styleAnimation';
3
+ import { LogBox } from 'react-native';
4
+ import { SharedValue } from '../commonTypes';
5
+ import { makeUIMutable } from '../mutables';
6
+ import {
7
+ LayoutAnimationFunction,
8
+ LayoutAnimationsValues,
9
+ } from './animationBuilder';
10
+
11
+ LogBox.ignoreLogs(['Overriding previous layout animation with']);
12
+
13
+ const TAG_OFFSET = 1e9;
14
+
15
+ function startObservingProgress(
16
+ tag: number,
17
+ sharedValue: SharedValue<number>
18
+ ): void {
19
+ 'worklet';
20
+ sharedValue.addListener(tag + TAG_OFFSET, () => {
21
+ _notifyAboutProgress(tag, sharedValue.value);
22
+ });
23
+ }
24
+
25
+ function stopObservingProgress(
26
+ tag: number,
27
+ sharedValue: SharedValue<number>,
28
+ cancelled: boolean,
29
+ removeView: boolean
30
+ ): void {
31
+ 'worklet';
32
+ sharedValue.removeListener(tag + TAG_OFFSET);
33
+ _notifyAboutEnd(tag, cancelled, removeView);
34
+ }
35
+
36
+ function createLayoutAnimationManager() {
37
+ 'worklet';
38
+ const enteringAnimationForTag = new Map();
39
+ const mutableValuesForTag = new Map();
40
+
41
+ return {
42
+ start(
43
+ tag: number,
44
+ type: string,
45
+ yogaValues: LayoutAnimationsValues,
46
+ config: LayoutAnimationFunction
47
+ ) {
48
+ const style = config(yogaValues);
49
+ let currentAnimation = style.animations;
50
+
51
+ if (type === 'entering') {
52
+ enteringAnimationForTag.set(tag, currentAnimation);
53
+ } else if (type === 'layout') {
54
+ // When layout animation is requested, but entering is still running, we merge
55
+ // new layout animation targets into the ongoing animation
56
+ const enteringAnimation = enteringAnimationForTag.get(tag);
57
+ if (enteringAnimation) {
58
+ currentAnimation = { ...enteringAnimation, ...style.animations };
59
+ }
60
+ }
61
+
62
+ let value = mutableValuesForTag.get(tag);
63
+ if (value === undefined) {
64
+ value = makeUIMutable(style.initialValues);
65
+ mutableValuesForTag.set(tag, value);
66
+ } else {
67
+ stopObservingProgress(tag, value, false, false);
68
+ value._value = style.initialValues;
69
+ }
70
+
71
+ // @ts-ignore The line below started failing because I added types to the method – don't have time to fix it right now
72
+ const animation = withStyleAnimation(currentAnimation);
73
+
74
+ animation.callback = (finished?: boolean) => {
75
+ if (finished) {
76
+ enteringAnimationForTag.delete(tag);
77
+ mutableValuesForTag.delete(tag);
78
+ const shouldRemoveView = type === 'exiting';
79
+ stopObservingProgress(tag, value, finished, shouldRemoveView);
80
+ }
81
+ style.callback &&
82
+ style.callback(finished === undefined ? false : finished);
83
+ };
84
+
85
+ startObservingProgress(tag, value);
86
+ value.value = animation;
87
+ },
88
+ };
89
+ }
90
+
91
+ runOnUI(() => {
92
+ 'worklet';
93
+ global.LayoutAnimationsManager = createLayoutAnimationManager();
94
+ })();