react-native-reanimated 3.0.0-rc.2 → 3.0.0-rc.3

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 (127) hide show
  1. package/Common/cpp/AnimatedSensor/AnimatedSensorModule.cpp +3 -0
  2. package/Common/cpp/{headers/AnimatedSensor → AnimatedSensor}/AnimatedSensorModule.h +0 -0
  3. package/Common/cpp/Fabric/FabricUtils.cpp +1 -1
  4. package/Common/cpp/{headers/Fabric → Fabric}/FabricUtils.h +0 -0
  5. package/Common/cpp/Fabric/ReanimatedUIManagerBinding.cpp +2 -0
  6. package/Common/cpp/{headers/Fabric → Fabric}/ReanimatedUIManagerBinding.h +0 -0
  7. package/Common/cpp/{headers/Fabric → Fabric}/ShadowTreeCloner.h +1 -1
  8. package/Common/cpp/LayoutAnimations/LayoutAnimationsProxy.cpp +2 -0
  9. package/Common/cpp/{headers/LayoutAnimations → LayoutAnimations}/LayoutAnimationsProxy.h +0 -0
  10. package/Common/cpp/NativeModules/NativeReanimatedModule.cpp +1 -0
  11. package/Common/cpp/{headers/NativeModules → NativeModules}/NativeReanimatedModule.h +5 -0
  12. package/Common/cpp/NativeModules/NativeReanimatedModuleSpec.cpp +2 -0
  13. package/Common/cpp/{headers/NativeModules → NativeModules}/NativeReanimatedModuleSpec.h +1 -1
  14. package/Common/cpp/ReanimatedRuntime/ReanimatedHermesRuntime.cpp +93 -0
  15. package/Common/cpp/ReanimatedRuntime/ReanimatedHermesRuntime.h +125 -0
  16. package/Common/cpp/ReanimatedRuntime/ReanimatedRuntime.cpp +50 -0
  17. package/Common/cpp/ReanimatedRuntime/ReanimatedRuntime.h +27 -0
  18. package/Common/cpp/ReanimatedRuntime/RuntimeInitialization.md +194 -0
  19. package/Common/cpp/{headers/Registries → Registries}/EventHandlerRegistry.h +0 -0
  20. package/Common/cpp/Registries/MapperRegistry.cpp +3 -1
  21. package/Common/cpp/{headers/Registries → Registries}/MapperRegistry.h +0 -0
  22. package/Common/cpp/{headers/Registries → Registries}/NewestShadowNodesRegistry.h +0 -0
  23. package/Common/cpp/Registries/WorkletsCache.cpp +8 -1
  24. package/Common/cpp/{headers/Registries → Registries}/WorkletsCache.h +0 -0
  25. package/Common/cpp/{headers/SharedItems → SharedItems}/FrozenObject.h +0 -0
  26. package/Common/cpp/{headers/SharedItems → SharedItems}/HostFunctionHandler.h +0 -0
  27. package/Common/cpp/SharedItems/MutableValue.cpp +2 -0
  28. package/Common/cpp/{headers/SharedItems → SharedItems}/MutableValue.h +0 -0
  29. package/Common/cpp/{headers/SharedItems → SharedItems}/MutableValueSetterProxy.h +0 -0
  30. package/Common/cpp/{headers/SharedItems → SharedItems}/RemoteObject.h +0 -0
  31. package/Common/cpp/{headers/SharedItems → SharedItems}/RuntimeManager.h +0 -0
  32. package/Common/cpp/SharedItems/ShareableValue.cpp +6 -0
  33. package/Common/cpp/{headers/SharedItems → SharedItems}/ShareableValue.h +0 -0
  34. package/Common/cpp/{headers/SharedItems → SharedItems}/SharedParent.h +0 -0
  35. package/Common/cpp/{headers/SharedItems → SharedItems}/ValueWrapper.h +0 -0
  36. package/Common/cpp/{headers/SpecTools → SpecTools}/ErrorHandler.h +0 -0
  37. package/Common/cpp/{headers/Tools → Tools}/FeaturesConfig.h +0 -0
  38. package/Common/cpp/Tools/JSIStoreValueUser.cpp +2 -2
  39. package/Common/cpp/{headers/Tools → Tools}/JSIStoreValueUser.h +0 -0
  40. package/Common/cpp/{headers/Tools → Tools}/Mapper.h +0 -0
  41. package/Common/cpp/{headers/Tools → Tools}/PlatformDepMethodsHolder.h +0 -0
  42. package/Common/cpp/{headers/Tools → Tools}/ReanimatedHiddenHeaders.h +1 -1
  43. package/Common/cpp/{headers/Tools → Tools}/RuntimeDecorator.h +0 -0
  44. package/Common/cpp/{headers/Tools → Tools}/Scheduler.h +0 -0
  45. package/Common/cpp/Tools/SingleInstanceChecker.h +58 -0
  46. package/Common/cpp/{headers/Tools → Tools}/WorkletEventHandler.h +0 -0
  47. package/RNReanimated.podspec +25 -23
  48. package/android/CMakeLists.txt +41 -38
  49. package/android/build.gradle +91 -72
  50. package/android/src/fabric/java/com/swmansion/reanimated/NativeProxy.java +10 -8
  51. package/android/src/main/cpp/{headers/AndroidErrorHandler.h → AndroidErrorHandler.h} +0 -0
  52. package/android/src/main/cpp/AndroidLogger.cpp +4 -1
  53. package/android/src/main/cpp/{headers/AndroidLogger.h → AndroidLogger.h} +0 -0
  54. package/android/src/main/cpp/{headers/AndroidScheduler.h → AndroidScheduler.h} +0 -0
  55. package/android/src/main/cpp/{headers/JNIHelper.h → JNIHelper.h} +0 -0
  56. package/android/src/main/cpp/{headers/LayoutAnimations.h → LayoutAnimations.h} +0 -0
  57. package/android/src/main/cpp/NativeProxy.cpp +12 -28
  58. package/android/src/main/cpp/{headers/NativeProxy.h → NativeProxy.h} +3 -1
  59. package/android/src/main/cpp/{headers/TurboModule.h → TurboModule.h} +0 -0
  60. package/android/src/main/java/com/swmansion/reanimated/ReanimatedMessageQueueThread.java +77 -0
  61. package/android/src/main/java/com/swmansion/reanimated/keyboardObserver/ReanimatedKeyboardEventListener.java +2 -1
  62. package/android/src/paper/java/com/swmansion/reanimated/NativeProxy.java +7 -7
  63. package/ios/REAModule.mm +10 -0
  64. package/ios/native/NativeProxy.mm +12 -19
  65. package/ios/native/REAInitializer.h +2 -2
  66. package/ios/native/REAInitializer.mm +2 -2
  67. package/ios/native/REAMessageThread.h +22 -0
  68. package/ios/native/REAMessageThread.mm +38 -0
  69. package/ios/native/UIResponder+Reanimated.mm +1 -1
  70. package/lib/commonjs/reanimated2/NativeMethods.js +20 -24
  71. package/lib/commonjs/reanimated2/NativeMethods.js.map +1 -1
  72. package/lib/commonjs/reanimated2/animation/delay.js +0 -12
  73. package/lib/commonjs/reanimated2/animation/delay.js.map +1 -1
  74. package/lib/commonjs/reanimated2/animation/repeat.js +0 -24
  75. package/lib/commonjs/reanimated2/animation/repeat.js.map +1 -1
  76. package/lib/commonjs/reanimated2/animation/sequence.js +0 -12
  77. package/lib/commonjs/reanimated2/animation/sequence.js.map +1 -1
  78. package/lib/commonjs/reanimated2/commonTypes.js.map +1 -1
  79. package/lib/commonjs/reanimated2/frameCallback/FrameCallbackRegistryJS.js +4 -4
  80. package/lib/commonjs/reanimated2/frameCallback/FrameCallbackRegistryJS.js.map +1 -1
  81. package/lib/commonjs/reanimated2/frameCallback/FrameCallbackRegistryUI.js +51 -19
  82. package/lib/commonjs/reanimated2/frameCallback/FrameCallbackRegistryUI.js.map +1 -1
  83. package/lib/commonjs/reanimated2/hook/index.js +8 -0
  84. package/lib/commonjs/reanimated2/hook/index.js.map +1 -1
  85. package/lib/commonjs/reanimated2/hook/useAnimatedScrollHandler.js.map +1 -1
  86. package/lib/commonjs/reanimated2/hook/useFrameCallback.js.map +1 -1
  87. package/lib/commonjs/reanimated2/hook/useScrollViewOffset.js +31 -0
  88. package/lib/commonjs/reanimated2/hook/useScrollViewOffset.js.map +1 -0
  89. package/lib/commonjs/reanimated2/utils.js +12 -3
  90. package/lib/commonjs/reanimated2/utils.js.map +1 -1
  91. package/lib/module/reanimated2/NativeMethods.js +20 -24
  92. package/lib/module/reanimated2/NativeMethods.js.map +1 -1
  93. package/lib/module/reanimated2/animation/delay.js +0 -10
  94. package/lib/module/reanimated2/animation/delay.js.map +1 -1
  95. package/lib/module/reanimated2/animation/repeat.js +0 -20
  96. package/lib/module/reanimated2/animation/repeat.js.map +1 -1
  97. package/lib/module/reanimated2/animation/sequence.js +0 -10
  98. package/lib/module/reanimated2/animation/sequence.js.map +1 -1
  99. package/lib/module/reanimated2/commonTypes.js.map +1 -1
  100. package/lib/module/reanimated2/frameCallback/FrameCallbackRegistryJS.js +4 -4
  101. package/lib/module/reanimated2/frameCallback/FrameCallbackRegistryJS.js.map +1 -1
  102. package/lib/module/reanimated2/frameCallback/FrameCallbackRegistryUI.js +51 -19
  103. package/lib/module/reanimated2/frameCallback/FrameCallbackRegistryUI.js.map +1 -1
  104. package/lib/module/reanimated2/hook/index.js +1 -0
  105. package/lib/module/reanimated2/hook/index.js.map +1 -1
  106. package/lib/module/reanimated2/hook/useAnimatedScrollHandler.js.map +1 -1
  107. package/lib/module/reanimated2/hook/useFrameCallback.js.map +1 -1
  108. package/lib/module/reanimated2/hook/useScrollViewOffset.js +20 -0
  109. package/lib/module/reanimated2/hook/useScrollViewOffset.js.map +1 -0
  110. package/lib/module/reanimated2/utils.js +13 -3
  111. package/lib/module/reanimated2/utils.js.map +1 -1
  112. package/package.json +6 -4
  113. package/plugin.js +105 -47
  114. package/react-native-reanimated.d.ts +13 -9
  115. package/src/reanimated2/NativeMethods.ts +28 -33
  116. package/src/reanimated2/animation/delay.ts +0 -12
  117. package/src/reanimated2/animation/repeat.ts +0 -25
  118. package/src/reanimated2/animation/sequence.ts +0 -13
  119. package/src/reanimated2/commonTypes.ts +9 -0
  120. package/src/reanimated2/frameCallback/FrameCallbackRegistryJS.ts +6 -9
  121. package/src/reanimated2/frameCallback/FrameCallbackRegistryUI.ts +78 -27
  122. package/src/reanimated2/hook/index.ts +1 -0
  123. package/src/reanimated2/hook/useAnimatedScrollHandler.ts +3 -1
  124. package/src/reanimated2/hook/useFrameCallback.ts +2 -1
  125. package/src/reanimated2/hook/useScrollViewOffset.ts +36 -0
  126. package/src/reanimated2/utils.ts +12 -5
  127. package/android/src/main/java/com/swmansion/reanimated/ReanimatedJSIModulePackage.java +0 -29
@@ -1,9 +1,8 @@
1
1
  /* global _WORKLET _measure _scrollTo _dispatchCommand _setGestureState */
2
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
3
- // @ts-nocheck
4
2
  import { Component } from 'react';
5
3
  import { findNodeHandle } from 'react-native';
6
- import { RefObjectFunction } from './commonTypes';
4
+ import { MeasuredDimensions } from './commonTypes';
5
+ import { RefObjectFunction } from './hook/commonTypes';
7
6
  import { shouldBeUseWeb } from './PlatformChecker';
8
7
 
9
8
  export function getTag(
@@ -12,45 +11,41 @@ export function getTag(
12
11
  return findNodeHandle(view);
13
12
  }
14
13
 
15
- export interface MeasuredDimensions {
16
- x: number;
17
- y: number;
18
- width: number;
19
- height: number;
20
- pageX: number;
21
- pageY: number;
22
- }
23
-
24
- const isNativeIndefined = shouldBeUseWeb();
14
+ const isNative = !shouldBeUseWeb();
25
15
 
26
16
  export function measure(
27
17
  animatedRef: RefObjectFunction<Component>
28
- ): MeasuredDimensions {
18
+ ): MeasuredDimensions | null {
29
19
  'worklet';
30
- if (!_WORKLET || isNativeIndefined) {
20
+ if (!_WORKLET || !isNative) {
31
21
  console.warn(
32
- '[reanimated.measure] method cannot be used for web or Chrome Debugger'
22
+ '[Reanimated] measure() cannot be used for web or Chrome Debugger'
33
23
  );
34
- return {
35
- x: NaN,
36
- y: NaN,
37
- width: NaN,
38
- height: NaN,
39
- pageX: NaN,
40
- pageY: NaN,
41
- };
24
+ return null;
42
25
  }
26
+
43
27
  const viewTag = animatedRef();
44
- const result = _measure(viewTag);
45
- if (result.x === -1234567) {
46
- throw new Error(`The view with tag ${viewTag} could not be measured`);
28
+ if (viewTag === -1) {
29
+ console.warn(
30
+ `[Reanimated] The view with tag ${viewTag} is not a valid argument for measure(). This may be because the view is not currently rendered, which may not be a bug (e.g. an off-screen FlatList item).`
31
+ );
32
+ return null;
47
33
  }
48
- if (isNaN(result.x)) {
34
+
35
+ const measured = _measure(viewTag);
36
+ if (measured.x === -1234567) {
37
+ console.warn(
38
+ `[Reanimated] The view with tag ${viewTag} returned an invalid measurement response`
39
+ );
40
+ return null;
41
+ } else if (isNaN(measured.x)) {
49
42
  console.warn(
50
- 'Trying to measure a component which gets view-flattened on Android. To disable view-flattening, set `collapsable={false}` on this component.'
43
+ `[Reanimated] The view with tag ${viewTag} gets view-flattened on Android. To disable view-flattening, set \`collapsable={false}\` on this component.`
51
44
  );
45
+ return null;
46
+ } else {
47
+ return measured;
52
48
  }
53
- return result;
54
49
  }
55
50
 
56
51
  export function dispatchCommand(
@@ -59,7 +54,7 @@ export function dispatchCommand(
59
54
  args: Array<unknown>
60
55
  ): void {
61
56
  'worklet';
62
- if (!_WORKLET || isNativeIndefined) {
57
+ if (!_WORKLET || !isNative) {
63
58
  return;
64
59
  }
65
60
  const shadowNodeWrapper = animatedRef();
@@ -91,7 +86,7 @@ if (global._IS_FABRIC) {
91
86
  animated: boolean
92
87
  ) => {
93
88
  'worklet';
94
- if (!_WORKLET || isNativeIndefined) {
89
+ if (!_WORKLET || !isNative) {
95
90
  return;
96
91
  }
97
92
  const viewTag = animatedRef();
@@ -101,7 +96,7 @@ if (global._IS_FABRIC) {
101
96
 
102
97
  export function setGestureState(handlerTag: number, newState: number): void {
103
98
  'worklet';
104
- if (!_WORKLET || isNativeIndefined) {
99
+ if (!_WORKLET || !isNative) {
105
100
  console.warn(
106
101
  '[Reanimated] You can not use setGestureState in non-worklet function.'
107
102
  );
@@ -75,15 +75,3 @@ export function withDelay(
75
75
  };
76
76
  });
77
77
  }
78
-
79
- /**
80
- * @deprecated Kept for backward compatibility. Will be removed soon.
81
- */
82
- export function delay(
83
- delayMs: number,
84
- _nextAnimation: NextAnimation<DelayAnimation>
85
- ): Animation<DelayAnimation> {
86
- 'worklet';
87
- console.warn('Method `delay` is deprecated. Please use `withDelay` instead');
88
- return withDelay(delayMs, _nextAnimation);
89
- }
@@ -91,28 +91,3 @@ export function withRepeat(
91
91
  } as RepeatAnimation;
92
92
  });
93
93
  }
94
-
95
- /**
96
- * @deprecated Kept for backward compatibility. Will be removed soon.
97
- */
98
- export function repeat(
99
- _nextAnimation: NextAnimation<RepeatAnimation>,
100
- numberOfReps = 2,
101
- reverse = false,
102
- callback?: AnimationCallback
103
- ): Animation<RepeatAnimation> {
104
- 'worklet';
105
- console.warn(
106
- 'Method `repeat` is deprecated. Please use `withRepeat` instead'
107
- );
108
- return withRepeat(_nextAnimation, numberOfReps, reverse, callback);
109
- }
110
-
111
- export function loop(
112
- nextAnimation: NextAnimation<RepeatAnimation>,
113
- numberOfLoops = 1
114
- ): Animation<RepeatAnimation> {
115
- 'worklet';
116
- console.warn('Method `loop` is deprecated. Please use `withRepeat` instead');
117
- return repeat(nextAnimation, Math.round(numberOfLoops * 2), true);
118
- }
@@ -83,16 +83,3 @@ export function withSequence(
83
83
  }
84
84
  );
85
85
  }
86
-
87
- /**
88
- * @deprecated Kept for backward compatibility. Will be removed soon.
89
- */
90
- export function sequence(
91
- ..._animations: NextAnimation<SequenceAnimation>[]
92
- ): Animation<SequenceAnimation> {
93
- 'worklet';
94
- console.warn(
95
- 'Method `sequence` is deprecated. Please use `withSequence` instead'
96
- );
97
- return withSequence(..._animations);
98
- }
@@ -161,3 +161,12 @@ export type AnimatedKeyboardInfo = {
161
161
  height: SharedValue<number>;
162
162
  state: SharedValue<KeyboardState>;
163
163
  };
164
+
165
+ export interface MeasuredDimensions {
166
+ x: number;
167
+ y: number;
168
+ width: number;
169
+ height: number;
170
+ pageX: number;
171
+ pageY: number;
172
+ }
@@ -1,5 +1,5 @@
1
1
  import { runOnUI } from '../core';
2
- import { prepareUIRegistry } from './FrameCallbackRegistryUI';
2
+ import { FrameInfo, prepareUIRegistry } from './FrameCallbackRegistryUI';
3
3
 
4
4
  export default class FrameCallbackRegistryJS {
5
5
  private nextCallbackId = 0;
@@ -8,7 +8,7 @@ export default class FrameCallbackRegistryJS {
8
8
  prepareUIRegistry();
9
9
  }
10
10
 
11
- registerFrameCallback(callback: () => void): number {
11
+ registerFrameCallback(callback: (frameInfo: FrameInfo) => void): number {
12
12
  if (!callback) {
13
13
  return -1;
14
14
  }
@@ -24,20 +24,17 @@ export default class FrameCallbackRegistryJS {
24
24
  return callbackId;
25
25
  }
26
26
 
27
- unregisterFrameCallback(frameCallbackId: number): void {
27
+ unregisterFrameCallback(callbackId: number): void {
28
28
  runOnUI(() => {
29
29
  'worklet';
30
- global._frameCallbackRegistry.unregisterFrameCallback(frameCallbackId);
30
+ global._frameCallbackRegistry.unregisterFrameCallback(callbackId);
31
31
  })();
32
32
  }
33
33
 
34
- manageStateFrameCallback(frameCallbackId: number, state: boolean): void {
34
+ manageStateFrameCallback(callbackId: number, state: boolean): void {
35
35
  runOnUI(() => {
36
36
  'worklet';
37
- global._frameCallbackRegistry.manageStateFrameCallback(
38
- frameCallbackId,
39
- state
40
- );
37
+ global._frameCallbackRegistry.manageStateFrameCallback(callbackId, state);
41
38
  })();
42
39
  }
43
40
  }
@@ -1,61 +1,112 @@
1
1
  import { runOnUI } from '../core';
2
2
 
3
- export default interface FrameCallbackRegistryUI {
4
- frameCallbackRegistry: Map<number, () => void>;
5
- frameCallbackActive: Set<number>;
6
- isFrameCallbackRunning: boolean;
3
+ type CallbackDetails = {
4
+ callback: (frameInfo: FrameInfo) => void;
5
+ startTime: number | null;
6
+ };
7
+
8
+ export type FrameInfo = {
9
+ timestamp: number;
10
+ timeSincePreviousFrame: number | null;
11
+ timeSinceFirstFrame: number;
12
+ };
13
+
14
+ interface FrameCallbackRegistryUI {
15
+ frameCallbackRegistry: Map<number, CallbackDetails>;
16
+ activeFrameCallbacks: Set<number>;
17
+ previousFrameTimestamp: number | null;
7
18
  runCallbacks: () => void;
8
- registerFrameCallback: (callback: () => void, callbackId: number) => void;
9
- unregisterFrameCallback: (frameCallbackId: number) => void;
10
- manageStateFrameCallback: (frameCallbackId: number, state: boolean) => void;
19
+ registerFrameCallback: (
20
+ callback: (frameInfo: FrameInfo) => void,
21
+ callbackId: number
22
+ ) => void;
23
+ unregisterFrameCallback: (callbackId: number) => void;
24
+ manageStateFrameCallback: (callbackId: number, state: boolean) => void;
11
25
  }
12
26
 
13
27
  export const prepareUIRegistry = runOnUI(() => {
14
28
  'worklet';
15
29
 
16
30
  const frameCallbackRegistry: FrameCallbackRegistryUI = {
17
- frameCallbackRegistry: new Map<number, () => void>(),
18
- frameCallbackActive: new Set<number>(),
19
- isFrameCallbackRunning: false,
31
+ frameCallbackRegistry: new Map<number, CallbackDetails>(),
32
+ activeFrameCallbacks: new Set<number>(),
33
+ previousFrameTimestamp: null,
20
34
 
21
35
  runCallbacks() {
22
- const loop = () => {
23
- this.frameCallbackActive.forEach((key: number) => {
24
- const callback = this.frameCallbackRegistry.get(key);
25
- callback!();
36
+ const loop = (timestamp: number) => {
37
+ if (this.previousFrameTimestamp === null) {
38
+ this.previousFrameTimestamp = timestamp;
39
+ }
40
+
41
+ const delta = timestamp - this.previousFrameTimestamp;
42
+
43
+ this.activeFrameCallbacks.forEach((callbackId: number) => {
44
+ const callbackDetails = this.frameCallbackRegistry.get(callbackId)!;
45
+
46
+ const { startTime } = callbackDetails;
47
+
48
+ if (startTime === null) {
49
+ // First frame
50
+ callbackDetails.startTime = timestamp;
51
+
52
+ callbackDetails.callback({
53
+ timestamp,
54
+ timeSincePreviousFrame: null,
55
+ timeSinceFirstFrame: 0,
56
+ });
57
+ } else {
58
+ // Next frame
59
+ callbackDetails.callback({
60
+ timestamp,
61
+ timeSincePreviousFrame: delta,
62
+ timeSinceFirstFrame: timestamp - startTime,
63
+ });
64
+ }
26
65
  });
27
66
 
28
- if (this.frameCallbackActive.size > 0) {
67
+ if (this.activeFrameCallbacks.size > 0) {
68
+ this.previousFrameTimestamp = timestamp;
29
69
  requestAnimationFrame(loop);
30
70
  } else {
31
- this.isFrameCallbackRunning = false;
71
+ this.previousFrameTimestamp = null;
32
72
  }
33
73
  };
34
74
 
35
- if (!this.isFrameCallbackRunning) {
75
+ // runCallback() should only be called after registering a callback,
76
+ // so if there is only one active callback, then it means that there were
77
+ // zero previously and the loop isn't running yet.
78
+ if (this.activeFrameCallbacks.size === 1) {
36
79
  requestAnimationFrame(loop);
37
- this.isFrameCallbackRunning = true;
38
80
  }
39
81
  },
40
82
 
41
- registerFrameCallback(callback: () => void, callbackId: number) {
42
- this.frameCallbackRegistry.set(callbackId, callback);
83
+ registerFrameCallback(
84
+ callback: (frameInfo: FrameInfo) => void,
85
+ callbackId: number
86
+ ) {
87
+ this.frameCallbackRegistry.set(callbackId, {
88
+ callback: callback,
89
+ startTime: null,
90
+ });
43
91
  },
44
92
 
45
- unregisterFrameCallback(frameCallbackId: number) {
46
- this.manageStateFrameCallback(frameCallbackId, false);
47
- this.frameCallbackRegistry.delete(frameCallbackId);
93
+ unregisterFrameCallback(callbackId: number) {
94
+ this.manageStateFrameCallback(callbackId, false);
95
+ this.frameCallbackRegistry.delete(callbackId);
48
96
  },
49
97
 
50
- manageStateFrameCallback(frameCallbackId: number, state: boolean) {
51
- if (frameCallbackId === -1) {
98
+ manageStateFrameCallback(callbackId: number, state: boolean) {
99
+ if (callbackId === -1) {
52
100
  return;
53
101
  }
54
102
  if (state) {
55
- this.frameCallbackActive.add(frameCallbackId);
103
+ this.activeFrameCallbacks.add(callbackId);
56
104
  this.runCallbacks();
57
105
  } else {
58
- this.frameCallbackActive.delete(frameCallbackId);
106
+ const callback = this.frameCallbackRegistry.get(callbackId)!;
107
+ callback.startTime = null;
108
+
109
+ this.activeFrameCallbacks.delete(callbackId);
59
110
  }
60
111
  },
61
112
  };
@@ -24,3 +24,4 @@ export { useAnimatedSensor, SensorType } from './useAnimatedSensor';
24
24
  export { useFrameCallback } from './useFrameCallback';
25
25
  export type { FrameCallback } from './useFrameCallback';
26
26
  export { useAnimatedKeyboard } from './useAnimatedKeyboard';
27
+ export { useScrollViewOffset } from './useScrollViewOffset';
@@ -10,7 +10,9 @@ export interface ScrollHandler<TContext extends Context>
10
10
  (event: NativeScrollEvent, context?: TContext): void;
11
11
  }
12
12
 
13
- interface ScrollEvent extends NativeScrollEvent, NativeEvent<ScrollEvent> {
13
+ export interface ScrollEvent
14
+ extends NativeScrollEvent,
15
+ NativeEvent<ScrollEvent> {
14
16
  eventName: string;
15
17
  }
16
18
  export interface ScrollHandlers<TContext extends Context> {
@@ -1,5 +1,6 @@
1
1
  import { useEffect, useRef } from 'react';
2
2
  import FrameCallbackRegistryJS from '../frameCallback/FrameCallbackRegistryJS';
3
+ import { FrameInfo } from '../frameCallback/FrameCallbackRegistryUI';
3
4
 
4
5
  export type FrameCallback = {
5
6
  setActive: (isActive: boolean) => void;
@@ -9,7 +10,7 @@ export type FrameCallback = {
9
10
  const frameCallbackRegistry = new FrameCallbackRegistryJS();
10
11
 
11
12
  export function useFrameCallback(
12
- callback: () => void,
13
+ callback: (frameInfo: FrameInfo) => void,
13
14
  autostart = true
14
15
  ): FrameCallback {
15
16
  const ref = useRef<FrameCallback>({
@@ -0,0 +1,36 @@
1
+ import { RefObject, useEffect, useRef } from 'react';
2
+
3
+ import { findNodeHandle } from 'react-native';
4
+ import type Animated from 'react-native-reanimated';
5
+ import { useEvent, useSharedValue } from '.';
6
+ import { SharedValue } from '../commonTypes';
7
+ import { ScrollEvent } from './useAnimatedScrollHandler';
8
+
9
+ const subscribeForEvents = [
10
+ 'onScroll',
11
+ 'onScrollBeginDrag',
12
+ 'onScrollEndDrag',
13
+ 'onMomentumScrollBegin',
14
+ 'onMomentumScrollEnd',
15
+ ];
16
+
17
+ export function useScrollViewOffset(
18
+ aref: RefObject<Animated.ScrollView>
19
+ ): SharedValue<number> {
20
+ const offsetRef = useRef(useSharedValue(0));
21
+
22
+ const event = useEvent<ScrollEvent>((event: ScrollEvent) => {
23
+ 'worklet';
24
+ offsetRef.current.value =
25
+ event.contentOffset.x === 0
26
+ ? event.contentOffset.y
27
+ : event.contentOffset.x;
28
+ }, subscribeForEvents);
29
+
30
+ useEffect(() => {
31
+ const viewTag = findNodeHandle(aref.current);
32
+ event.current?.registerForEvents(viewTag as number);
33
+ }, [aref.current]);
34
+
35
+ return offsetRef.current;
36
+ }
@@ -7,15 +7,22 @@ export interface ComponentCoords {
7
7
  y: number;
8
8
  }
9
9
 
10
+ /**
11
+ * Given an absolute position and a component ref, returns the relative
12
+ * position in the component's local coordinate space.
13
+ */
10
14
  export function getRelativeCoords(
11
15
  parentRef: RefObjectFunction<Component>,
12
- x: number,
13
- y: number
14
- ): ComponentCoords {
16
+ absoluteX: number,
17
+ absoluteY: number
18
+ ): ComponentCoords | null {
15
19
  'worklet';
16
20
  const parentCoords = measure(parentRef);
21
+ if (parentCoords === null) {
22
+ return null;
23
+ }
17
24
  return {
18
- x: x - parentCoords.x,
19
- y: y - parentCoords.y,
25
+ x: absoluteX - parentCoords.x,
26
+ y: absoluteY - parentCoords.y,
20
27
  };
21
28
  }
@@ -1,29 +0,0 @@
1
- package com.swmansion.reanimated;
2
-
3
- import android.util.Log;
4
- import com.facebook.react.bridge.JSIModulePackage;
5
- import com.facebook.react.bridge.JSIModuleSpec;
6
- import com.facebook.react.bridge.JavaScriptContextHolder;
7
- import com.facebook.react.bridge.ReactApplicationContext;
8
- import java.util.Arrays;
9
- import java.util.List;
10
-
11
- public class ReanimatedJSIModulePackage implements JSIModulePackage {
12
- /**
13
- * @deprecated Since 2.5.0, Reanimated autoinstalls on Android - you can remove
14
- * getJSIModulePackage() override in MainApplication.java.
15
- */
16
- @Deprecated
17
- public ReanimatedJSIModulePackage() {
18
- super();
19
- }
20
-
21
- @Override
22
- public List<JSIModuleSpec> getJSIModules(
23
- ReactApplicationContext reactApplicationContext, JavaScriptContextHolder jsContext) {
24
- Log.w(
25
- "[REANIMATED]",
26
- "Since 2.5.0, Reanimated autoinstalls on Android - you can remove getJSIModulePackage() override in MainApplication.java.");
27
- return Arrays.<JSIModuleSpec>asList();
28
- }
29
- }