react-native-reanimated 2.10.0 → 2.11.0

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/LayoutAnimations/LayoutAnimationsProxy.cpp +2 -0
  4. package/Common/cpp/{headers/LayoutAnimations → LayoutAnimations}/LayoutAnimationsProxy.h +0 -0
  5. package/Common/cpp/NativeModules/NativeReanimatedModule.cpp +2 -0
  6. package/Common/cpp/{headers/NativeModules → NativeModules}/NativeReanimatedModule.h +5 -0
  7. package/Common/cpp/NativeModules/NativeReanimatedModuleSpec.cpp +2 -0
  8. package/Common/cpp/{headers/NativeModules → NativeModules}/NativeReanimatedModuleSpec.h +1 -1
  9. package/Common/cpp/{headers/Registries → Registries}/EventHandlerRegistry.h +0 -0
  10. package/Common/cpp/Registries/MapperRegistry.cpp +3 -1
  11. package/Common/cpp/{headers/Registries → Registries}/MapperRegistry.h +0 -0
  12. package/Common/cpp/Registries/WorkletsCache.cpp +3 -0
  13. package/Common/cpp/{headers/Registries → Registries}/WorkletsCache.h +0 -0
  14. package/Common/cpp/{headers/SharedItems → SharedItems}/FrozenObject.h +0 -0
  15. package/Common/cpp/{headers/SharedItems → SharedItems}/HostFunctionHandler.h +0 -0
  16. package/Common/cpp/SharedItems/MutableValue.cpp +2 -0
  17. package/Common/cpp/{headers/SharedItems → SharedItems}/MutableValue.h +0 -0
  18. package/Common/cpp/{headers/SharedItems → SharedItems}/MutableValueSetterProxy.h +0 -0
  19. package/Common/cpp/{headers/SharedItems → SharedItems}/RemoteObject.h +0 -0
  20. package/Common/cpp/{headers/SharedItems → SharedItems}/RuntimeManager.h +0 -0
  21. package/Common/cpp/SharedItems/ShareableValue.cpp +7 -2
  22. package/Common/cpp/{headers/SharedItems → SharedItems}/ShareableValue.h +0 -0
  23. package/Common/cpp/{headers/SharedItems → SharedItems}/SharedParent.h +0 -0
  24. package/Common/cpp/{headers/SharedItems → SharedItems}/ValueWrapper.h +0 -0
  25. package/Common/cpp/{headers/SpecTools → SpecTools}/ErrorHandler.h +0 -0
  26. package/Common/cpp/{headers/Tools → Tools}/FeaturesConfig.h +0 -0
  27. package/Common/cpp/Tools/JSIStoreValueUser.cpp +2 -2
  28. package/Common/cpp/{headers/Tools → Tools}/JSIStoreValueUser.h +0 -0
  29. package/Common/cpp/{headers/Tools → Tools}/Mapper.h +0 -0
  30. package/Common/cpp/{headers/Tools → Tools}/PlatformDepMethodsHolder.h +0 -0
  31. package/Common/cpp/{headers/Tools → Tools}/ReanimatedHiddenHeaders.h +1 -1
  32. package/Common/cpp/{headers/Tools → Tools}/RuntimeDecorator.h +0 -0
  33. package/Common/cpp/{headers/Tools → Tools}/Scheduler.h +0 -0
  34. package/Common/cpp/Tools/SingleInstanceChecker.h +58 -0
  35. package/Common/cpp/{headers/Tools → Tools}/WorkletEventHandler.h +0 -0
  36. package/RNReanimated.podspec +14 -73
  37. package/android/.gradle/7.5.1/checksums/checksums.lock +0 -0
  38. package/android/.gradle/7.5.1/checksums/md5-checksums.bin +0 -0
  39. package/android/.gradle/7.5.1/checksums/sha1-checksums.bin +0 -0
  40. package/android/.gradle/7.5.1/dependencies-accessors/dependencies-accessors.lock +0 -0
  41. package/android/.gradle/{7.2 → 7.5.1}/dependencies-accessors/gc.properties +0 -0
  42. package/android/.gradle/7.5.1/executionHistory/executionHistory.bin +0 -0
  43. package/android/.gradle/7.5.1/executionHistory/executionHistory.lock +0 -0
  44. package/android/.gradle/{7.2 → 7.5.1}/fileChanges/last-build.bin +0 -0
  45. package/android/.gradle/7.5.1/fileHashes/fileHashes.bin +0 -0
  46. package/android/.gradle/7.5.1/fileHashes/fileHashes.lock +0 -0
  47. package/android/.gradle/{7.2 → 7.5.1}/fileHashes/resourceHashesCache.bin +0 -0
  48. package/android/.gradle/{7.2 → 7.5.1}/gc.properties +0 -0
  49. package/android/.gradle/buildOutputCleanup/buildOutputCleanup.lock +0 -0
  50. package/android/.gradle/buildOutputCleanup/cache.properties +2 -2
  51. package/android/.gradle/buildOutputCleanup/outputFiles.bin +0 -0
  52. package/android/.gradle/file-system.probe +0 -0
  53. package/android/CMakeLists.txt +56 -65
  54. package/android/build.gradle +377 -103
  55. package/android/gradle/wrapper/gradle-wrapper.properties +1 -1
  56. package/android/react-native-reanimated-66-hermes.aar +0 -0
  57. package/android/react-native-reanimated-66-jsc.aar +0 -0
  58. package/android/react-native-reanimated-67-hermes.aar +0 -0
  59. package/android/react-native-reanimated-67-jsc.aar +0 -0
  60. package/android/react-native-reanimated-68-hermes.aar +0 -0
  61. package/android/react-native-reanimated-68-jsc.aar +0 -0
  62. package/android/react-native-reanimated-69-hermes.aar +0 -0
  63. package/android/react-native-reanimated-69-jsc.aar +0 -0
  64. package/android/react-native-reanimated-70-hermes.aar +0 -0
  65. package/android/react-native-reanimated-70-jsc.aar +0 -0
  66. package/android/src/main/cpp/AndroidErrorHandler.cpp +3 -4
  67. package/android/src/main/cpp/{headers/AndroidErrorHandler.h → AndroidErrorHandler.h} +1 -4
  68. package/android/src/main/cpp/AndroidLogger.cpp +4 -1
  69. package/android/src/main/cpp/{headers/AndroidLogger.h → AndroidLogger.h} +0 -0
  70. package/android/src/main/cpp/{headers/AndroidScheduler.h → AndroidScheduler.h} +0 -0
  71. package/android/src/main/cpp/{headers/JNIHelper.h → JNIHelper.h} +0 -0
  72. package/android/src/main/cpp/{headers/LayoutAnimations.h → LayoutAnimations.h} +0 -0
  73. package/android/src/main/cpp/NativeProxy.cpp +6 -8
  74. package/android/src/main/cpp/{headers/NativeProxy.h → NativeProxy.h} +0 -0
  75. package/android/src/main/cpp/{headers/TurboModule.h → TurboModule.h} +0 -0
  76. package/android/src/main/java/com/swmansion/reanimated/NativeProxy.java +4 -5
  77. package/android/src/main/java/com/swmansion/reanimated/keyboardObserver/ReanimatedKeyboardEventListener.java +2 -1
  78. package/android/src/main/java/com/swmansion/reanimated/layoutReanimation/AnimationsManager.java +2 -2
  79. package/ios/{REAModule.m → REAModule.mm} +12 -0
  80. package/ios/native/NativeProxy.mm +4 -2
  81. package/ios/native/REAInitializer.h +2 -2
  82. package/ios/native/REAInitializer.mm +2 -2
  83. package/ios/native/UIResponder+Reanimated.mm +1 -1
  84. package/lib/index.web.js +2 -0
  85. package/lib/reanimated2/NativeMethods.js +33 -19
  86. package/lib/reanimated2/frameCallback/FrameCallbackRegistryJS.js +4 -4
  87. package/lib/reanimated2/frameCallback/FrameCallbackRegistryUI.js +47 -18
  88. package/lib/reanimated2/globals.d.ts +2 -2
  89. package/lib/reanimated2/hook/index.js +1 -0
  90. package/lib/reanimated2/hook/useScrollViewOffset.js +26 -0
  91. package/lib/reanimated2/utils.js +10 -3
  92. package/lib/types/index.web.d.ts +1 -0
  93. package/lib/types/reanimated2/NativeMethods.d.ts +3 -10
  94. package/lib/types/reanimated2/commonTypes.d.ts +8 -0
  95. package/lib/types/reanimated2/frameCallback/FrameCallbackRegistryJS.d.ts +4 -3
  96. package/lib/types/reanimated2/frameCallback/FrameCallbackRegistryUI.d.ts +5 -9
  97. package/lib/types/reanimated2/hook/index.d.ts +1 -0
  98. package/lib/types/reanimated2/hook/useAnimatedScrollHandler.d.ts +1 -2
  99. package/lib/types/reanimated2/hook/useFrameCallback.d.ts +2 -1
  100. package/lib/types/reanimated2/hook/useScrollViewOffset.d.ts +4 -0
  101. package/lib/types/reanimated2/utils.d.ts +5 -1
  102. package/package.json +5 -4
  103. package/plugin.js +1 -1
  104. package/react-native-reanimated.d.ts +13 -9
  105. package/scripts/reanimated_utils.rb +65 -0
  106. package/src/index.web.ts +2 -0
  107. package/src/reanimated2/NativeMethods.ts +44 -32
  108. package/src/reanimated2/commonTypes.ts +9 -0
  109. package/src/reanimated2/frameCallback/FrameCallbackRegistryJS.ts +6 -9
  110. package/src/reanimated2/frameCallback/FrameCallbackRegistryUI.ts +78 -27
  111. package/src/reanimated2/globals.d.ts +2 -2
  112. package/src/reanimated2/hook/index.ts +1 -0
  113. package/src/reanimated2/hook/useAnimatedScrollHandler.ts +3 -1
  114. package/src/reanimated2/hook/useFrameCallback.ts +2 -1
  115. package/src/reanimated2/hook/useScrollViewOffset.ts +36 -0
  116. package/src/reanimated2/utils.ts +12 -5
  117. package/android/.gradle/7.2/dependencies-accessors/dependencies-accessors.lock +0 -0
  118. package/android/.gradle/7.2/executionHistory/executionHistory.bin +0 -0
  119. package/android/.gradle/7.2/executionHistory/executionHistory.lock +0 -0
  120. package/android/.gradle/7.2/fileHashes/fileHashes.bin +0 -0
  121. package/android/.gradle/7.2/fileHashes/fileHashes.lock +0 -0
  122. package/android/.gradle/checksums/checksums.lock +0 -0
  123. package/android/.gradle/checksums/md5-checksums.bin +0 -0
  124. package/android/.gradle/checksums/sha1-checksums.bin +0 -0
  125. package/android/src/main/java/com/swmansion/reanimated/AndroidErrorHandler.java +0 -8
  126. package/lib/reanimated2/__mocks__/NativeReanimated.native.js +0 -20
  127. package/lib/types/reanimated2/__mocks__/NativeReanimated.native.d.ts +0 -13
@@ -1,5 +1,5 @@
1
1
  distributionBase=GRADLE_USER_HOME
2
2
  distributionPath=wrapper/dists
3
- distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
3
+ distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-all.zip
4
4
  zipStoreBase=GRADLE_USER_HOME
5
5
  zipStorePath=wrapper/dists
@@ -1,5 +1,6 @@
1
1
  #include "AndroidErrorHandler.h"
2
2
  #include <fbjni/fbjni.h>
3
+ #include <exception>
3
4
  #include <string>
4
5
  #include "Logger.h"
5
6
 
@@ -17,11 +18,9 @@ void AndroidErrorHandler::raiseSpec() {
17
18
  return;
18
19
  }
19
20
 
20
- static const auto cls = javaClassStatic();
21
- static auto method = cls->getStaticMethod<void(std::string)>("raise");
22
- method(cls, error->message);
23
-
21
+ // mark error as handled before this method throws exception
24
22
  this->error->handled = true;
23
+ throw std::runtime_error(this->error->message);
25
24
  }
26
25
 
27
26
  std::shared_ptr<Scheduler> AndroidErrorHandler::getScheduler() {
@@ -11,15 +11,12 @@
11
11
 
12
12
  namespace reanimated {
13
13
 
14
- class AndroidErrorHandler : public JavaClass<AndroidErrorHandler>,
15
- public ErrorHandler {
14
+ class AndroidErrorHandler : public ErrorHandler {
16
15
  std::shared_ptr<ErrorWrapper> error;
17
16
  std::shared_ptr<Scheduler> scheduler;
18
17
  void raiseSpec() override;
19
18
 
20
19
  public:
21
- static auto constexpr kJavaDescriptor =
22
- "Lcom/swmansion/reanimated/AndroidErrorHandler;";
23
20
  explicit AndroidErrorHandler(std::shared_ptr<Scheduler> scheduler);
24
21
  std::shared_ptr<Scheduler> getScheduler() override;
25
22
  std::shared_ptr<ErrorWrapper> getError() override;
@@ -1,6 +1,9 @@
1
- #include "AndroidLogger.h"
2
1
  #include <android/log.h>
2
+ #include <memory>
3
+
4
+ #include "AndroidLogger.h"
3
5
  #include "Logger.h"
6
+
4
7
  #define APPNAME "NATIVE_REANIMATED"
5
8
 
6
9
  namespace reanimated {
@@ -73,11 +73,10 @@ void NativeProxy::installJSIBindings() {
73
73
  };
74
74
 
75
75
  auto getCurrentTime = [this]() {
76
- auto method =
77
- javaPart_->getClass()->getMethod<local_ref<JString>()>("getUptime");
78
- local_ref<JString> output = method(javaPart_.get());
79
- return static_cast<double>(
80
- std::strtoll(output->toStdString().c_str(), NULL, 10));
76
+ static const auto method =
77
+ javaPart_->getClass()->getMethod<jlong()>("getCurrentTime");
78
+ jlong output = method(javaPart_.get());
79
+ return static_cast<double>(output);
81
80
  };
82
81
 
83
82
  auto requestRender = [this, getCurrentTime](
@@ -353,11 +352,10 @@ void NativeProxy::configureProps(
353
352
  ReadableNativeArray::javaobject)>("configureProps");
354
353
  method(
355
354
  javaPart_.get(),
356
- ReadableNativeArray::newObjectCxxArgs(
357
- std::move(jsi::dynamicFromValue(rt, uiProps)))
355
+ ReadableNativeArray::newObjectCxxArgs(jsi::dynamicFromValue(rt, uiProps))
358
356
  .get(),
359
357
  ReadableNativeArray::newObjectCxxArgs(
360
- std::move(jsi::dynamicFromValue(rt, nativeProps)))
358
+ jsi::dynamicFromValue(rt, nativeProps))
361
359
  .get());
362
360
  }
363
361
 
@@ -203,14 +203,13 @@ public class NativeProxy {
203
203
  }
204
204
 
205
205
  @DoNotStrip
206
- private String getUptime() {
206
+ private long getCurrentTime() {
207
207
  if (slowAnimationsEnabled) {
208
208
  final long ANIMATIONS_DRAG_FACTOR = 10;
209
- return Long.toString(
210
- this.firstUptime
211
- + (SystemClock.uptimeMillis() - this.firstUptime) / ANIMATIONS_DRAG_FACTOR);
209
+ return this.firstUptime
210
+ + (SystemClock.uptimeMillis() - this.firstUptime) / ANIMATIONS_DRAG_FACTOR;
212
211
  } else {
213
- return Long.toString(SystemClock.uptimeMillis());
212
+ return SystemClock.uptimeMillis();
214
213
  }
215
214
  }
216
215
 
@@ -57,7 +57,8 @@ public class ReanimatedKeyboardEventListener {
57
57
  rootView,
58
58
  (v, insets) -> {
59
59
  int paddingBottom = 0;
60
- if (!BuildConfig.IS_NEW_ARCHITECTURE_ENABLED && BuildConfig.REACT_NATIVE_VERSION < 70) {
60
+ if (!BuildConfig.IS_NEW_ARCHITECTURE_ENABLED
61
+ && BuildConfig.REACT_NATIVE_MINOR_VERSION < 70) {
61
62
  paddingBottom = insets.getInsets(WindowInsetsCompat.Type.navigationBars()).bottom;
62
63
  }
63
64
  int paddingTop = insets.getInsets(WindowInsetsCompat.Type.systemBars()).top;
@@ -229,8 +229,8 @@ public class AnimationsManager implements ViewHierarchyObserver {
229
229
  }
230
230
 
231
231
  private void removeLeftovers() {
232
- //mToRemove may be null if onCatalystInstanceDestroy was called first
233
- if(mToRemove != null){
232
+ // mToRemove may be null if onCatalystInstanceDestroy was called first
233
+ if (mToRemove != null) {
234
234
  HashSet<Integer> roots = new HashSet<>();
235
235
  // go through ready to remove from bottom to top
236
236
  for (int tag : mToRemove) {
@@ -3,15 +3,27 @@
3
3
  #import <RNReanimated/REANodesManager.h>
4
4
  #import <RNReanimated/REATransitionManager.h>
5
5
 
6
+ #import "SingleInstanceChecker.h"
7
+
8
+ using namespace reanimated;
9
+
6
10
  typedef void (^AnimatedOperation)(REANodesManager *nodesManager);
7
11
 
8
12
  @implementation REAModule {
9
13
  NSMutableArray<AnimatedOperation> *_operations;
10
14
  REATransitionManager *_transitionManager;
15
+ #ifdef DEBUG
16
+ SingleInstanceChecker<REAModule> singleInstanceChecker_;
17
+ #endif
11
18
  }
12
19
 
13
20
  RCT_EXPORT_MODULE(ReanimatedModule);
14
21
 
22
+ + (BOOL)requiresMainQueueSetup
23
+ {
24
+ return YES;
25
+ }
26
+
15
27
  - (void)invalidate
16
28
  {
17
29
  _transitionManager = nil;
@@ -201,8 +201,10 @@ std::shared_ptr<NativeReanimatedModule> createReanimatedModule(
201
201
  }
202
202
  };
203
203
 
204
- auto requestRender = [reanimatedModule, &module](std::function<void(double)> onRender, jsi::Runtime &rt) {
205
- [reanimatedModule.nodesManager postOnAnimation:^(CADisplayLink *displayLink) {
204
+ auto nodesManager = reanimatedModule.nodesManager;
205
+
206
+ auto requestRender = [nodesManager, &module](std::function<void(double)> onRender, jsi::Runtime &rt) {
207
+ [nodesManager postOnAnimation:^(CADisplayLink *displayLink) {
206
208
  double frameTimestamp = calculateTimestampWithSlowAnimations(displayLink.targetTimestamp) * 1000;
207
209
  jsi::Object global = rt.global();
208
210
  jsi::String frameTimestampName = jsi::String::createFromAscii(rt, "_frameTimestamp");
@@ -7,11 +7,11 @@
7
7
  #import <ReactCommon/RCTTurboModuleManager.h>
8
8
  #import <jsireact/JSIExecutor.h>
9
9
 
10
- #if RNVERSION >= 64
10
+ #if REACT_NATIVE_MINOR_VERSION >= 64
11
11
  #import <React/RCTJSIExecutorRuntimeInstaller.h>
12
12
  #endif
13
13
 
14
- #if RNVERSION < 63
14
+ #if REACT_NATIVE_MINOR_VERSION < 63
15
15
  #import <ReactCommon/BridgeJSCallInvoker.h>
16
16
  #endif
17
17
 
@@ -24,7 +24,7 @@ JSIExecutor::RuntimeInstaller REAJSIExecutorRuntimeInstaller(
24
24
 
25
25
  [bridge moduleForClass:[RCTEventDispatcher class]];
26
26
  RCTEventDispatcher *eventDispatcher = [REAEventDispatcher new];
27
- #if RNVERSION >= 66
27
+ #if REACT_NATIVE_MINOR_VERSION >= 66
28
28
  RCTCallableJSModules *callableJSModules = [RCTCallableJSModules new];
29
29
  [bridge setValue:callableJSModules forKey:@"_callableJSModules"];
30
30
  [callableJSModules setBridge:bridge];
@@ -39,7 +39,7 @@ JSIExecutor::RuntimeInstaller REAJSIExecutorRuntimeInstaller(
39
39
  if (!bridge) {
40
40
  return;
41
41
  }
42
- #if RNVERSION >= 63
42
+ #if REACT_NATIVE_MINOR_VERSION >= 63
43
43
  auto reanimatedModule = reanimated::createReanimatedModule(bridge, bridge.jsCallInvoker);
44
44
  #else
45
45
  auto callInvoker = std::make_shared<react::BridgeJSCallInvoker>(bridge.reactInstance);
@@ -20,7 +20,7 @@ typedef JSCExecutorFactory ExecutorFactory;
20
20
  {
21
21
  const auto installer = reanimated::REAJSIExecutorRuntimeInstaller(bridge, NULL);
22
22
 
23
- #if RNVERSION >= 64
23
+ #if REACT_NATIVE_MINOR_VERSION >= 64
24
24
  // installs globals such as console, nativePerformanceNow, etc.
25
25
  return std::make_unique<ExecutorFactory>(RCTJSIExecutorRuntimeInstaller(installer));
26
26
  #else
package/lib/index.web.js CHANGED
@@ -1,4 +1,6 @@
1
1
  // tree-shaken side effects
2
2
  import './reanimated2/js-reanimated/global';
3
+ // @ts-ignore backward compatibility with treeshaking
4
+ export * from './reanimated1';
3
5
  export * from './reanimated2';
4
6
  export * as default from './Animated'; // If this line fails, you probably forgot some installation steps. Check out the installation guide here: https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/installation/ 1) Make sure reanimated's babel plugin is installed in your babel.config.js (you should have 'react-native-reanimated/plugin' listed there - also see the above link for details) 2) Make sure you reset build cache after updating the config, run: yarn start --reset-cache
@@ -3,33 +3,47 @@ import { shouldBeUseWeb } from './PlatformChecker';
3
3
  export function getTag(view) {
4
4
  return findNodeHandle(view);
5
5
  }
6
- const isNativeIndefined = shouldBeUseWeb();
6
+ const isNative = !shouldBeUseWeb();
7
7
  export function measure(animatedRef) {
8
8
  'worklet';
9
- if (!_WORKLET || isNativeIndefined) {
10
- console.warn('[reanimated.measure] method cannot be used for web or Chrome Debugger');
11
- return {
12
- x: NaN,
13
- y: NaN,
14
- width: NaN,
15
- height: NaN,
16
- pageX: NaN,
17
- pageY: NaN,
18
- };
9
+ if (!isNative) {
10
+ console.warn('[Reanimated] measure() cannot be used on web or Chrome Debugger');
11
+ return null;
12
+ }
13
+ if (!_WORKLET) {
14
+ console.warn('[Reanimated] measure() was called from the main JS context. Measure is ' +
15
+ 'only available in the UI runtime. This may also happen if measure() ' +
16
+ 'was called by a worklet in the useAnimatedStyle hook, because useAnimatedStyle ' +
17
+ 'calls the given worklet on the JS runtime during render. If you want to ' +
18
+ 'prevent this warning then wrap the call with `if (_WORKLET)`. Then it will ' +
19
+ 'only be called on the UI runtime after the render has been completed.');
20
+ return null;
19
21
  }
20
22
  const viewTag = animatedRef();
21
- const result = _measure(viewTag);
22
- if (result.x === -1234567) {
23
- throw new Error(`The view with tag ${viewTag} could not be measured`);
23
+ if (viewTag === -1) {
24
+ console.warn(`[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).`);
25
+ return null;
26
+ }
27
+ const measured = _measure(viewTag);
28
+ if (measured === null) {
29
+ console.warn(`[Reanimated] The view with tag ${viewTag} has some undefined, not-yet-computed or meaningless value of \`LayoutMetrics\` type. This may be because the view is not currently rendered, which may not be a bug (e.g. an off-screen FlatList item).`);
30
+ return null;
31
+ }
32
+ else if (measured.x === -1234567) {
33
+ console.warn(`[Reanimated] The view with tag ${viewTag} returned an invalid measurement response`);
34
+ return null;
35
+ }
36
+ else if (isNaN(measured.x)) {
37
+ console.warn(`[Reanimated] The view with tag ${viewTag} gets view-flattened on Android. To disable view-flattening, set \`collapsable={false}\` on this component.`);
38
+ return null;
24
39
  }
25
- if (isNaN(result.x)) {
26
- console.warn('Trying to measure a component which gets view-flattened on Android. To disable view-flattening, set `collapsable={false}` on this component.');
40
+ else {
41
+ return measured;
27
42
  }
28
- return result;
29
43
  }
30
44
  export function scrollTo(animatedRef, x, y, animated) {
31
45
  'worklet';
32
- if (!_WORKLET || isNativeIndefined) {
46
+ if (!_WORKLET || !isNative) {
33
47
  return;
34
48
  }
35
49
  const viewTag = animatedRef();
@@ -37,7 +51,7 @@ export function scrollTo(animatedRef, x, y, animated) {
37
51
  }
38
52
  export function setGestureState(handlerTag, newState) {
39
53
  'worklet';
40
- if (!_WORKLET || isNativeIndefined) {
54
+ if (!_WORKLET || !isNative) {
41
55
  console.warn('[Reanimated] You can not use setGestureState in non-worklet function.');
42
56
  return;
43
57
  }
@@ -17,16 +17,16 @@ export default class FrameCallbackRegistryJS {
17
17
  })();
18
18
  return callbackId;
19
19
  }
20
- unregisterFrameCallback(frameCallbackId) {
20
+ unregisterFrameCallback(callbackId) {
21
21
  runOnUI(() => {
22
22
  'worklet';
23
- global._frameCallbackRegistry.unregisterFrameCallback(frameCallbackId);
23
+ global._frameCallbackRegistry.unregisterFrameCallback(callbackId);
24
24
  })();
25
25
  }
26
- manageStateFrameCallback(frameCallbackId, state) {
26
+ manageStateFrameCallback(callbackId, state) {
27
27
  runOnUI(() => {
28
28
  'worklet';
29
- global._frameCallbackRegistry.manageStateFrameCallback(frameCallbackId, state);
29
+ global._frameCallbackRegistry.manageStateFrameCallback(callbackId, state);
30
30
  })();
31
31
  }
32
32
  }
@@ -3,43 +3,72 @@ export const prepareUIRegistry = runOnUI(() => {
3
3
  'worklet';
4
4
  const frameCallbackRegistry = {
5
5
  frameCallbackRegistry: new Map(),
6
- frameCallbackActive: new Set(),
7
- isFrameCallbackRunning: false,
6
+ activeFrameCallbacks: new Set(),
7
+ previousFrameTimestamp: null,
8
8
  runCallbacks() {
9
- const loop = () => {
10
- this.frameCallbackActive.forEach((key) => {
11
- const callback = this.frameCallbackRegistry.get(key);
12
- callback();
9
+ const loop = (timestamp) => {
10
+ if (this.previousFrameTimestamp === null) {
11
+ this.previousFrameTimestamp = timestamp;
12
+ }
13
+ const delta = timestamp - this.previousFrameTimestamp;
14
+ this.activeFrameCallbacks.forEach((callbackId) => {
15
+ const callbackDetails = this.frameCallbackRegistry.get(callbackId);
16
+ const { startTime } = callbackDetails;
17
+ if (startTime === null) {
18
+ // First frame
19
+ callbackDetails.startTime = timestamp;
20
+ callbackDetails.callback({
21
+ timestamp,
22
+ timeSincePreviousFrame: null,
23
+ timeSinceFirstFrame: 0,
24
+ });
25
+ }
26
+ else {
27
+ // Next frame
28
+ callbackDetails.callback({
29
+ timestamp,
30
+ timeSincePreviousFrame: delta,
31
+ timeSinceFirstFrame: timestamp - startTime,
32
+ });
33
+ }
13
34
  });
14
- if (this.frameCallbackActive.size > 0) {
35
+ if (this.activeFrameCallbacks.size > 0) {
36
+ this.previousFrameTimestamp = timestamp;
15
37
  requestAnimationFrame(loop);
16
38
  }
17
39
  else {
18
- this.isFrameCallbackRunning = false;
40
+ this.previousFrameTimestamp = null;
19
41
  }
20
42
  };
21
- if (!this.isFrameCallbackRunning) {
43
+ // runCallback() should only be called after registering a callback,
44
+ // so if there is only one active callback, then it means that there were
45
+ // zero previously and the loop isn't running yet.
46
+ if (this.activeFrameCallbacks.size === 1) {
22
47
  requestAnimationFrame(loop);
23
- this.isFrameCallbackRunning = true;
24
48
  }
25
49
  },
26
50
  registerFrameCallback(callback, callbackId) {
27
- this.frameCallbackRegistry.set(callbackId, callback);
51
+ this.frameCallbackRegistry.set(callbackId, {
52
+ callback: callback,
53
+ startTime: null,
54
+ });
28
55
  },
29
- unregisterFrameCallback(frameCallbackId) {
30
- this.manageStateFrameCallback(frameCallbackId, false);
31
- this.frameCallbackRegistry.delete(frameCallbackId);
56
+ unregisterFrameCallback(callbackId) {
57
+ this.manageStateFrameCallback(callbackId, false);
58
+ this.frameCallbackRegistry.delete(callbackId);
32
59
  },
33
- manageStateFrameCallback(frameCallbackId, state) {
34
- if (frameCallbackId === -1) {
60
+ manageStateFrameCallback(callbackId, state) {
61
+ if (callbackId === -1) {
35
62
  return;
36
63
  }
37
64
  if (state) {
38
- this.frameCallbackActive.add(frameCallbackId);
65
+ this.activeFrameCallbacks.add(callbackId);
39
66
  this.runCallbacks();
40
67
  }
41
68
  else {
42
- this.frameCallbackActive.delete(frameCallbackId);
69
+ const callback = this.frameCallbackRegistry.get(callbackId);
70
+ callback.startTime = null;
71
+ this.activeFrameCallbacks.delete(callbackId);
43
72
  }
44
73
  },
45
74
  };
@@ -20,7 +20,7 @@ declare global {
20
20
  name: string,
21
21
  updates: StyleProps | AnimatedStyle
22
22
  ) => void;
23
- const _measure: (viewTag: number) => MeasuredDimensions;
23
+ const _measure: (viewTag: number) => MeasuredDimensions | null;
24
24
  const _scrollTo: (
25
25
  viewTag: number,
26
26
  x: number,
@@ -40,7 +40,7 @@ declare global {
40
40
  _WORKLET: boolean;
41
41
  __reanimatedModuleProxy: NativeReanimated;
42
42
  _frameTimestamp: number | null;
43
- _measure: () => MeasuredDimensions;
43
+ _measure: () => MeasuredDimensions | null;
44
44
  _scrollTo: () => void;
45
45
  _chronoNow: () => number;
46
46
  performance: { now: () => number };
@@ -9,3 +9,4 @@ export { useDerivedValue } from './useDerivedValue';
9
9
  export { useAnimatedSensor, SensorType } from './useAnimatedSensor';
10
10
  export { useFrameCallback } from './useFrameCallback';
11
11
  export { useAnimatedKeyboard } from './useAnimatedKeyboard';
12
+ export { useScrollViewOffset } from './useScrollViewOffset';
@@ -0,0 +1,26 @@
1
+ import { useEffect, useRef } from 'react';
2
+ import { findNodeHandle } from 'react-native';
3
+ import { useEvent, useSharedValue } from '.';
4
+ const subscribeForEvents = [
5
+ 'onScroll',
6
+ 'onScrollBeginDrag',
7
+ 'onScrollEndDrag',
8
+ 'onMomentumScrollBegin',
9
+ 'onMomentumScrollEnd',
10
+ ];
11
+ export function useScrollViewOffset(aref) {
12
+ const offsetRef = useRef(useSharedValue(0));
13
+ const event = useEvent((event) => {
14
+ 'worklet';
15
+ offsetRef.current.value =
16
+ event.contentOffset.x === 0
17
+ ? event.contentOffset.y
18
+ : event.contentOffset.x;
19
+ }, subscribeForEvents);
20
+ useEffect(() => {
21
+ var _a;
22
+ const viewTag = findNodeHandle(aref.current);
23
+ (_a = event.current) === null || _a === void 0 ? void 0 : _a.registerForEvents(viewTag);
24
+ }, [aref.current]);
25
+ return offsetRef.current;
26
+ }
@@ -1,9 +1,16 @@
1
1
  import { measure } from './NativeMethods';
2
- export function getRelativeCoords(parentRef, x, y) {
2
+ /**
3
+ * Given an absolute position and a component ref, returns the relative
4
+ * position in the component's local coordinate space.
5
+ */
6
+ export function getRelativeCoords(parentRef, absoluteX, absoluteY) {
3
7
  'worklet';
4
8
  const parentCoords = measure(parentRef);
9
+ if (parentCoords === null) {
10
+ return null;
11
+ }
5
12
  return {
6
- x: x - parentCoords.x,
7
- y: y - parentCoords.y,
13
+ x: absoluteX - parentCoords.x,
14
+ y: absoluteY - parentCoords.y,
8
15
  };
9
16
  }
@@ -1,3 +1,4 @@
1
1
  import './reanimated2/js-reanimated/global';
2
+ export * from './reanimated1';
2
3
  export * from './reanimated2';
3
4
  export * as default from './Animated';
@@ -1,14 +1,7 @@
1
1
  import { Component } from 'react';
2
- import { RefObjectFunction } from './commonTypes';
2
+ import { MeasuredDimensions } from './commonTypes';
3
+ import { RefObjectFunction } from './hook/commonTypes';
3
4
  export declare function getTag(view: null | number | React.Component<any, any> | React.ComponentClass<any>): null | number;
4
- export interface MeasuredDimensions {
5
- x: number;
6
- y: number;
7
- width: number;
8
- height: number;
9
- pageX: number;
10
- pageY: number;
11
- }
12
- export declare function measure(animatedRef: RefObjectFunction<Component>): MeasuredDimensions;
5
+ export declare function measure(animatedRef: RefObjectFunction<Component>): MeasuredDimensions | null;
13
6
  export declare function scrollTo(animatedRef: RefObjectFunction<Component>, x: number, y: number, animated: boolean): void;
14
7
  export declare function setGestureState(handlerTag: number, newState: number): void;
@@ -89,3 +89,11 @@ export declare type AnimatedKeyboardInfo = {
89
89
  height: SharedValue<number>;
90
90
  state: SharedValue<KeyboardState>;
91
91
  };
92
+ export interface MeasuredDimensions {
93
+ x: number;
94
+ y: number;
95
+ width: number;
96
+ height: number;
97
+ pageX: number;
98
+ pageY: number;
99
+ }
@@ -1,7 +1,8 @@
1
+ import { FrameInfo } from './FrameCallbackRegistryUI';
1
2
  export default class FrameCallbackRegistryJS {
2
3
  private nextCallbackId;
3
4
  constructor();
4
- registerFrameCallback(callback: () => void): number;
5
- unregisterFrameCallback(frameCallbackId: number): void;
6
- manageStateFrameCallback(frameCallbackId: number, state: boolean): void;
5
+ registerFrameCallback(callback: (frameInfo: FrameInfo) => void): number;
6
+ unregisterFrameCallback(callbackId: number): void;
7
+ manageStateFrameCallback(callbackId: number, state: boolean): void;
7
8
  }
@@ -1,10 +1,6 @@
1
- export default interface FrameCallbackRegistryUI {
2
- frameCallbackRegistry: Map<number, () => void>;
3
- frameCallbackActive: Set<number>;
4
- isFrameCallbackRunning: boolean;
5
- runCallbacks: () => void;
6
- registerFrameCallback: (callback: () => void, callbackId: number) => void;
7
- unregisterFrameCallback: (frameCallbackId: number) => void;
8
- manageStateFrameCallback: (frameCallbackId: number, state: boolean) => void;
9
- }
1
+ export declare type FrameInfo = {
2
+ timestamp: number;
3
+ timeSincePreviousFrame: number | null;
4
+ timeSinceFirstFrame: number;
5
+ };
10
6
  export declare const prepareUIRegistry: () => void;
@@ -11,3 +11,4 @@ export { useAnimatedSensor, SensorType } from './useAnimatedSensor';
11
11
  export { useFrameCallback } from './useFrameCallback';
12
12
  export type { FrameCallback } from './useFrameCallback';
13
13
  export { useAnimatedKeyboard } from './useAnimatedKeyboard';
14
+ export { useScrollViewOffset } from './useScrollViewOffset';
@@ -6,7 +6,7 @@ import { Context, DependencyList } from './commonTypes';
6
6
  export interface ScrollHandler<TContext extends Context> extends WorkletFunction {
7
7
  (event: NativeScrollEvent, context?: TContext): void;
8
8
  }
9
- interface ScrollEvent extends NativeScrollEvent, NativeEvent<ScrollEvent> {
9
+ export interface ScrollEvent extends NativeScrollEvent, NativeEvent<ScrollEvent> {
10
10
  eventName: string;
11
11
  }
12
12
  export interface ScrollHandlers<TContext extends Context> {
@@ -18,4 +18,3 @@ export interface ScrollHandlers<TContext extends Context> {
18
18
  onMomentumEnd?: ScrollHandler<TContext>;
19
19
  }
20
20
  export declare function useAnimatedScrollHandler<TContext extends Context>(handlers: ScrollHandlers<TContext> | ScrollHandler<TContext>, dependencies?: DependencyList): RefObject<WorkletEventHandler<ScrollEvent>>;
21
- export {};
@@ -1,6 +1,7 @@
1
+ import { FrameInfo } from '../frameCallback/FrameCallbackRegistryUI';
1
2
  export declare type FrameCallback = {
2
3
  setActive: (isActive: boolean) => void;
3
4
  isActive: boolean;
4
5
  callbackId: number;
5
6
  };
6
- export declare function useFrameCallback(callback: () => void, autostart?: boolean): FrameCallback;
7
+ export declare function useFrameCallback(callback: (frameInfo: FrameInfo) => void, autostart?: boolean): FrameCallback;
@@ -0,0 +1,4 @@
1
+ import { RefObject } from 'react';
2
+ import type Animated from 'react-native-reanimated';
3
+ import { SharedValue } from '../commonTypes';
4
+ export declare function useScrollViewOffset(aref: RefObject<Animated.ScrollView>): SharedValue<number>;