react-native-reanimated 3.0.0-rc.8 → 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 (339) 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 +122 -121
  10. package/Common/cpp/NativeModules/NativeReanimatedModule.h +21 -14
  11. package/Common/cpp/NativeModules/NativeReanimatedModuleSpec.cpp +15 -7
  12. package/Common/cpp/NativeModules/NativeReanimatedModuleSpec.h +8 -4
  13. package/Common/cpp/ReanimatedRuntime/ReanimatedHermesRuntime.cpp +50 -1
  14. package/Common/cpp/ReanimatedRuntime/ReanimatedHermesRuntime.h +10 -1
  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 +11 -63
  19. package/Common/cpp/SharedItems/Shareables.h +113 -93
  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 +67 -247
  27. package/Common/cpp/Tools/RuntimeDecorator.h +2 -3
  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 +31 -5
  62. package/ios/LayoutReanimation/REAAnimationsManager.m +265 -77
  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 +67 -60
  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 +18 -35
  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/component/FlatList.js +19 -3
  97. package/lib/commonjs/reanimated2/component/FlatList.js.map +1 -1
  98. package/lib/commonjs/reanimated2/core.js +34 -83
  99. package/lib/commonjs/reanimated2/core.js.map +1 -1
  100. package/lib/commonjs/reanimated2/errors.js +66 -0
  101. package/lib/commonjs/reanimated2/errors.js.map +1 -0
  102. package/lib/commonjs/reanimated2/frameCallback/FrameCallbackRegistryUI.js +2 -2
  103. package/lib/commonjs/reanimated2/frameCallback/FrameCallbackRegistryUI.js.map +1 -1
  104. package/lib/commonjs/reanimated2/globals.d.js.map +1 -1
  105. package/lib/commonjs/reanimated2/hook/index.js +0 -6
  106. package/lib/commonjs/reanimated2/hook/index.js.map +1 -1
  107. package/lib/commonjs/reanimated2/hook/useAnimatedKeyboard.js +5 -2
  108. package/lib/commonjs/reanimated2/hook/useAnimatedKeyboard.js.map +1 -1
  109. package/lib/commonjs/reanimated2/hook/useAnimatedReaction.js +13 -1
  110. package/lib/commonjs/reanimated2/hook/useAnimatedReaction.js.map +1 -1
  111. package/lib/commonjs/reanimated2/hook/useAnimatedRef.js +1 -1
  112. package/lib/commonjs/reanimated2/hook/useAnimatedRef.js.map +1 -1
  113. package/lib/commonjs/reanimated2/hook/useAnimatedSensor.js +89 -16
  114. package/lib/commonjs/reanimated2/hook/useAnimatedSensor.js.map +1 -1
  115. package/lib/commonjs/reanimated2/hook/useAnimatedStyle.js +43 -50
  116. package/lib/commonjs/reanimated2/hook/useAnimatedStyle.js.map +1 -1
  117. package/lib/commonjs/reanimated2/hook/useDerivedValue.js +13 -1
  118. package/lib/commonjs/reanimated2/hook/useDerivedValue.js.map +1 -1
  119. package/lib/commonjs/reanimated2/hook/useScrollViewOffset.js +2 -2
  120. package/lib/commonjs/reanimated2/hook/useScrollViewOffset.js.map +1 -1
  121. package/lib/commonjs/reanimated2/hook/utils.js +18 -65
  122. package/lib/commonjs/reanimated2/hook/utils.js.map +1 -1
  123. package/lib/commonjs/reanimated2/index.js +13 -0
  124. package/lib/commonjs/reanimated2/index.js.map +1 -1
  125. package/lib/commonjs/reanimated2/initializers.js +196 -0
  126. package/lib/commonjs/reanimated2/initializers.js.map +1 -0
  127. package/lib/commonjs/reanimated2/interpolateColor.js +96 -19
  128. package/lib/commonjs/reanimated2/interpolateColor.js.map +1 -1
  129. package/lib/commonjs/reanimated2/jestUtils.js +1 -1
  130. package/lib/commonjs/reanimated2/jestUtils.js.map +1 -1
  131. package/lib/commonjs/reanimated2/js-reanimated/JSReanimated.js +101 -16
  132. package/lib/commonjs/reanimated2/js-reanimated/JSReanimated.js.map +1 -1
  133. package/lib/commonjs/reanimated2/js-reanimated/Mapper.js +22 -0
  134. package/lib/commonjs/reanimated2/js-reanimated/Mapper.js.map +1 -1
  135. package/lib/commonjs/reanimated2/js-reanimated/WebSensor.js +2 -0
  136. package/lib/commonjs/reanimated2/js-reanimated/WebSensor.js.map +1 -0
  137. package/lib/commonjs/reanimated2/js-reanimated/commonTypes.js.map +1 -1
  138. package/lib/commonjs/reanimated2/js-reanimated/global.js +4 -9
  139. package/lib/commonjs/reanimated2/js-reanimated/global.js.map +1 -1
  140. package/lib/commonjs/reanimated2/js-reanimated/index.js +0 -1
  141. package/lib/commonjs/reanimated2/js-reanimated/index.js.map +1 -1
  142. package/lib/commonjs/reanimated2/layoutReanimation/animationBuilder/commonTypes.js +11 -0
  143. package/lib/commonjs/reanimated2/layoutReanimation/animationBuilder/commonTypes.js.map +1 -1
  144. package/lib/commonjs/reanimated2/layoutReanimation/animationBuilder/index.js +8 -0
  145. package/lib/commonjs/reanimated2/layoutReanimation/animationBuilder/index.js.map +1 -1
  146. package/lib/commonjs/reanimated2/layoutReanimation/animationsManager.js +16 -12
  147. package/lib/commonjs/reanimated2/layoutReanimation/animationsManager.js.map +1 -1
  148. package/lib/commonjs/reanimated2/layoutReanimation/defaultAnimations/Bounce.js +16 -23
  149. package/lib/commonjs/reanimated2/layoutReanimation/defaultAnimations/Bounce.js.map +1 -1
  150. package/lib/commonjs/reanimated2/layoutReanimation/defaultAnimations/Lightspeed.js +8 -14
  151. package/lib/commonjs/reanimated2/layoutReanimation/defaultAnimations/Lightspeed.js.map +1 -1
  152. package/lib/commonjs/reanimated2/layoutReanimation/defaultAnimations/Roll.js +8 -14
  153. package/lib/commonjs/reanimated2/layoutReanimation/defaultAnimations/Roll.js.map +1 -1
  154. package/lib/commonjs/reanimated2/layoutReanimation/defaultAnimations/Slide.js +8 -15
  155. package/lib/commonjs/reanimated2/layoutReanimation/defaultAnimations/Slide.js.map +1 -1
  156. package/lib/commonjs/reanimated2/layoutReanimation/defaultAnimations/Zoom.js +16 -23
  157. package/lib/commonjs/reanimated2/layoutReanimation/defaultAnimations/Zoom.js.map +1 -1
  158. package/lib/commonjs/reanimated2/layoutReanimation/index.js +13 -0
  159. package/lib/commonjs/reanimated2/layoutReanimation/index.js.map +1 -1
  160. package/lib/commonjs/reanimated2/layoutReanimation/sharedTransitions/index.js +94 -0
  161. package/lib/commonjs/reanimated2/layoutReanimation/sharedTransitions/index.js.map +1 -0
  162. package/lib/commonjs/reanimated2/mappers.js +18 -6
  163. package/lib/commonjs/reanimated2/mappers.js.map +1 -1
  164. package/lib/commonjs/reanimated2/mock.js +45 -0
  165. package/lib/commonjs/reanimated2/mock.js.map +1 -1
  166. package/lib/commonjs/reanimated2/mutables.js +4 -2
  167. package/lib/commonjs/reanimated2/mutables.js.map +1 -1
  168. package/lib/commonjs/reanimated2/platform-specific/checkVersion.js +42 -0
  169. package/lib/commonjs/reanimated2/platform-specific/checkVersion.js.map +1 -0
  170. package/lib/commonjs/reanimated2/platform-specific/checkVersion.web.js +15 -0
  171. package/lib/commonjs/reanimated2/platform-specific/checkVersion.web.js.map +1 -0
  172. package/lib/commonjs/reanimated2/pluginUtils.js +11 -0
  173. package/lib/commonjs/reanimated2/pluginUtils.js.map +1 -0
  174. package/lib/commonjs/reanimated2/shareables.js +38 -11
  175. package/lib/commonjs/reanimated2/shareables.js.map +1 -1
  176. package/lib/commonjs/reanimated2/threads.js +114 -4
  177. package/lib/commonjs/reanimated2/threads.js.map +1 -1
  178. package/lib/commonjs/reanimated2/utils.js +7 -0
  179. package/lib/commonjs/reanimated2/utils.js.map +1 -1
  180. package/lib/commonjs/reanimated2/valueSetter.js +1 -3
  181. package/lib/commonjs/reanimated2/valueSetter.js.map +1 -1
  182. package/lib/module/createAnimatedComponent.js +229 -29
  183. package/lib/module/createAnimatedComponent.js.map +1 -1
  184. package/lib/module/reanimated2/Colors.js +35 -19
  185. package/lib/module/reanimated2/Colors.js.map +1 -1
  186. package/lib/module/reanimated2/NativeReanimated/NativeReanimated.js +18 -34
  187. package/lib/module/reanimated2/NativeReanimated/NativeReanimated.js.map +1 -1
  188. package/lib/module/reanimated2/animation/styleAnimation.js +5 -1
  189. package/lib/module/reanimated2/animation/styleAnimation.js.map +1 -1
  190. package/lib/module/reanimated2/animation/util.js +17 -16
  191. package/lib/module/reanimated2/animation/util.js.map +1 -1
  192. package/lib/module/reanimated2/commonTypes.js +29 -0
  193. package/lib/module/reanimated2/commonTypes.js.map +1 -1
  194. package/lib/module/reanimated2/component/FlatList.js +18 -4
  195. package/lib/module/reanimated2/component/FlatList.js.map +1 -1
  196. package/lib/module/reanimated2/core.js +30 -76
  197. package/lib/module/reanimated2/core.js.map +1 -1
  198. package/lib/module/reanimated2/errors.js +58 -0
  199. package/lib/module/reanimated2/errors.js.map +1 -0
  200. package/lib/module/reanimated2/frameCallback/FrameCallbackRegistryUI.js +2 -2
  201. package/lib/module/reanimated2/frameCallback/FrameCallbackRegistryUI.js.map +1 -1
  202. package/lib/module/reanimated2/globals.d.js.map +1 -1
  203. package/lib/module/reanimated2/hook/index.js +1 -1
  204. package/lib/module/reanimated2/hook/index.js.map +1 -1
  205. package/lib/module/reanimated2/hook/useAnimatedKeyboard.js +5 -2
  206. package/lib/module/reanimated2/hook/useAnimatedKeyboard.js.map +1 -1
  207. package/lib/module/reanimated2/hook/useAnimatedReaction.js +12 -1
  208. package/lib/module/reanimated2/hook/useAnimatedReaction.js.map +1 -1
  209. package/lib/module/reanimated2/hook/useAnimatedRef.js +2 -2
  210. package/lib/module/reanimated2/hook/useAnimatedRef.js.map +1 -1
  211. package/lib/module/reanimated2/hook/useAnimatedSensor.js +88 -13
  212. package/lib/module/reanimated2/hook/useAnimatedSensor.js.map +1 -1
  213. package/lib/module/reanimated2/hook/useAnimatedStyle.js +45 -52
  214. package/lib/module/reanimated2/hook/useAnimatedStyle.js.map +1 -1
  215. package/lib/module/reanimated2/hook/useDerivedValue.js +12 -1
  216. package/lib/module/reanimated2/hook/useDerivedValue.js.map +1 -1
  217. package/lib/module/reanimated2/hook/useScrollViewOffset.js +2 -2
  218. package/lib/module/reanimated2/hook/useScrollViewOffset.js.map +1 -1
  219. package/lib/module/reanimated2/hook/utils.js +17 -60
  220. package/lib/module/reanimated2/hook/utils.js.map +1 -1
  221. package/lib/module/reanimated2/index.js +1 -0
  222. package/lib/module/reanimated2/index.js.map +1 -1
  223. package/lib/module/reanimated2/initializers.js +182 -0
  224. package/lib/module/reanimated2/initializers.js.map +1 -0
  225. package/lib/module/reanimated2/interpolateColor.js +96 -19
  226. package/lib/module/reanimated2/interpolateColor.js.map +1 -1
  227. package/lib/module/reanimated2/jestUtils.js +1 -1
  228. package/lib/module/reanimated2/jestUtils.js.map +1 -1
  229. package/lib/module/reanimated2/js-reanimated/JSReanimated.js +101 -16
  230. package/lib/module/reanimated2/js-reanimated/JSReanimated.js.map +1 -1
  231. package/lib/module/reanimated2/js-reanimated/Mapper.js +21 -0
  232. package/lib/module/reanimated2/js-reanimated/Mapper.js.map +1 -1
  233. package/lib/module/reanimated2/js-reanimated/WebSensor.js +2 -0
  234. package/lib/module/reanimated2/js-reanimated/WebSensor.js.map +1 -0
  235. package/lib/module/reanimated2/js-reanimated/commonTypes.js.map +1 -1
  236. package/lib/module/reanimated2/js-reanimated/global.js +4 -9
  237. package/lib/module/reanimated2/js-reanimated/global.js.map +1 -1
  238. package/lib/module/reanimated2/js-reanimated/index.js +0 -1
  239. package/lib/module/reanimated2/js-reanimated/index.js.map +1 -1
  240. package/lib/module/reanimated2/layoutReanimation/animationBuilder/commonTypes.js +9 -1
  241. package/lib/module/reanimated2/layoutReanimation/animationBuilder/commonTypes.js.map +1 -1
  242. package/lib/module/reanimated2/layoutReanimation/animationBuilder/index.js +1 -0
  243. package/lib/module/reanimated2/layoutReanimation/animationBuilder/index.js.map +1 -1
  244. package/lib/module/reanimated2/layoutReanimation/animationsManager.js +15 -10
  245. package/lib/module/reanimated2/layoutReanimation/animationsManager.js.map +1 -1
  246. package/lib/module/reanimated2/layoutReanimation/defaultAnimations/Bounce.js +16 -21
  247. package/lib/module/reanimated2/layoutReanimation/defaultAnimations/Bounce.js.map +1 -1
  248. package/lib/module/reanimated2/layoutReanimation/defaultAnimations/Lightspeed.js +8 -12
  249. package/lib/module/reanimated2/layoutReanimation/defaultAnimations/Lightspeed.js.map +1 -1
  250. package/lib/module/reanimated2/layoutReanimation/defaultAnimations/Roll.js +8 -12
  251. package/lib/module/reanimated2/layoutReanimation/defaultAnimations/Roll.js.map +1 -1
  252. package/lib/module/reanimated2/layoutReanimation/defaultAnimations/Slide.js +8 -13
  253. package/lib/module/reanimated2/layoutReanimation/defaultAnimations/Slide.js.map +1 -1
  254. package/lib/module/reanimated2/layoutReanimation/defaultAnimations/Zoom.js +16 -21
  255. package/lib/module/reanimated2/layoutReanimation/defaultAnimations/Zoom.js.map +1 -1
  256. package/lib/module/reanimated2/layoutReanimation/index.js +1 -0
  257. package/lib/module/reanimated2/layoutReanimation/index.js.map +1 -1
  258. package/lib/module/reanimated2/layoutReanimation/sharedTransitions/index.js +82 -0
  259. package/lib/module/reanimated2/layoutReanimation/sharedTransitions/index.js.map +1 -0
  260. package/lib/module/reanimated2/mappers.js +16 -6
  261. package/lib/module/reanimated2/mappers.js.map +1 -1
  262. package/lib/module/reanimated2/mock.js +45 -0
  263. package/lib/module/reanimated2/mock.js.map +1 -1
  264. package/lib/module/reanimated2/mutables.js +4 -2
  265. package/lib/module/reanimated2/mutables.js.map +1 -1
  266. package/lib/module/reanimated2/platform-specific/checkVersion.js +35 -0
  267. package/lib/module/reanimated2/platform-specific/checkVersion.js.map +1 -0
  268. package/lib/module/reanimated2/platform-specific/checkVersion.web.js +8 -0
  269. package/lib/module/reanimated2/platform-specific/checkVersion.web.js.map +1 -0
  270. package/lib/module/reanimated2/pluginUtils.js +4 -0
  271. package/lib/module/reanimated2/pluginUtils.js.map +1 -0
  272. package/lib/module/reanimated2/shareables.js +38 -9
  273. package/lib/module/reanimated2/shareables.js.map +1 -1
  274. package/lib/module/reanimated2/threads.js +105 -4
  275. package/lib/module/reanimated2/threads.js.map +1 -1
  276. package/lib/module/reanimated2/utils.js +5 -0
  277. package/lib/module/reanimated2/utils.js.map +1 -1
  278. package/lib/module/reanimated2/valueSetter.js +1 -2
  279. package/lib/module/reanimated2/valueSetter.js.map +1 -1
  280. package/package.json +9 -8
  281. package/{plugin.js → plugin/index.js} +173 -228
  282. package/react-native-reanimated.d.ts +57 -10
  283. package/scripts/reanimated_utils.rb +15 -2
  284. package/src/createAnimatedComponent.tsx +238 -28
  285. package/src/reanimated2/Colors.ts +38 -11
  286. package/src/reanimated2/NativeReanimated/NativeReanimated.ts +63 -41
  287. package/src/reanimated2/animation/styleAnimation.ts +8 -1
  288. package/src/reanimated2/animation/util.ts +29 -16
  289. package/src/reanimated2/commonTypes.ts +28 -1
  290. package/src/reanimated2/component/FlatList.tsx +30 -5
  291. package/src/reanimated2/core.ts +45 -75
  292. package/src/reanimated2/errors.ts +57 -0
  293. package/src/reanimated2/frameCallback/FrameCallbackRegistryUI.ts +2 -2
  294. package/src/reanimated2/globals.d.ts +34 -18
  295. package/src/reanimated2/hook/index.ts +1 -1
  296. package/src/reanimated2/hook/useAnimatedKeyboard.ts +10 -4
  297. package/src/reanimated2/hook/useAnimatedReaction.ts +12 -5
  298. package/src/reanimated2/hook/useAnimatedRef.ts +1 -4
  299. package/src/reanimated2/hook/useAnimatedSensor.ts +88 -11
  300. package/src/reanimated2/hook/useAnimatedStyle.ts +43 -45
  301. package/src/reanimated2/hook/useDerivedValue.ts +8 -1
  302. package/src/reanimated2/hook/useScrollViewOffset.ts +2 -2
  303. package/src/reanimated2/hook/utils.ts +15 -61
  304. package/src/reanimated2/index.ts +1 -0
  305. package/src/reanimated2/initializers.ts +184 -0
  306. package/src/reanimated2/interpolateColor.ts +104 -20
  307. package/src/reanimated2/jestUtils.ts +1 -1
  308. package/src/reanimated2/js-reanimated/JSReanimated.ts +105 -18
  309. package/src/reanimated2/js-reanimated/Mapper.ts +32 -0
  310. package/src/reanimated2/js-reanimated/WebSensor.ts +34 -0
  311. package/src/reanimated2/js-reanimated/commonTypes.ts +0 -1
  312. package/src/reanimated2/js-reanimated/global.ts +4 -8
  313. package/src/reanimated2/js-reanimated/index.ts +0 -1
  314. package/src/reanimated2/layoutReanimation/animationBuilder/commonTypes.ts +13 -1
  315. package/src/reanimated2/layoutReanimation/animationBuilder/index.ts +1 -0
  316. package/src/reanimated2/layoutReanimation/animationsManager.ts +17 -12
  317. package/src/reanimated2/layoutReanimation/defaultAnimations/Bounce.ts +16 -19
  318. package/src/reanimated2/layoutReanimation/defaultAnimations/Lightspeed.ts +14 -11
  319. package/src/reanimated2/layoutReanimation/defaultAnimations/Roll.ts +21 -11
  320. package/src/reanimated2/layoutReanimation/defaultAnimations/Slide.ts +32 -11
  321. package/src/reanimated2/layoutReanimation/defaultAnimations/Zoom.ts +36 -19
  322. package/src/reanimated2/layoutReanimation/index.ts +1 -0
  323. package/src/reanimated2/layoutReanimation/sharedTransitions/index.ts +87 -0
  324. package/src/reanimated2/mappers.ts +17 -6
  325. package/src/reanimated2/mock.ts +128 -0
  326. package/src/reanimated2/mutables.ts +2 -0
  327. package/src/reanimated2/platform-specific/checkVersion.ts +39 -0
  328. package/src/reanimated2/platform-specific/checkVersion.web.ts +6 -0
  329. package/src/reanimated2/pluginUtils.ts +8 -0
  330. package/src/reanimated2/shareables.ts +47 -11
  331. package/src/reanimated2/threads.ts +103 -1
  332. package/src/reanimated2/utils.ts +6 -0
  333. package/src/reanimated2/valueSetter.ts +1 -2
  334. package/lib/commonjs/reanimated2/time.js +0 -55
  335. package/lib/commonjs/reanimated2/time.js.map +0 -1
  336. package/lib/module/reanimated2/time.js +0 -37
  337. package/lib/module/reanimated2/time.js.map +0 -1
  338. package/src/reanimated2/time.ts +0 -30
  339. /package/android/src/{main/java → reactNativeVersionPatch/ReanimatedUIManager/70}/com/swmansion/reanimated/layoutReanimation/ReanimatedUIManager.java +0 -0
@@ -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';
@@ -0,0 +1,184 @@
1
+ import { reportFatalErrorOnJS } from './errors';
2
+ import NativeReanimatedModule from './NativeReanimated';
3
+ import { isJest } from './PlatformChecker';
4
+ import {
5
+ runOnJS,
6
+ setupSetImmediate,
7
+ flushImmediates,
8
+ runOnUIImmediately,
9
+ } from './threads';
10
+
11
+ // callGuard is only used with debug builds
12
+ function callGuardDEV<T extends Array<any>, U>(
13
+ fn: (...args: T) => U,
14
+ ...args: T
15
+ ): void {
16
+ 'worklet';
17
+ try {
18
+ fn(...args);
19
+ } catch (e) {
20
+ if (global.ErrorUtils) {
21
+ global.ErrorUtils.reportFatalError(e as Error);
22
+ } else {
23
+ throw e;
24
+ }
25
+ }
26
+ }
27
+
28
+ function valueUnpacker(objectToUnpack: any, category?: string): any {
29
+ 'worklet';
30
+ let workletsCache = global.__workletsCache;
31
+ let handleCache = global.__handleCache;
32
+ if (workletsCache === undefined) {
33
+ // init
34
+ workletsCache = global.__workletsCache = new Map();
35
+ handleCache = global.__handleCache = new WeakMap();
36
+ }
37
+ const workletHash = objectToUnpack.__workletHash;
38
+ if (workletHash !== undefined) {
39
+ let workletFun = workletsCache.get(workletHash);
40
+ if (workletFun === undefined) {
41
+ const initData = objectToUnpack.__initData;
42
+ if (global.evalWithSourceMap) {
43
+ // if the runtime (hermes only for now) supports loading source maps
44
+ // we want to use the proper filename for the location as it guarantees
45
+ // that debugger understands and loads the source code of the file where
46
+ // the worklet is defined.
47
+ workletFun = global.evalWithSourceMap(
48
+ '(' + initData.code + '\n)',
49
+ initData.location,
50
+ initData.sourceMap
51
+ ) as (...args: any[]) => any;
52
+ } else if (global.evalWithSourceUrl) {
53
+ // if the runtime doesn't support loading source maps, in dev mode we
54
+ // can pass source url when evaluating the worklet. Now, instead of using
55
+ // the actual file location we use worklet hash, as it the allows us to
56
+ // properly symbolicate traces (see errors.ts for details)
57
+ workletFun = global.evalWithSourceUrl(
58
+ '(' + initData.code + '\n)',
59
+ `worklet_${workletHash}`
60
+ ) as (...args: any[]) => any;
61
+ } else {
62
+ // in release we use the regular eval to save on JSI calls
63
+ // eslint-disable-next-line no-eval
64
+ workletFun = eval('(' + initData.code + '\n)') as (
65
+ ...args: any[]
66
+ ) => any;
67
+ }
68
+ workletsCache.set(workletHash, workletFun);
69
+ }
70
+ const functionInstance = workletFun.bind(objectToUnpack);
71
+ objectToUnpack._recur = functionInstance;
72
+ return functionInstance;
73
+ } else if (objectToUnpack.__init) {
74
+ let value = handleCache!.get(objectToUnpack);
75
+ if (value === undefined) {
76
+ value = objectToUnpack.__init();
77
+ handleCache!.set(objectToUnpack, value);
78
+ }
79
+ return value;
80
+ } else if (category === 'RemoteFunction') {
81
+ const fun = () => {
82
+ throw new Error(`Tried to synchronously call a non-worklet function on the UI thread.
83
+
84
+ Possible solutions are:
85
+ a) If you want to synchronously execute this method, mark it as a worklet
86
+ b) If you want to execute this function on the JS thread, wrap it using \`runOnJS\``);
87
+ };
88
+ fun.__remoteFunction = objectToUnpack;
89
+ return fun;
90
+ } else {
91
+ throw new Error('data type not recognized by unpack method');
92
+ }
93
+ }
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
+
139
+ export function initializeUIRuntime() {
140
+ NativeReanimatedModule.installCoreFunctions(callGuardDEV, valueUnpacker);
141
+
142
+ const IS_JEST = isJest();
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
+
156
+ const capturableConsole = console;
157
+ runOnUIImmediately(() => {
158
+ 'worklet';
159
+ // setup error handler
160
+ global.ErrorUtils = {
161
+ reportFatalError: (error: Error) => {
162
+ runOnJS(reportFatalErrorOnJS)({
163
+ message: error.message,
164
+ stack: error.stack,
165
+ });
166
+ },
167
+ };
168
+
169
+ // setup console
170
+ // @ts-ignore TypeScript doesn't like that there are missing methods in console object, but we don't provide all the methods for the UI runtime console version
171
+ global.console = {
172
+ debug: runOnJS(capturableConsole.debug),
173
+ log: runOnJS(capturableConsole.log),
174
+ warn: runOnJS(capturableConsole.warn),
175
+ error: runOnJS(capturableConsole.error),
176
+ info: runOnJS(capturableConsole.info),
177
+ };
178
+
179
+ if (!IS_JEST) {
180
+ setupSetImmediate();
181
+ setupRequestAnimationFrame();
182
+ }
183
+ })();
184
+ }
@@ -19,29 +19,96 @@ export const Extrapolate = {
19
19
  IDENTITY: 'identity',
20
20
  };
21
21
 
22
+ export type InterpolationOptions = {
23
+ gamma?: number;
24
+ useCorrectedHSVInterpolation?: boolean;
25
+ };
26
+
22
27
  const interpolateColorsHSV = (
23
28
  value: number,
24
29
  inputRange: readonly number[],
25
- colors: InterpolateHSV
30
+ colors: InterpolateHSV,
31
+ options: InterpolationOptions
26
32
  ) => {
27
33
  'worklet';
28
- const h = interpolate(value, inputRange, colors.h, Extrapolate.CLAMP);
34
+ let h = 0;
35
+ const { useCorrectedHSVInterpolation = true } = options;
36
+ if (useCorrectedHSVInterpolation) {
37
+ // if the difference between hues in a range is > 180 deg
38
+ // then move the hue at the right end of the range +/- 360 deg
39
+ // and add the next point in the original place + 0.00001 with original hue
40
+ // to not break the next range
41
+ const correctedInputRange = [inputRange[0]];
42
+ const originalH = colors.h;
43
+ const correctedH = [originalH[0]];
44
+
45
+ for (let i = 1; i < originalH.length; ++i) {
46
+ const d = originalH[i] - originalH[i - 1];
47
+ if (originalH[i] > originalH[i - 1] && d > 0.5) {
48
+ correctedInputRange.push(inputRange[i]);
49
+ correctedInputRange.push(inputRange[i] + 0.00001);
50
+ correctedH.push(originalH[i] - 1);
51
+ correctedH.push(originalH[i]);
52
+ } else if (originalH[i] < originalH[i - 1] && d < -0.5) {
53
+ correctedInputRange.push(inputRange[i]);
54
+ correctedInputRange.push(inputRange[i] + 0.00001);
55
+ correctedH.push(originalH[i] + 1);
56
+ correctedH.push(originalH[i]);
57
+ } else {
58
+ correctedInputRange.push(inputRange[i]);
59
+ correctedH.push(originalH[i]);
60
+ }
61
+ }
62
+ h =
63
+ (interpolate(value, correctedInputRange, correctedH, Extrapolate.CLAMP) +
64
+ 1) %
65
+ 1;
66
+ } else {
67
+ h = interpolate(value, inputRange, colors.h, Extrapolate.CLAMP);
68
+ }
29
69
  const s = interpolate(value, inputRange, colors.s, Extrapolate.CLAMP);
30
70
  const v = interpolate(value, inputRange, colors.v, Extrapolate.CLAMP);
31
- return hsvToColor(h, s, v);
71
+ const a = interpolate(value, inputRange, colors.a, Extrapolate.CLAMP);
72
+ return hsvToColor(h, s, v, a);
73
+ };
74
+
75
+ const toLinearSpace = (x: number[], gamma: number): number[] => {
76
+ 'worklet';
77
+ return x.map((v) => Math.pow(v / 255, gamma));
78
+ };
79
+
80
+ const toGammaSpace = (x: number, gamma: number): number => {
81
+ 'worklet';
82
+ return Math.round(Math.pow(x, 1 / gamma) * 255);
32
83
  };
33
84
 
34
85
  const interpolateColorsRGB = (
35
86
  value: number,
36
87
  inputRange: readonly number[],
37
- colors: InterpolateRGB
88
+ colors: InterpolateRGB,
89
+ options: InterpolationOptions
38
90
  ) => {
39
91
  'worklet';
40
- const r = interpolate(value, inputRange, colors.r, Extrapolate.CLAMP);
41
- const g = interpolate(value, inputRange, colors.g, Extrapolate.CLAMP);
42
- const b = interpolate(value, inputRange, colors.b, Extrapolate.CLAMP);
92
+ const { gamma = 2.2 } = options;
93
+ let { r: outputR, g: outputG, b: outputB } = colors;
94
+ if (gamma !== 1) {
95
+ outputR = toLinearSpace(outputR, gamma);
96
+ outputG = toLinearSpace(outputG, gamma);
97
+ outputB = toLinearSpace(outputB, gamma);
98
+ }
99
+ const r = interpolate(value, inputRange, outputR, Extrapolate.CLAMP);
100
+ const g = interpolate(value, inputRange, outputG, Extrapolate.CLAMP);
101
+ const b = interpolate(value, inputRange, outputB, Extrapolate.CLAMP);
43
102
  const a = interpolate(value, inputRange, colors.a, Extrapolate.CLAMP);
44
- return rgbaColor(r, g, b, a);
103
+ if (gamma === 1) {
104
+ return rgbaColor(r, g, b, a);
105
+ }
106
+ return rgbaColor(
107
+ toGammaSpace(r, gamma),
108
+ toGammaSpace(g, gamma),
109
+ toGammaSpace(b, gamma),
110
+ a
111
+ );
45
112
  };
46
113
 
47
114
  interface InterpolateRGB {
@@ -78,6 +145,7 @@ interface InterpolateHSV {
78
145
  h: number[];
79
146
  s: number[];
80
147
  v: number[];
148
+ a: number[];
81
149
  }
82
150
 
83
151
  const getInterpolateHSV = (
@@ -87,36 +155,47 @@ const getInterpolateHSV = (
87
155
  const h = [];
88
156
  const s = [];
89
157
  const v = [];
158
+ const a = [];
90
159
  for (let i = 0; i < colors.length; ++i) {
91
160
  const color = colors[i];
92
- const processedColor = RGBtoHSV(processColor(color) as any);
93
- if (processedColor) {
94
- h.push(processedColor.h);
95
- s.push(processedColor.s);
96
- v.push(processedColor.v);
161
+ const processedColor = processColor(color) as any;
162
+ if (typeof processedColor === 'number') {
163
+ const processedHSVColor = RGBtoHSV(
164
+ red(processedColor),
165
+ green(processedColor),
166
+ blue(processedColor)
167
+ );
168
+
169
+ h.push(processedHSVColor.h);
170
+ s.push(processedHSVColor.s);
171
+ v.push(processedHSVColor.v);
172
+ a.push(opacity(processedColor));
97
173
  }
98
174
  }
99
- return { h, s, v };
175
+ return { h, s, v, a };
100
176
  };
101
177
 
102
178
  export const interpolateColor = (
103
179
  value: number,
104
180
  inputRange: readonly number[],
105
181
  outputRange: readonly (string | number)[],
106
- colorSpace: 'RGB' | 'HSV' = 'RGB'
182
+ colorSpace: 'RGB' | 'HSV' = 'RGB',
183
+ options: InterpolationOptions = {}
107
184
  ): string | number => {
108
185
  'worklet';
109
186
  if (colorSpace === 'HSV') {
110
187
  return interpolateColorsHSV(
111
188
  value,
112
189
  inputRange,
113
- getInterpolateHSV(outputRange)
190
+ getInterpolateHSV(outputRange),
191
+ options
114
192
  );
115
193
  } else if (colorSpace === 'RGB') {
116
194
  return interpolateColorsRGB(
117
195
  value,
118
196
  inputRange,
119
- getInterpolateRGB(outputRange)
197
+ getInterpolateRGB(outputRange),
198
+ options
120
199
  );
121
200
  }
122
201
  throw new Error(
@@ -134,18 +213,21 @@ export interface InterpolateConfig {
134
213
  outputRange: readonly (string | number)[];
135
214
  colorSpace: ColorSpace;
136
215
  cache: SharedValue<InterpolateRGB | InterpolateHSV | null>;
216
+ options: InterpolationOptions;
137
217
  }
138
218
 
139
219
  export function useInterpolateConfig(
140
220
  inputRange: readonly number[],
141
221
  outputRange: readonly (string | number)[],
142
- colorSpace = ColorSpace.RGB
222
+ colorSpace = ColorSpace.RGB,
223
+ options: InterpolationOptions = {}
143
224
  ): SharedValue<InterpolateConfig> {
144
225
  return useSharedValue<InterpolateConfig>({
145
226
  inputRange,
146
227
  outputRange,
147
228
  colorSpace,
148
229
  cache: makeMutable<InterpolateRGB | InterpolateHSV | null>(null),
230
+ options,
149
231
  });
150
232
  }
151
233
 
@@ -163,7 +245,8 @@ export const interpolateSharableColor = (
163
245
  return interpolateColorsRGB(
164
246
  value,
165
247
  interpolateConfig.value.inputRange,
166
- colors as InterpolateRGB
248
+ colors as InterpolateRGB,
249
+ interpolateConfig.value.options
167
250
  );
168
251
  } else if (interpolateConfig.value.colorSpace === ColorSpace.HSV) {
169
252
  if (!colors) {
@@ -173,7 +256,8 @@ export const interpolateSharableColor = (
173
256
  return interpolateColorsHSV(
174
257
  value,
175
258
  interpolateConfig.value.inputRange,
176
- colors as InterpolateHSV
259
+ colors as InterpolateHSV,
260
+ interpolateConfig.value.options
177
261
  );
178
262
  }
179
263
  throw new Error(
@@ -149,7 +149,7 @@ const afterTest = () => {
149
149
 
150
150
  export const withReanimatedTimer = (animationTest) => {
151
151
  console.warn(
152
- 'This method is deprecated, you shoulddefine your own before and after test hooks to enable jest.useFakeTimers(). Check out the documentation for details on testing'
152
+ 'This method is deprecated, you should define your own before and after test hooks to enable jest.useFakeTimers(). Check out the documentation for details on testing'
153
153
  );
154
154
  beforeTest();
155
155
  animationTest();
@@ -1,28 +1,32 @@
1
1
  import { NativeReanimated } from '../NativeReanimated/NativeReanimated';
2
- import { ShareableRef } from '../commonTypes';
3
- import { isJest } from '../PlatformChecker';
2
+ import {
3
+ SensorType,
4
+ ShareableRef,
5
+ Value3D,
6
+ ValueRotation,
7
+ } from '../commonTypes';
8
+ import { WebSensor } from './WebSensor';
4
9
 
5
10
  export default class JSReanimated extends NativeReanimated {
6
- _valueUnpacker?: <T>(value: T) => void = undefined;
11
+ nextSensorId = 0;
12
+ sensors = new Map<number, WebSensor>();
7
13
 
8
14
  constructor() {
9
15
  super(false);
10
- if (isJest()) {
11
- // on node < 16 jest have problems mocking performance.now method which
12
- // results in the tests failing, since date precision isn't that important
13
- // for tests, we use Date.now instead
14
- this.getTimestamp = () => Date.now();
15
- } else {
16
- this.getTimestamp = () => window.performance.now();
17
- }
18
16
  }
19
17
 
20
18
  makeShareableClone<T>(value: T): ShareableRef<T> {
21
19
  return { __hostObjectShareableJSRef: value };
22
20
  }
23
21
 
24
- installCoreFunctions(valueUnpacker: <T>(value: T) => T): void {
25
- this._valueUnpacker = valueUnpacker;
22
+ installCoreFunctions(
23
+ _callGuard: <T extends Array<any>, U>(
24
+ fn: (...args: T) => U,
25
+ ...args: T
26
+ ) => void,
27
+ _valueUnpacker: <T>(value: T) => T
28
+ ): void {
29
+ // noop
26
30
  }
27
31
 
28
32
  scheduleOnUI<T>(worklet: ShareableRef<T>) {
@@ -48,13 +52,62 @@ export default class JSReanimated extends NativeReanimated {
48
52
  );
49
53
  }
50
54
 
51
- registerSensor(): number {
52
- console.warn('[Reanimated] useAnimatedSensor is not available on web yet.');
53
- return -1;
55
+ registerSensor(
56
+ sensorType: SensorType,
57
+ interval: number,
58
+ iosReferenceFrame: number,
59
+ eventHandler: (data: Value3D | ValueRotation) => void
60
+ ): number {
61
+ if (!(this.getSensorName(sensorType) in window)) {
62
+ return -1;
63
+ }
64
+
65
+ const sensor: WebSensor = this.initializeSensor(sensorType, interval);
66
+ let callback;
67
+ if (sensorType === SensorType.ROTATION) {
68
+ callback = () => {
69
+ const [qw, qx, qy, qz] = sensor.quaternion;
70
+
71
+ // reference: https://stackoverflow.com/questions/5782658/extracting-yaw-from-a-quaternion
72
+ const yaw = Math.atan2(
73
+ 2.0 * (qy * qz + qw * qx),
74
+ qw * qw - qx * qx - qy * qy + qz * qz
75
+ );
76
+ const pitch = Math.sin(-2.0 * (qx * qz - qw * qy));
77
+ const roll = Math.atan2(
78
+ 2.0 * (qx * qy + qw * qz),
79
+ qw * qw + qx * qx - qy * qy - qz * qz
80
+ );
81
+ eventHandler({
82
+ qw,
83
+ qx,
84
+ qy,
85
+ qz,
86
+ yaw,
87
+ pitch,
88
+ roll,
89
+ interfaceOrientation: 0,
90
+ });
91
+ };
92
+ } else {
93
+ callback = () => {
94
+ const { x, y, z } = sensor;
95
+ eventHandler({ x, y, z, interfaceOrientation: 0 });
96
+ };
97
+ }
98
+ sensor.addEventListener('reading', callback);
99
+ sensor.start();
100
+
101
+ this.sensors.set(this.nextSensorId, sensor);
102
+ return this.nextSensorId++;
54
103
  }
55
104
 
56
- unregisterSensor(): void {
57
- // noop
105
+ unregisterSensor(id: number): void {
106
+ const sensor: WebSensor | undefined = this.sensors.get(id);
107
+ if (sensor !== undefined) {
108
+ sensor.stop();
109
+ this.sensors.delete(id);
110
+ }
58
111
  }
59
112
 
60
113
  subscribeForKeyboardEvents(_: ShareableRef<number>): number {
@@ -67,4 +120,38 @@ export default class JSReanimated extends NativeReanimated {
67
120
  unsubscribeFromKeyboardEvents(_: number): void {
68
121
  // noop
69
122
  }
123
+
124
+ initializeSensor(sensorType: SensorType, interval: number): WebSensor {
125
+ const config =
126
+ interval <= 0
127
+ ? { referenceFrame: 'device' }
128
+ : { frequency: 1000 / interval };
129
+ switch (sensorType) {
130
+ case SensorType.ACCELEROMETER:
131
+ return new window.Accelerometer(config);
132
+ case SensorType.GYROSCOPE:
133
+ return new window.Gyroscope(config);
134
+ case SensorType.GRAVITY:
135
+ return new window.GravitySensor(config);
136
+ case SensorType.MAGNETIC_FIELD:
137
+ return new window.Magnetometer(config);
138
+ case SensorType.ROTATION:
139
+ return new window.AbsoluteOrientationSensor(config);
140
+ }
141
+ }
142
+
143
+ getSensorName(sensorType: SensorType): string {
144
+ switch (sensorType) {
145
+ case SensorType.ACCELEROMETER:
146
+ return 'Accelerometer';
147
+ case SensorType.GRAVITY:
148
+ return 'GravitySensor';
149
+ case SensorType.GYROSCOPE:
150
+ return 'Gyroscope';
151
+ case SensorType.MAGNETIC_FIELD:
152
+ return 'Magnetometer';
153
+ case SensorType.ROTATION:
154
+ return 'AbsoluteOrientationSensor';
155
+ }
156
+ }
70
157
  }