react-native-reanimated 3.0.0-rc.9 → 3.0.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 (329) hide show
  1. package/Common/cpp/AnimatedSensor/AnimatedSensorModule.cpp +30 -12
  2. package/Common/cpp/AnimatedSensor/AnimatedSensorModule.h +2 -0
  3. package/Common/cpp/Fabric/FabricUtils.cpp +3 -107
  4. package/Common/cpp/Fabric/FabricUtils.h +3 -24
  5. package/Common/cpp/Fabric/ReanimatedUIManagerBinding.cpp +54 -37
  6. package/Common/cpp/LayoutAnimations/LayoutAnimationType.h +8 -0
  7. package/Common/cpp/LayoutAnimations/LayoutAnimationsManager.cpp +81 -26
  8. package/Common/cpp/LayoutAnimations/LayoutAnimationsManager.h +20 -3
  9. package/Common/cpp/NativeModules/NativeReanimatedModule.cpp +105 -109
  10. package/Common/cpp/NativeModules/NativeReanimatedModule.h +19 -12
  11. package/Common/cpp/NativeModules/NativeReanimatedModuleSpec.cpp +15 -7
  12. package/Common/cpp/NativeModules/NativeReanimatedModuleSpec.h +6 -2
  13. package/Common/cpp/ReanimatedRuntime/ReanimatedHermesRuntime.cpp +20 -1
  14. package/Common/cpp/ReanimatedRuntime/ReanimatedHermesRuntime.h +9 -0
  15. package/Common/cpp/ReanimatedRuntime/ReanimatedRuntime.cpp +5 -1
  16. package/Common/cpp/Registries/EventHandlerRegistry.cpp +8 -34
  17. package/Common/cpp/Registries/EventHandlerRegistry.h +7 -13
  18. package/Common/cpp/SharedItems/Shareables.cpp +9 -63
  19. package/Common/cpp/SharedItems/Shareables.h +77 -72
  20. package/Common/cpp/Tools/CollectionUtils.h +14 -0
  21. package/Common/cpp/Tools/JsiUtils.cpp +26 -0
  22. package/Common/cpp/Tools/JsiUtils.h +176 -0
  23. package/Common/cpp/Tools/PlatformDepMethodsHolder.h +5 -5
  24. package/Common/cpp/Tools/ReanimatedVersion.cpp +17 -0
  25. package/Common/cpp/Tools/ReanimatedVersion.h +11 -0
  26. package/Common/cpp/Tools/RuntimeDecorator.cpp +49 -230
  27. package/Common/cpp/Tools/RuntimeDecorator.h +0 -2
  28. package/Common/cpp/Tools/WorkletEventHandler.cpp +3 -2
  29. package/Common/cpp/Tools/WorkletEventHandler.h +14 -6
  30. package/RNReanimated.podspec +2 -1
  31. package/android/CMakeLists.txt +188 -181
  32. package/android/build.gradle +362 -311
  33. package/android/proguard-rules.pro +2 -0
  34. package/android/src/fabric/java/com/swmansion/reanimated/NativeProxy.java +63 -295
  35. package/android/src/main/cpp/LayoutAnimations.cpp +35 -5
  36. package/android/src/main/cpp/LayoutAnimations.h +23 -6
  37. package/android/src/main/cpp/NativeProxy.cpp +143 -98
  38. package/android/src/main/cpp/NativeProxy.h +22 -28
  39. package/android/src/main/java/com/swmansion/reanimated/ReanimatedPackage.java +5 -4
  40. package/android/src/main/java/com/swmansion/reanimated/Utils.java +7 -1
  41. package/android/src/main/java/com/swmansion/reanimated/keyboardObserver/ReanimatedKeyboardEventListener.java +10 -3
  42. package/android/src/main/java/com/swmansion/reanimated/layoutReanimation/AnimationsManager.java +218 -35
  43. package/android/src/main/java/com/swmansion/reanimated/layoutReanimation/LayoutAnimations.java +43 -17
  44. package/android/src/main/java/com/swmansion/reanimated/layoutReanimation/NativeMethodsHolder.java +6 -2
  45. package/android/src/main/java/com/swmansion/reanimated/layoutReanimation/SharedElement.java +19 -0
  46. package/android/src/main/java/com/swmansion/reanimated/layoutReanimation/SharedTransitionManager.java +610 -0
  47. package/android/src/main/java/com/swmansion/reanimated/layoutReanimation/Snapshot.java +93 -23
  48. package/android/src/main/java/com/swmansion/reanimated/nativeProxy/AnimationFrameCallback.java +19 -0
  49. package/android/src/main/java/com/swmansion/reanimated/nativeProxy/EventHandler.java +35 -0
  50. package/android/src/main/java/com/swmansion/reanimated/nativeProxy/KeyboardEventDataUpdater.java +16 -0
  51. package/android/src/main/java/com/swmansion/reanimated/nativeProxy/NativeProxyCommon.java +206 -0
  52. package/android/src/main/java/com/swmansion/reanimated/nativeProxy/SensorSetter.java +17 -0
  53. package/android/src/main/java/com/swmansion/reanimated/sensor/ReanimatedSensor.java +9 -3
  54. package/android/src/main/java/com/swmansion/reanimated/sensor/ReanimatedSensorContainer.java +3 -4
  55. package/android/src/main/java/com/swmansion/reanimated/sensor/ReanimatedSensorListener.java +31 -7
  56. package/android/src/paper/java/com/swmansion/reanimated/NativeProxy.java +90 -307
  57. package/android/src/reactNativeVersionPatch/ReanimatedUIManager/70/com/swmansion/reanimated/ReanimatedUIManagerFactory.java +16 -0
  58. package/android/src/reactNativeVersionPatch/ReanimatedUIManager/latest/com/swmansion/reanimated/ReanimatedUIManagerFactory.java +63 -0
  59. package/android/src/reactNativeVersionPatch/ReanimatedUIManager/latest/com/swmansion/reanimated/layoutReanimation/ReanimatedUIManager.java +271 -0
  60. package/android/src/reactNativeVersionPatch/nativeHierarchyManager/latest/com/swmansion/reanimated/layoutReanimation/ReanimatedNativeHierarchyManager.java +76 -20
  61. package/ios/LayoutReanimation/REAAnimationsManager.h +28 -4
  62. package/ios/LayoutReanimation/REAAnimationsManager.m +216 -61
  63. package/ios/LayoutReanimation/REAFrame.h +10 -0
  64. package/ios/LayoutReanimation/REAFrame.m +15 -0
  65. package/ios/LayoutReanimation/REAScreensHelper.h +19 -0
  66. package/ios/LayoutReanimation/REAScreensHelper.m +106 -0
  67. package/ios/LayoutReanimation/REASharedElement.h +15 -0
  68. package/ios/LayoutReanimation/REASharedElement.m +16 -0
  69. package/ios/LayoutReanimation/REASharedTransitionManager.h +17 -0
  70. package/ios/LayoutReanimation/REASharedTransitionManager.m +632 -0
  71. package/ios/LayoutReanimation/REASnapshot.h +1 -0
  72. package/ios/LayoutReanimation/REASnapshot.m +96 -11
  73. package/ios/LayoutReanimation/REAUIManager.mm +64 -17
  74. package/ios/REAModule.mm +2 -7
  75. package/ios/REANodesManager.mm +1 -21
  76. package/ios/keyboardObserver/REAKeyboardEventObserver.m +72 -36
  77. package/ios/native/NativeProxy.mm +75 -114
  78. package/ios/native/REAInitializer.mm +2 -7
  79. package/ios/native/REAJSIUtils.h +60 -2
  80. package/ios/sensor/ReanimatedSensor.h +6 -2
  81. package/ios/sensor/ReanimatedSensor.m +43 -7
  82. package/ios/sensor/ReanimatedSensorContainer.h +4 -1
  83. package/ios/sensor/ReanimatedSensorContainer.m +8 -2
  84. package/lib/commonjs/createAnimatedComponent.js +232 -28
  85. package/lib/commonjs/createAnimatedComponent.js.map +1 -1
  86. package/lib/commonjs/reanimated2/Colors.js +43 -22
  87. package/lib/commonjs/reanimated2/Colors.js.map +1 -1
  88. package/lib/commonjs/reanimated2/NativeReanimated/NativeReanimated.js +16 -33
  89. package/lib/commonjs/reanimated2/NativeReanimated/NativeReanimated.js.map +1 -1
  90. package/lib/commonjs/reanimated2/animation/styleAnimation.js +5 -1
  91. package/lib/commonjs/reanimated2/animation/styleAnimation.js.map +1 -1
  92. package/lib/commonjs/reanimated2/animation/util.js +17 -15
  93. package/lib/commonjs/reanimated2/animation/util.js.map +1 -1
  94. package/lib/commonjs/reanimated2/commonTypes.js +33 -1
  95. package/lib/commonjs/reanimated2/commonTypes.js.map +1 -1
  96. package/lib/commonjs/reanimated2/core.js +33 -14
  97. package/lib/commonjs/reanimated2/core.js.map +1 -1
  98. package/lib/commonjs/reanimated2/frameCallback/FrameCallbackRegistryUI.js +2 -2
  99. package/lib/commonjs/reanimated2/frameCallback/FrameCallbackRegistryUI.js.map +1 -1
  100. package/lib/commonjs/reanimated2/globals.d.js.map +1 -1
  101. package/lib/commonjs/reanimated2/hook/index.js +0 -6
  102. package/lib/commonjs/reanimated2/hook/index.js.map +1 -1
  103. package/lib/commonjs/reanimated2/hook/useAnimatedKeyboard.js +5 -2
  104. package/lib/commonjs/reanimated2/hook/useAnimatedKeyboard.js.map +1 -1
  105. package/lib/commonjs/reanimated2/hook/useAnimatedReaction.js +13 -1
  106. package/lib/commonjs/reanimated2/hook/useAnimatedReaction.js.map +1 -1
  107. package/lib/commonjs/reanimated2/hook/useAnimatedRef.js +1 -1
  108. package/lib/commonjs/reanimated2/hook/useAnimatedRef.js.map +1 -1
  109. package/lib/commonjs/reanimated2/hook/useAnimatedSensor.js +89 -16
  110. package/lib/commonjs/reanimated2/hook/useAnimatedSensor.js.map +1 -1
  111. package/lib/commonjs/reanimated2/hook/useAnimatedStyle.js +43 -40
  112. package/lib/commonjs/reanimated2/hook/useAnimatedStyle.js.map +1 -1
  113. package/lib/commonjs/reanimated2/hook/useDerivedValue.js +13 -1
  114. package/lib/commonjs/reanimated2/hook/useDerivedValue.js.map +1 -1
  115. package/lib/commonjs/reanimated2/hook/useScrollViewOffset.js +2 -2
  116. package/lib/commonjs/reanimated2/hook/useScrollViewOffset.js.map +1 -1
  117. package/lib/commonjs/reanimated2/hook/utils.js +18 -65
  118. package/lib/commonjs/reanimated2/hook/utils.js.map +1 -1
  119. package/lib/commonjs/reanimated2/index.js +13 -0
  120. package/lib/commonjs/reanimated2/index.js.map +1 -1
  121. package/lib/commonjs/reanimated2/initializers.js +71 -32
  122. package/lib/commonjs/reanimated2/initializers.js.map +1 -1
  123. package/lib/commonjs/reanimated2/interpolateColor.js +96 -19
  124. package/lib/commonjs/reanimated2/interpolateColor.js.map +1 -1
  125. package/lib/commonjs/reanimated2/jestUtils.js +1 -1
  126. package/lib/commonjs/reanimated2/jestUtils.js.map +1 -1
  127. package/lib/commonjs/reanimated2/js-reanimated/JSReanimated.js +103 -13
  128. package/lib/commonjs/reanimated2/js-reanimated/JSReanimated.js.map +1 -1
  129. package/lib/commonjs/reanimated2/js-reanimated/Mapper.js +22 -0
  130. package/lib/commonjs/reanimated2/js-reanimated/Mapper.js.map +1 -1
  131. package/lib/commonjs/reanimated2/js-reanimated/WebSensor.js +2 -0
  132. package/lib/commonjs/reanimated2/js-reanimated/WebSensor.js.map +1 -0
  133. package/lib/commonjs/reanimated2/js-reanimated/commonTypes.js.map +1 -1
  134. package/lib/commonjs/reanimated2/js-reanimated/global.js +0 -2
  135. package/lib/commonjs/reanimated2/js-reanimated/global.js.map +1 -1
  136. package/lib/commonjs/reanimated2/js-reanimated/index.js +0 -1
  137. package/lib/commonjs/reanimated2/js-reanimated/index.js.map +1 -1
  138. package/lib/commonjs/reanimated2/layoutReanimation/animationBuilder/commonTypes.js +11 -0
  139. package/lib/commonjs/reanimated2/layoutReanimation/animationBuilder/commonTypes.js.map +1 -1
  140. package/lib/commonjs/reanimated2/layoutReanimation/animationBuilder/index.js +8 -0
  141. package/lib/commonjs/reanimated2/layoutReanimation/animationBuilder/index.js.map +1 -1
  142. package/lib/commonjs/reanimated2/layoutReanimation/animationsManager.js +17 -9
  143. package/lib/commonjs/reanimated2/layoutReanimation/animationsManager.js.map +1 -1
  144. package/lib/commonjs/reanimated2/layoutReanimation/defaultAnimations/Bounce.js +16 -23
  145. package/lib/commonjs/reanimated2/layoutReanimation/defaultAnimations/Bounce.js.map +1 -1
  146. package/lib/commonjs/reanimated2/layoutReanimation/defaultAnimations/Lightspeed.js +8 -14
  147. package/lib/commonjs/reanimated2/layoutReanimation/defaultAnimations/Lightspeed.js.map +1 -1
  148. package/lib/commonjs/reanimated2/layoutReanimation/defaultAnimations/Roll.js +8 -14
  149. package/lib/commonjs/reanimated2/layoutReanimation/defaultAnimations/Roll.js.map +1 -1
  150. package/lib/commonjs/reanimated2/layoutReanimation/defaultAnimations/Slide.js +8 -15
  151. package/lib/commonjs/reanimated2/layoutReanimation/defaultAnimations/Slide.js.map +1 -1
  152. package/lib/commonjs/reanimated2/layoutReanimation/defaultAnimations/Zoom.js +16 -23
  153. package/lib/commonjs/reanimated2/layoutReanimation/defaultAnimations/Zoom.js.map +1 -1
  154. package/lib/commonjs/reanimated2/layoutReanimation/index.js +13 -0
  155. package/lib/commonjs/reanimated2/layoutReanimation/index.js.map +1 -1
  156. package/lib/commonjs/reanimated2/layoutReanimation/sharedTransitions/index.js +94 -0
  157. package/lib/commonjs/reanimated2/layoutReanimation/sharedTransitions/index.js.map +1 -0
  158. package/lib/commonjs/reanimated2/mappers.js +18 -6
  159. package/lib/commonjs/reanimated2/mappers.js.map +1 -1
  160. package/lib/commonjs/reanimated2/mock.js +20 -1
  161. package/lib/commonjs/reanimated2/mock.js.map +1 -1
  162. package/lib/commonjs/reanimated2/mutables.js +4 -2
  163. package/lib/commonjs/reanimated2/mutables.js.map +1 -1
  164. package/lib/commonjs/reanimated2/platform-specific/checkVersion.js +42 -0
  165. package/lib/commonjs/reanimated2/platform-specific/checkVersion.js.map +1 -0
  166. package/lib/commonjs/reanimated2/platform-specific/checkVersion.web.js +15 -0
  167. package/lib/commonjs/reanimated2/platform-specific/checkVersion.web.js.map +1 -0
  168. package/lib/commonjs/reanimated2/pluginUtils.js +11 -0
  169. package/lib/commonjs/reanimated2/pluginUtils.js.map +1 -0
  170. package/lib/commonjs/reanimated2/shareables.js +36 -15
  171. package/lib/commonjs/reanimated2/shareables.js.map +1 -1
  172. package/lib/commonjs/reanimated2/threads.js +114 -4
  173. package/lib/commonjs/reanimated2/threads.js.map +1 -1
  174. package/lib/commonjs/reanimated2/utils.js +7 -0
  175. package/lib/commonjs/reanimated2/utils.js.map +1 -1
  176. package/lib/commonjs/reanimated2/valueSetter.js +1 -3
  177. package/lib/commonjs/reanimated2/valueSetter.js.map +1 -1
  178. package/lib/module/createAnimatedComponent.js +229 -29
  179. package/lib/module/createAnimatedComponent.js.map +1 -1
  180. package/lib/module/reanimated2/Colors.js +35 -19
  181. package/lib/module/reanimated2/Colors.js.map +1 -1
  182. package/lib/module/reanimated2/NativeReanimated/NativeReanimated.js +16 -32
  183. package/lib/module/reanimated2/NativeReanimated/NativeReanimated.js.map +1 -1
  184. package/lib/module/reanimated2/animation/styleAnimation.js +5 -1
  185. package/lib/module/reanimated2/animation/styleAnimation.js.map +1 -1
  186. package/lib/module/reanimated2/animation/util.js +17 -16
  187. package/lib/module/reanimated2/animation/util.js.map +1 -1
  188. package/lib/module/reanimated2/commonTypes.js +29 -0
  189. package/lib/module/reanimated2/commonTypes.js.map +1 -1
  190. package/lib/module/reanimated2/core.js +32 -7
  191. package/lib/module/reanimated2/core.js.map +1 -1
  192. package/lib/module/reanimated2/frameCallback/FrameCallbackRegistryUI.js +2 -2
  193. package/lib/module/reanimated2/frameCallback/FrameCallbackRegistryUI.js.map +1 -1
  194. package/lib/module/reanimated2/globals.d.js.map +1 -1
  195. package/lib/module/reanimated2/hook/index.js +1 -1
  196. package/lib/module/reanimated2/hook/index.js.map +1 -1
  197. package/lib/module/reanimated2/hook/useAnimatedKeyboard.js +5 -2
  198. package/lib/module/reanimated2/hook/useAnimatedKeyboard.js.map +1 -1
  199. package/lib/module/reanimated2/hook/useAnimatedReaction.js +12 -1
  200. package/lib/module/reanimated2/hook/useAnimatedReaction.js.map +1 -1
  201. package/lib/module/reanimated2/hook/useAnimatedRef.js +2 -2
  202. package/lib/module/reanimated2/hook/useAnimatedRef.js.map +1 -1
  203. package/lib/module/reanimated2/hook/useAnimatedSensor.js +88 -13
  204. package/lib/module/reanimated2/hook/useAnimatedSensor.js.map +1 -1
  205. package/lib/module/reanimated2/hook/useAnimatedStyle.js +46 -43
  206. package/lib/module/reanimated2/hook/useAnimatedStyle.js.map +1 -1
  207. package/lib/module/reanimated2/hook/useDerivedValue.js +12 -1
  208. package/lib/module/reanimated2/hook/useDerivedValue.js.map +1 -1
  209. package/lib/module/reanimated2/hook/useScrollViewOffset.js +2 -2
  210. package/lib/module/reanimated2/hook/useScrollViewOffset.js.map +1 -1
  211. package/lib/module/reanimated2/hook/utils.js +17 -60
  212. package/lib/module/reanimated2/hook/utils.js.map +1 -1
  213. package/lib/module/reanimated2/index.js +1 -0
  214. package/lib/module/reanimated2/index.js.map +1 -1
  215. package/lib/module/reanimated2/initializers.js +72 -33
  216. package/lib/module/reanimated2/initializers.js.map +1 -1
  217. package/lib/module/reanimated2/interpolateColor.js +96 -19
  218. package/lib/module/reanimated2/interpolateColor.js.map +1 -1
  219. package/lib/module/reanimated2/jestUtils.js +1 -1
  220. package/lib/module/reanimated2/jestUtils.js.map +1 -1
  221. package/lib/module/reanimated2/js-reanimated/JSReanimated.js +103 -13
  222. package/lib/module/reanimated2/js-reanimated/JSReanimated.js.map +1 -1
  223. package/lib/module/reanimated2/js-reanimated/Mapper.js +21 -0
  224. package/lib/module/reanimated2/js-reanimated/Mapper.js.map +1 -1
  225. package/lib/module/reanimated2/js-reanimated/WebSensor.js +2 -0
  226. package/lib/module/reanimated2/js-reanimated/WebSensor.js.map +1 -0
  227. package/lib/module/reanimated2/js-reanimated/commonTypes.js.map +1 -1
  228. package/lib/module/reanimated2/js-reanimated/global.js +0 -2
  229. package/lib/module/reanimated2/js-reanimated/global.js.map +1 -1
  230. package/lib/module/reanimated2/js-reanimated/index.js +0 -1
  231. package/lib/module/reanimated2/js-reanimated/index.js.map +1 -1
  232. package/lib/module/reanimated2/layoutReanimation/animationBuilder/commonTypes.js +9 -1
  233. package/lib/module/reanimated2/layoutReanimation/animationBuilder/commonTypes.js.map +1 -1
  234. package/lib/module/reanimated2/layoutReanimation/animationBuilder/index.js +1 -0
  235. package/lib/module/reanimated2/layoutReanimation/animationBuilder/index.js.map +1 -1
  236. package/lib/module/reanimated2/layoutReanimation/animationsManager.js +15 -8
  237. package/lib/module/reanimated2/layoutReanimation/animationsManager.js.map +1 -1
  238. package/lib/module/reanimated2/layoutReanimation/defaultAnimations/Bounce.js +16 -21
  239. package/lib/module/reanimated2/layoutReanimation/defaultAnimations/Bounce.js.map +1 -1
  240. package/lib/module/reanimated2/layoutReanimation/defaultAnimations/Lightspeed.js +8 -12
  241. package/lib/module/reanimated2/layoutReanimation/defaultAnimations/Lightspeed.js.map +1 -1
  242. package/lib/module/reanimated2/layoutReanimation/defaultAnimations/Roll.js +8 -12
  243. package/lib/module/reanimated2/layoutReanimation/defaultAnimations/Roll.js.map +1 -1
  244. package/lib/module/reanimated2/layoutReanimation/defaultAnimations/Slide.js +8 -13
  245. package/lib/module/reanimated2/layoutReanimation/defaultAnimations/Slide.js.map +1 -1
  246. package/lib/module/reanimated2/layoutReanimation/defaultAnimations/Zoom.js +16 -21
  247. package/lib/module/reanimated2/layoutReanimation/defaultAnimations/Zoom.js.map +1 -1
  248. package/lib/module/reanimated2/layoutReanimation/index.js +1 -0
  249. package/lib/module/reanimated2/layoutReanimation/index.js.map +1 -1
  250. package/lib/module/reanimated2/layoutReanimation/sharedTransitions/index.js +82 -0
  251. package/lib/module/reanimated2/layoutReanimation/sharedTransitions/index.js.map +1 -0
  252. package/lib/module/reanimated2/mappers.js +16 -6
  253. package/lib/module/reanimated2/mappers.js.map +1 -1
  254. package/lib/module/reanimated2/mock.js +20 -1
  255. package/lib/module/reanimated2/mock.js.map +1 -1
  256. package/lib/module/reanimated2/mutables.js +4 -2
  257. package/lib/module/reanimated2/mutables.js.map +1 -1
  258. package/lib/module/reanimated2/platform-specific/checkVersion.js +35 -0
  259. package/lib/module/reanimated2/platform-specific/checkVersion.js.map +1 -0
  260. package/lib/module/reanimated2/platform-specific/checkVersion.web.js +8 -0
  261. package/lib/module/reanimated2/platform-specific/checkVersion.web.js.map +1 -0
  262. package/lib/module/reanimated2/pluginUtils.js +4 -0
  263. package/lib/module/reanimated2/pluginUtils.js.map +1 -0
  264. package/lib/module/reanimated2/shareables.js +36 -12
  265. package/lib/module/reanimated2/shareables.js.map +1 -1
  266. package/lib/module/reanimated2/threads.js +105 -4
  267. package/lib/module/reanimated2/threads.js.map +1 -1
  268. package/lib/module/reanimated2/utils.js +5 -0
  269. package/lib/module/reanimated2/utils.js.map +1 -1
  270. package/lib/module/reanimated2/valueSetter.js +1 -2
  271. package/lib/module/reanimated2/valueSetter.js.map +1 -1
  272. package/package.json +10 -10
  273. package/{plugin.js → plugin/index.js} +128 -249
  274. package/react-native-reanimated.d.ts +57 -10
  275. package/scripts/reanimated_utils.rb +15 -2
  276. package/src/createAnimatedComponent.tsx +238 -28
  277. package/src/reanimated2/Colors.ts +38 -11
  278. package/src/reanimated2/NativeReanimated/NativeReanimated.ts +52 -39
  279. package/src/reanimated2/animation/styleAnimation.ts +8 -1
  280. package/src/reanimated2/animation/util.ts +29 -16
  281. package/src/reanimated2/commonTypes.ts +28 -1
  282. package/src/reanimated2/core.ts +42 -9
  283. package/src/reanimated2/frameCallback/FrameCallbackRegistryUI.ts +2 -2
  284. package/src/reanimated2/globals.d.ts +12 -15
  285. package/src/reanimated2/hook/index.ts +1 -1
  286. package/src/reanimated2/hook/useAnimatedKeyboard.ts +10 -4
  287. package/src/reanimated2/hook/useAnimatedReaction.ts +12 -5
  288. package/src/reanimated2/hook/useAnimatedRef.ts +1 -4
  289. package/src/reanimated2/hook/useAnimatedSensor.ts +88 -11
  290. package/src/reanimated2/hook/useAnimatedStyle.ts +44 -36
  291. package/src/reanimated2/hook/useDerivedValue.ts +8 -1
  292. package/src/reanimated2/hook/useScrollViewOffset.ts +2 -2
  293. package/src/reanimated2/hook/utils.ts +15 -61
  294. package/src/reanimated2/index.ts +1 -0
  295. package/src/reanimated2/initializers.ts +76 -35
  296. package/src/reanimated2/interpolateColor.ts +104 -20
  297. package/src/reanimated2/jestUtils.ts +1 -1
  298. package/src/reanimated2/js-reanimated/JSReanimated.ts +98 -15
  299. package/src/reanimated2/js-reanimated/Mapper.ts +32 -0
  300. package/src/reanimated2/js-reanimated/WebSensor.ts +34 -0
  301. package/src/reanimated2/js-reanimated/commonTypes.ts +0 -1
  302. package/src/reanimated2/js-reanimated/global.ts +0 -1
  303. package/src/reanimated2/js-reanimated/index.ts +0 -1
  304. package/src/reanimated2/layoutReanimation/animationBuilder/commonTypes.ts +13 -1
  305. package/src/reanimated2/layoutReanimation/animationBuilder/index.ts +1 -0
  306. package/src/reanimated2/layoutReanimation/animationsManager.ts +17 -9
  307. package/src/reanimated2/layoutReanimation/defaultAnimations/Bounce.ts +16 -19
  308. package/src/reanimated2/layoutReanimation/defaultAnimations/Lightspeed.ts +14 -11
  309. package/src/reanimated2/layoutReanimation/defaultAnimations/Roll.ts +21 -11
  310. package/src/reanimated2/layoutReanimation/defaultAnimations/Slide.ts +32 -11
  311. package/src/reanimated2/layoutReanimation/defaultAnimations/Zoom.ts +36 -19
  312. package/src/reanimated2/layoutReanimation/index.ts +1 -0
  313. package/src/reanimated2/layoutReanimation/sharedTransitions/index.ts +87 -0
  314. package/src/reanimated2/mappers.ts +17 -6
  315. package/src/reanimated2/mock.ts +24 -1
  316. package/src/reanimated2/mutables.ts +2 -0
  317. package/src/reanimated2/platform-specific/checkVersion.ts +39 -0
  318. package/src/reanimated2/platform-specific/checkVersion.web.ts +6 -0
  319. package/src/reanimated2/pluginUtils.ts +8 -0
  320. package/src/reanimated2/shareables.ts +45 -16
  321. package/src/reanimated2/threads.ts +103 -1
  322. package/src/reanimated2/utils.ts +6 -0
  323. package/src/reanimated2/valueSetter.ts +1 -2
  324. package/lib/commonjs/reanimated2/time.js +0 -55
  325. package/lib/commonjs/reanimated2/time.js.map +0 -1
  326. package/lib/module/reanimated2/time.js +0 -37
  327. package/lib/module/reanimated2/time.js.map +0 -1
  328. package/src/reanimated2/time.ts +0 -30
  329. /package/android/src/{main/java → reactNativeVersionPatch/ReanimatedUIManager/70}/com/swmansion/reanimated/layoutReanimation/ReanimatedUIManager.java +0 -0
@@ -1,17 +1,17 @@
1
1
  import { useEffect, useRef } from 'react';
2
2
  import { makeMutable, registerSensor, unregisterSensor } from '../core';
3
- import { SharedValue, Value3D, ValueRotation } from '../commonTypes';
4
-
5
- export enum SensorType {
6
- ACCELEROMETER = 1,
7
- GYROSCOPE = 2,
8
- GRAVITY = 3,
9
- MAGNETIC_FIELD = 4,
10
- ROTATION = 5,
11
- }
3
+ import {
4
+ SensorType,
5
+ SharedValue,
6
+ Value3D,
7
+ ValueRotation,
8
+ IOSReferenceFrame,
9
+ } from '../commonTypes';
12
10
 
13
11
  export type SensorConfig = {
14
12
  interval: number | 'auto';
13
+ adjustToInterfaceOrientation: boolean;
14
+ iosReferenceFrame: IOSReferenceFrame;
15
15
  };
16
16
 
17
17
  export type AnimatedSensor = {
@@ -33,19 +33,81 @@ function initSensorData(
33
33
  yaw: 0,
34
34
  pitch: 0,
35
35
  roll: 0,
36
+ interfaceOrientation: 0,
36
37
  });
37
38
  } else {
38
39
  return makeMutable<Value3D | ValueRotation>({
39
40
  x: 0,
40
41
  y: 0,
41
42
  z: 0,
43
+ interfaceOrientation: 0,
42
44
  });
43
45
  }
44
46
  }
45
47
 
48
+ // euler angles are in order ZXY, z = yaw, x = pitch, y = roll
49
+ // https://github.com/mrdoob/three.js/blob/dev/src/math/Quaternion.js#L237
50
+ function eulerToQuaternion(pitch: number, roll: number, yaw: number) {
51
+ 'worklet';
52
+ const c1 = Math.cos(pitch / 2);
53
+ const s1 = Math.sin(pitch / 2);
54
+ const c2 = Math.cos(roll / 2);
55
+ const s2 = Math.sin(roll / 2);
56
+ const c3 = Math.cos(yaw / 2);
57
+ const s3 = Math.sin(yaw / 2);
58
+
59
+ return [
60
+ s1 * c2 * c3 - c1 * s2 * s3,
61
+ c1 * s2 * c3 + s1 * c2 * s3,
62
+ c1 * c2 * s3 + s1 * s2 * c3,
63
+ c1 * c2 * c3 - s1 * s2 * s3,
64
+ ];
65
+ }
66
+
67
+ function adjustRotationToInterfaceOrientation(data: ValueRotation) {
68
+ 'worklet';
69
+ const { interfaceOrientation, pitch, roll, yaw } = data;
70
+ if (interfaceOrientation === 90) {
71
+ data.pitch = roll;
72
+ data.roll = -pitch;
73
+ data.yaw = yaw - Math.PI / 2;
74
+ } else if (interfaceOrientation === 270) {
75
+ data.pitch = -roll;
76
+ data.roll = pitch;
77
+ data.yaw = yaw + Math.PI / 2;
78
+ } else if (interfaceOrientation === 180) {
79
+ data.pitch *= -1;
80
+ data.roll *= -1;
81
+ data.yaw *= -1;
82
+ }
83
+
84
+ const q = eulerToQuaternion(data.pitch, data.roll, data.yaw);
85
+ data.qx = q[0];
86
+ data.qy = q[1];
87
+ data.qz = q[2];
88
+ data.qw = q[3];
89
+ return data;
90
+ }
91
+
92
+ function adjustVectorToInterfaceOrientation(data: Value3D) {
93
+ 'worklet';
94
+ const { interfaceOrientation, x, y } = data;
95
+ if (interfaceOrientation === 90) {
96
+ data.x = -y;
97
+ data.y = x;
98
+ } else if (interfaceOrientation === 270) {
99
+ data.x = y;
100
+ data.y = -x;
101
+ } else if (interfaceOrientation === 180) {
102
+ data.x *= -1;
103
+ data.y *= -1;
104
+ }
105
+ return data;
106
+ }
107
+
46
108
  export function useAnimatedSensor(
47
109
  sensorType: SensorType,
48
- userConfig?: SensorConfig
110
+ userConfig?: Partial<SensorConfig>
49
111
  ): AnimatedSensor {
50
112
  const ref = useRef<AnimatedSensor>({
51
113
  sensor: initSensorData(sensorType),
@@ -55,17 +117,32 @@ export function useAnimatedSensor(
55
117
  isAvailable: false,
56
118
  config: {
57
119
  interval: 0,
120
+ adjustToInterfaceOrientation: true,
121
+ iosReferenceFrame: IOSReferenceFrame.Auto,
58
122
  },
59
123
  });
60
124
 
61
125
  useEffect(() => {
62
- ref.current.config = { interval: 'auto', ...userConfig };
126
+ ref.current.config = {
127
+ interval: 'auto',
128
+ adjustToInterfaceOrientation: true,
129
+ iosReferenceFrame: IOSReferenceFrame.Auto,
130
+ ...userConfig,
131
+ };
63
132
  const sensorData = ref.current.sensor!;
64
133
  const id = registerSensor(
65
134
  sensorType,
66
135
  ref.current.config.interval === 'auto' ? -1 : ref.current.config.interval,
136
+ ref.current.config.iosReferenceFrame,
67
137
  (data) => {
68
138
  'worklet';
139
+ if (ref.current.config.adjustToInterfaceOrientation) {
140
+ if (sensorType === SensorType.ROTATION) {
141
+ data = adjustRotationToInterfaceOrientation(data as ValueRotation);
142
+ } else {
143
+ data = adjustVectorToInterfaceOrientation(data as Value3D);
144
+ }
145
+ }
69
146
  sensorData.value = data;
70
147
  }
71
148
  );
@@ -1,16 +1,14 @@
1
- /* global _frameTimestamp */
2
1
  import { MutableRefObject, useEffect, useRef } from 'react';
3
2
 
4
- import { startMapper, stopMapper, makeRemote, getTimestamp } from '../core';
3
+ import { startMapper, stopMapper, makeRemote } from '../core';
5
4
  import updateProps, { updatePropsJestWrapper } from '../UpdateProps';
6
5
  import { initialUpdaterRun } from '../animation';
7
6
  import NativeReanimatedModule from '../NativeReanimated';
8
7
  import { useSharedValue } from './useSharedValue';
9
8
  import {
10
9
  buildWorkletsHash,
11
- getStyleWithoutAnimations,
12
10
  isAnimated,
13
- styleDiff,
11
+ shallowEqual,
14
12
  validateAnimatedStyles,
15
13
  } from './utils';
16
14
  import { DependencyList, Descriptor } from './commonTypes';
@@ -20,7 +18,7 @@ import {
20
18
  ViewDescriptorsSet,
21
19
  ViewRefSet,
22
20
  } from '../ViewDescriptorsSet';
23
- import { isJest } from '../PlatformChecker';
21
+ import { isJest, shouldBeUseWeb } from '../PlatformChecker';
24
22
  import {
25
23
  AnimationObject,
26
24
  Timestamp,
@@ -30,6 +28,7 @@ import {
30
28
  BasicWorkletFunctionOptional,
31
29
  NestedObjectValues,
32
30
  SharedValue,
31
+ StyleProps,
33
32
  } from '../commonTypes';
34
33
  export interface AnimatedStyleResult {
35
34
  viewDescriptors: ViewDescriptorsSet;
@@ -55,6 +54,7 @@ interface AnimationRef {
55
54
  }
56
55
 
57
56
  function prepareAnimation(
57
+ frameTimestamp: number,
58
58
  animatedProp: AnimatedStyle,
59
59
  lastAnimation: AnimatedStyle,
60
60
  lastValue: AnimatedStyle
@@ -63,6 +63,7 @@ function prepareAnimation(
63
63
  if (Array.isArray(animatedProp)) {
64
64
  animatedProp.forEach((prop, index) => {
65
65
  prepareAnimation(
66
+ frameTimestamp,
66
67
  prop,
67
68
  lastAnimation && lastAnimation[index],
68
69
  lastValue && lastValue[index]
@@ -97,12 +98,13 @@ function prepareAnimation(
97
98
  animation.callStart = (timestamp: Timestamp) => {
98
99
  animation.onStart(animation, value, timestamp, lastAnimation);
99
100
  };
100
- animation.callStart(getTimestamp());
101
+ animation.callStart(frameTimestamp);
101
102
  animation.callStart = null;
102
103
  } else if (typeof animatedProp === 'object') {
103
104
  // it is an object
104
105
  Object.keys(animatedProp).forEach((key) =>
105
106
  prepareAnimation(
107
+ frameTimestamp,
106
108
  animatedProp[key],
107
109
  lastAnimation && lastAnimation[key],
108
110
  lastValue && lastValue[key]
@@ -183,23 +185,28 @@ function styleUpdater(
183
185
  const animations = state.animations ?? {};
184
186
  const newValues = updater() ?? {};
185
187
  const oldValues = state.last;
188
+ const nonAnimatedNewValues: StyleProps = {};
186
189
 
187
190
  let hasAnimations = false;
191
+ let frameTimestamp: number | undefined;
192
+ let hasNonAnimatedValues = false;
188
193
  for (const key in newValues) {
189
194
  const value = newValues[key];
190
195
  if (isAnimated(value)) {
191
- prepareAnimation(value, animations[key], oldValues[key]);
196
+ frameTimestamp = global.__frameTimestamp || performance.now();
197
+ prepareAnimation(frameTimestamp, value, animations[key], oldValues[key]);
192
198
  animations[key] = value;
193
199
  hasAnimations = true;
194
200
  } else {
201
+ hasNonAnimatedValues = true;
202
+ nonAnimatedNewValues[key] = value;
195
203
  delete animations[key];
196
204
  }
197
205
  }
198
206
 
199
207
  if (hasAnimations) {
200
- const frame = (_timestamp?: Timestamp) => {
208
+ const frame = (timestamp: Timestamp) => {
201
209
  const { animations, last, isAnimationCancelled } = state;
202
- const timestamp = _timestamp ?? getTimestamp();
203
210
  if (isAnimationCancelled) {
204
211
  state.isAnimationRunning = false;
205
212
  return;
@@ -238,27 +245,21 @@ function styleUpdater(
238
245
  if (!state.isAnimationRunning) {
239
246
  state.isAnimationCancelled = false;
240
247
  state.isAnimationRunning = true;
241
- if (_frameTimestamp) {
242
- frame(_frameTimestamp);
243
- } else {
244
- requestAnimationFrame(frame);
245
- }
248
+ frame(frameTimestamp!);
246
249
  }
247
- state.last = Object.assign({}, oldValues, newValues);
248
- const style = getStyleWithoutAnimations(state.last);
249
- if (style) {
250
- updateProps(viewDescriptors, style, maybeViewRef);
250
+
251
+ if (hasNonAnimatedValues) {
252
+ updateProps(viewDescriptors, nonAnimatedNewValues, maybeViewRef);
251
253
  }
252
254
  } else {
253
255
  state.isAnimationCancelled = true;
254
256
  state.animations = [];
255
257
 
256
- const diff = styleDiff(oldValues, newValues);
257
- state.last = Object.assign({}, oldValues, newValues);
258
- if (diff) {
258
+ if (!shallowEqual(oldValues, newValues)) {
259
259
  updateProps(viewDescriptors, newValues, maybeViewRef);
260
260
  }
261
261
  }
262
+ state.last = newValues;
262
263
  }
263
264
 
264
265
  function jestStyleUpdater(
@@ -277,6 +278,7 @@ function jestStyleUpdater(
277
278
 
278
279
  // extract animated props
279
280
  let hasAnimations = false;
281
+ let frameTimestamp: number | undefined;
280
282
  Object.keys(animations).forEach((key) => {
281
283
  const value = newValues[key];
282
284
  if (!isAnimated(value)) {
@@ -286,15 +288,15 @@ function jestStyleUpdater(
286
288
  Object.keys(newValues).forEach((key) => {
287
289
  const value = newValues[key];
288
290
  if (isAnimated(value)) {
289
- prepareAnimation(value, animations[key], oldValues[key]);
291
+ frameTimestamp = global.__frameTimestamp || performance.now();
292
+ prepareAnimation(frameTimestamp, value, animations[key], oldValues[key]);
290
293
  animations[key] = value;
291
294
  hasAnimations = true;
292
295
  }
293
296
  });
294
297
 
295
- function frame(_timestamp?: Timestamp) {
298
+ function frame(timestamp: Timestamp) {
296
299
  const { animations, last, isAnimationCancelled } = state;
297
- const timestamp = _timestamp ?? getTimestamp();
298
300
  if (isAnimationCancelled) {
299
301
  state.isAnimationRunning = false;
300
302
  return;
@@ -340,11 +342,7 @@ function jestStyleUpdater(
340
342
  if (!state.isAnimationRunning) {
341
343
  state.isAnimationCancelled = false;
342
344
  state.isAnimationRunning = true;
343
- if (_frameTimestamp) {
344
- frame(_frameTimestamp);
345
- } else {
346
- requestAnimationFrame(frame);
347
- }
345
+ frame(frameTimestamp!);
348
346
  }
349
347
  } else {
350
348
  state.isAnimationCancelled = true;
@@ -352,13 +350,12 @@ function jestStyleUpdater(
352
350
  }
353
351
 
354
352
  // calculate diff
355
- const diff = styleDiff(oldValues, newValues);
356
- state.last = Object.assign({}, oldValues, newValues);
353
+ state.last = newValues;
357
354
 
358
- if (Object.keys(diff).length !== 0) {
355
+ if (!shallowEqual(oldValues, newValues)) {
359
356
  updatePropsJestWrapper(
360
357
  viewDescriptors,
361
- diff,
358
+ newValues,
362
359
  maybeViewRef,
363
360
  animatedStyle,
364
361
  adapters
@@ -400,7 +397,20 @@ export function useAnimatedStyle<T extends AnimatedStyle>(
400
397
  ): AnimatedStyleResult {
401
398
  const viewsRef: ViewRefSet<any> = makeViewsRefSet();
402
399
  const initRef = useRef<AnimationRef>();
403
- const inputs = Object.values(updater._closure ?? {});
400
+ let inputs = Object.values(updater._closure ?? {});
401
+ if (shouldBeUseWeb()) {
402
+ if (!inputs.length && dependencies?.length) {
403
+ // let web work without a Babel/SWC plugin
404
+ inputs = dependencies;
405
+ }
406
+ if (__DEV__ && !inputs.length && !dependencies && !updater.__workletHash) {
407
+ throw new Error(
408
+ `useAnimatedStyle was used without a dependency array or Babel plugin. Please explicitly pass a dependency array, or enable the Babel/SWC plugin.
409
+
410
+ For more, see the docs: https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/web-support#web-without-a-babel-plugin`
411
+ );
412
+ }
413
+ }
404
414
  const adaptersArray: AdapterWorkletFunction[] = adapters
405
415
  ? Array.isArray(adapters)
406
416
  ? adapters
@@ -493,8 +503,6 @@ export function useAnimatedStyle<T extends AnimatedStyle>(
493
503
  useEffect(() => {
494
504
  animationsActive.value = true;
495
505
  return () => {
496
- // initRef.current = null;
497
- // viewsRef = null;
498
506
  animationsActive.value = false;
499
507
  };
500
508
  }, []);
@@ -3,6 +3,7 @@ import { initialUpdaterRun } from '../animation';
3
3
  import { BasicWorkletFunction, SharedValue } from '../commonTypes';
4
4
  import { makeMutable, startMapper, stopMapper } from '../core';
5
5
  import { DependencyList } from './commonTypes';
6
+ import { shouldBeUseWeb } from '../PlatformChecker';
6
7
 
7
8
  export type DerivedValue<T> = Readonly<SharedValue<T>>;
8
9
 
@@ -11,7 +12,13 @@ export function useDerivedValue<T>(
11
12
  dependencies: DependencyList
12
13
  ): DerivedValue<T> {
13
14
  const initRef = useRef<SharedValue<T> | null>(null);
14
- const inputs = Object.values(processor._closure ?? {});
15
+ let inputs = Object.values(processor._closure ?? {});
16
+ if (shouldBeUseWeb()) {
17
+ if (!inputs.length && dependencies?.length) {
18
+ // let web work without a Babel/SWC plugin
19
+ inputs = dependencies;
20
+ }
21
+ }
15
22
 
16
23
  // build dependencies
17
24
  if (dependencies === undefined) {
@@ -7,7 +7,7 @@ import { findNodeHandle } from 'react-native';
7
7
  import { useEvent } from './utils';
8
8
  import { useSharedValue } from './useSharedValue';
9
9
 
10
- const subscribeForEvents = [
10
+ const scrollEventNames = [
11
11
  'onScroll',
12
12
  'onScrollBeginDrag',
13
13
  'onScrollEndDrag',
@@ -26,7 +26,7 @@ export function useScrollViewOffset(
26
26
  event.contentOffset.x === 0
27
27
  ? event.contentOffset.y
28
28
  : event.contentOffset.x;
29
- }, subscribeForEvents);
29
+ }, scrollEventNames);
30
30
 
31
31
  useEffect(() => {
32
32
  const viewTag = findNodeHandle(aref.current);
@@ -5,7 +5,6 @@ import {
5
5
  Context,
6
6
  NativeEvent,
7
7
  NestedObjectValues,
8
- StyleProps,
9
8
  WorkletFunction,
10
9
  AnimationObject,
11
10
  } from '../commonTypes';
@@ -164,78 +163,33 @@ export function parseColors(updates: AnimatedStyle): void {
164
163
  }
165
164
  }
166
165
 
167
- export function canApplyOptimalisation(upadterFn: WorkletFunction): number {
168
- const FUNCTIONLESS_FLAG = 0b00000001;
169
- const STATEMENTLESS_FLAG = 0b00000010;
170
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
171
- const optimalization = upadterFn.__optimalization!;
172
- return (
173
- optimalization & FUNCTIONLESS_FLAG && optimalization & STATEMENTLESS_FLAG
174
- );
175
- }
176
-
177
166
  export function isAnimated(prop: NestedObjectValues<AnimationObject>): boolean {
178
167
  'worklet';
179
- const propsToCheck: NestedObjectValues<AnimationObject>[] = [prop];
180
- while (propsToCheck.length > 0) {
181
- const currentProp: NestedObjectValues<AnimationObject> =
182
- propsToCheck.pop() as NestedObjectValues<AnimationObject>;
183
- if (Array.isArray(currentProp)) {
184
- for (const item of currentProp) {
185
- propsToCheck.push(item);
186
- }
187
- } else if (currentProp?.onFrame !== undefined) {
168
+ if (Array.isArray(prop)) {
169
+ return prop.some(isAnimated);
170
+ } else if (typeof prop === 'object') {
171
+ if (prop.onFrame !== undefined) {
188
172
  return true;
189
- } else if (typeof currentProp === 'object') {
190
- for (const item of Object.values(currentProp)) {
191
- propsToCheck.push(item);
192
- }
173
+ } else {
174
+ return Object.values(prop).some(isAnimated);
193
175
  }
194
- // if none of the above, it's not the animated prop, check next one
195
176
  }
196
-
197
- // when none of the props were animated return false
198
177
  return false;
199
178
  }
200
179
 
201
- export function styleDiff<T extends AnimatedStyle>(
202
- oldStyle: AnimatedStyle,
203
- newStyle: AnimatedStyle
204
- ): Partial<T> {
180
+ export function shallowEqual(a: any, b: any) {
205
181
  'worklet';
206
- const diff: any = {};
207
- for (const key in oldStyle) {
208
- if (newStyle[key] === undefined) {
209
- diff[key] = null;
210
- }
211
- }
212
- for (const key in newStyle) {
213
- const value = newStyle[key];
214
- const oldValue = oldStyle[key];
215
-
216
- if (isAnimated(value)) {
217
- // do nothing
218
- continue;
219
- }
220
- if (oldValue !== value) {
221
- diff[key] = value;
222
- }
182
+ const aKeys = Object.keys(a);
183
+ const bKeys = Object.keys(b);
184
+ if (aKeys.length !== bKeys.length) {
185
+ return false;
223
186
  }
224
- return diff;
225
- }
226
-
227
- export function getStyleWithoutAnimations(newStyle: AnimatedStyle): StyleProps {
228
- 'worklet';
229
- const diff: StyleProps = {};
230
-
231
- for (const key in newStyle) {
232
- const value = newStyle[key];
233
- if (isAnimated(value)) {
234
- continue;
187
+ for (let i = 0; i < aKeys.length; i++) {
188
+ if (a[aKeys[i]] !== b[aKeys[i]]) {
189
+ return false;
235
190
  }
236
- diff[key] = value;
237
191
  }
238
- return diff;
192
+ return true;
239
193
  }
240
194
 
241
195
  export const validateAnimatedStyles = (styles: AnimatedStyle): void => {
@@ -10,3 +10,4 @@ export * from './PropAdapters';
10
10
  export * from './layoutReanimation';
11
11
  export * from './utils';
12
12
  export * from './commonTypes';
13
+ export * from './pluginUtils';
@@ -1,7 +1,12 @@
1
1
  import { reportFatalErrorOnJS } from './errors';
2
2
  import NativeReanimatedModule from './NativeReanimated';
3
3
  import { isJest } from './PlatformChecker';
4
- import { runOnUI, runOnJS } from './threads';
4
+ import {
5
+ runOnJS,
6
+ setupSetImmediate,
7
+ flushImmediates,
8
+ runOnUIImmediately,
9
+ } from './threads';
5
10
 
6
11
  // callGuard is only used with debug builds
7
12
  function callGuardDEV<T extends Array<any>, U>(
@@ -29,18 +34,20 @@ function valueUnpacker(objectToUnpack: any, category?: string): any {
29
34
  workletsCache = global.__workletsCache = new Map();
30
35
  handleCache = global.__handleCache = new WeakMap();
31
36
  }
32
- if (objectToUnpack.__workletHash) {
33
- let workletFun = workletsCache.get(objectToUnpack.__workletHash);
37
+ const workletHash = objectToUnpack.__workletHash;
38
+ if (workletHash !== undefined) {
39
+ let workletFun = workletsCache.get(workletHash);
34
40
  if (workletFun === undefined) {
41
+ const initData = objectToUnpack.__initData;
35
42
  if (global.evalWithSourceMap) {
36
43
  // if the runtime (hermes only for now) supports loading source maps
37
44
  // we want to use the proper filename for the location as it guarantees
38
45
  // that debugger understands and loads the source code of the file where
39
46
  // the worklet is defined.
40
47
  workletFun = global.evalWithSourceMap(
41
- '(' + objectToUnpack.asString + '\n)',
42
- objectToUnpack.__location,
43
- objectToUnpack.__sourceMap
48
+ '(' + initData.code + '\n)',
49
+ initData.location,
50
+ initData.sourceMap
44
51
  ) as (...args: any[]) => any;
45
52
  } else if (global.evalWithSourceUrl) {
46
53
  // if the runtime doesn't support loading source maps, in dev mode we
@@ -48,17 +55,17 @@ function valueUnpacker(objectToUnpack: any, category?: string): any {
48
55
  // the actual file location we use worklet hash, as it the allows us to
49
56
  // properly symbolicate traces (see errors.ts for details)
50
57
  workletFun = global.evalWithSourceUrl(
51
- '(' + objectToUnpack.asString + '\n)',
52
- `worklet_${objectToUnpack.__workletHash}`
58
+ '(' + initData.code + '\n)',
59
+ `worklet_${workletHash}`
53
60
  ) as (...args: any[]) => any;
54
61
  } else {
55
62
  // in release we use the regular eval to save on JSI calls
56
63
  // eslint-disable-next-line no-eval
57
- workletFun = eval('(' + objectToUnpack.asString + '\n)') as (
64
+ workletFun = eval('(' + initData.code + '\n)') as (
58
65
  ...args: any[]
59
66
  ) => any;
60
67
  }
61
- workletsCache.set(objectToUnpack.__workletHash, workletFun);
68
+ workletsCache.set(workletHash, workletFun);
62
69
  }
63
70
  const functionInstance = workletFun.bind(objectToUnpack);
64
71
  objectToUnpack._recur = functionInstance;
@@ -85,13 +92,69 @@ Possible solutions are:
85
92
  }
86
93
  }
87
94
 
95
+ function setupRequestAnimationFrame() {
96
+ 'worklet';
97
+
98
+ // Jest mocks requestAnimationFrame API and it does not like if that mock gets overridden
99
+ // so we avoid doing requestAnimationFrame batching in Jest environment.
100
+ const nativeRequestAnimationFrame = global.requestAnimationFrame;
101
+
102
+ let animationFrameCallbacks: Array<(timestamp: number) => void> = [];
103
+ let lastNativeAnimationFrameTimestamp = -1;
104
+
105
+ global.__flushAnimationFrame = (frameTimestamp: number) => {
106
+ const currentCallbacks = animationFrameCallbacks;
107
+ animationFrameCallbacks = [];
108
+ currentCallbacks.forEach((f) => f(frameTimestamp));
109
+ flushImmediates();
110
+ };
111
+
112
+ global.requestAnimationFrame = (
113
+ callback: (timestamp: number) => void
114
+ ): number => {
115
+ animationFrameCallbacks.push(callback);
116
+ if (animationFrameCallbacks.length === 1) {
117
+ // We schedule native requestAnimationFrame only when the first callback
118
+ // is added and then use it to execute all the enqueued callbacks. Once
119
+ // the callbacks are run, we clear the array.
120
+ nativeRequestAnimationFrame((timestamp) => {
121
+ if (lastNativeAnimationFrameTimestamp >= timestamp) {
122
+ // Make sure we only execute the callbacks once for a given frame
123
+ return;
124
+ }
125
+ lastNativeAnimationFrameTimestamp = timestamp;
126
+ global.__frameTimestamp = timestamp;
127
+ global.__flushAnimationFrame(timestamp);
128
+ global.__frameTimestamp = undefined;
129
+ });
130
+ }
131
+ // Reanimated currently does not support cancelling calbacks requested with
132
+ // requestAnimationFrame. We return -1 as identifier which isn't in line
133
+ // with the spec but it should give users better clue in case they actually
134
+ // attempt to store the value returned from rAF and use it for cancelling.
135
+ return -1;
136
+ };
137
+ }
138
+
88
139
  export function initializeUIRuntime() {
89
140
  NativeReanimatedModule.installCoreFunctions(callGuardDEV, valueUnpacker);
90
141
 
91
142
  const IS_JEST = isJest();
92
143
 
144
+ if (IS_JEST) {
145
+ // requestAnimationFrame react-native jest's setup is incorrect as it polyfills
146
+ // the method directly using setTimeout, therefore the callback doesn't get the
147
+ // expected timestamp as the only argument: https://github.com/facebook/react-native/blob/main/jest/setup.js#L28
148
+ // We override this setup here to make sure that callbacks get the proper timestamps
149
+ // when executed. For non-jest environments we define requestAnimationFrame in setupRequestAnimationFrame
150
+ // @ts-ignore TypeScript uses Node definition for rAF, setTimeout, etc which returns a Timeout object rather than a number
151
+ global.requestAnimationFrame = (callback: (timestamp: number) => void) => {
152
+ return setTimeout(() => callback(performance.now()), 0);
153
+ };
154
+ }
155
+
93
156
  const capturableConsole = console;
94
- runOnUI(() => {
157
+ runOnUIImmediately(() => {
95
158
  'worklet';
96
159
  // setup error handler
97
160
  global.ErrorUtils = {
@@ -114,30 +177,8 @@ export function initializeUIRuntime() {
114
177
  };
115
178
 
116
179
  if (!IS_JEST) {
117
- // Jest mocks requestAnimationFrame API and it does not like if that mock gets overridden
118
- // so we avoid doing requestAnimationFrame batching in Jest environment.
119
- const nativeRequestAnimationFrame = global.requestAnimationFrame;
120
- let callbacks: Array<(time: number) => void> = [];
121
- global.requestAnimationFrame = (
122
- callback: (timestamp: number) => void
123
- ): number => {
124
- callbacks.push(callback);
125
- if (callbacks.length === 1) {
126
- // We schedule native requestAnimationFrame only when the first callback
127
- // is added and then use it to execute all the enqueued callbacks. Once
128
- // the callbacks are run, we clear the array.
129
- nativeRequestAnimationFrame((timestamp) => {
130
- const currentCallbacks = callbacks;
131
- callbacks = [];
132
- currentCallbacks.forEach((f) => f(timestamp));
133
- });
134
- }
135
- // Reanimated currently does not support cancelling calbacks requested with
136
- // requestAnimationFrame. We return -1 as identifier which isn't in line
137
- // with the spec but it should give users better clue in case they actually
138
- // attempt to store the value returned from rAF and use it for cancelling.
139
- return -1;
140
- };
180
+ setupSetImmediate();
181
+ setupRequestAnimationFrame();
141
182
  }
142
183
  })();
143
184
  }