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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (177) hide show
  1. package/Common/cpp/AnimatedSensor/AnimatedSensorModule.cpp +32 -37
  2. package/Common/cpp/AnimatedSensor/AnimatedSensorModule.h +4 -3
  3. package/Common/cpp/LayoutAnimations/LayoutAnimationsManager.cpp +76 -0
  4. package/Common/cpp/LayoutAnimations/LayoutAnimationsManager.h +41 -0
  5. package/Common/cpp/NativeModules/NativeReanimatedModule.cpp +161 -165
  6. package/Common/cpp/NativeModules/NativeReanimatedModule.h +36 -34
  7. package/Common/cpp/NativeModules/NativeReanimatedModuleSpec.cpp +22 -34
  8. package/Common/cpp/NativeModules/NativeReanimatedModuleSpec.h +13 -14
  9. package/Common/cpp/SharedItems/RuntimeManager.h +1 -34
  10. package/Common/cpp/SharedItems/Shareables.cpp +142 -0
  11. package/Common/cpp/SharedItems/Shareables.h +455 -0
  12. package/Common/cpp/Tools/PlatformDepMethodsHolder.h +6 -0
  13. package/Common/cpp/Tools/RuntimeDecorator.cpp +54 -26
  14. package/Common/cpp/Tools/RuntimeDecorator.h +11 -2
  15. package/Common/cpp/Tools/Scheduler.cpp +10 -0
  16. package/Common/cpp/Tools/SingleInstanceChecker.h +1 -1
  17. package/android/src/main/cpp/LayoutAnimations.cpp +6 -16
  18. package/android/src/main/cpp/LayoutAnimations.h +4 -4
  19. package/android/src/main/cpp/NativeProxy.cpp +50 -63
  20. package/ios/native/NativeProxy.mm +26 -42
  21. package/lib/commonjs/createAnimatedComponent.js +7 -11
  22. package/lib/commonjs/createAnimatedComponent.js.map +1 -1
  23. package/lib/commonjs/reanimated2/NativeReanimated/NativeReanimated.js +24 -25
  24. package/lib/commonjs/reanimated2/NativeReanimated/NativeReanimated.js.map +1 -1
  25. package/lib/commonjs/reanimated2/ViewDescriptorsSet.js +27 -55
  26. package/lib/commonjs/reanimated2/ViewDescriptorsSet.js.map +1 -1
  27. package/lib/commonjs/reanimated2/WorkletEventHandler.js +5 -3
  28. package/lib/commonjs/reanimated2/WorkletEventHandler.js.map +1 -1
  29. package/lib/commonjs/reanimated2/animation/util.js.map +1 -1
  30. package/lib/commonjs/reanimated2/commonTypes.js +7 -0
  31. package/lib/commonjs/reanimated2/commonTypes.js.map +1 -1
  32. package/lib/commonjs/reanimated2/core.js +111 -209
  33. package/lib/commonjs/reanimated2/core.js.map +1 -1
  34. package/lib/commonjs/reanimated2/globals.d.js.map +1 -1
  35. package/lib/commonjs/reanimated2/hook/useAnimatedKeyboard.js +15 -9
  36. package/lib/commonjs/reanimated2/hook/useAnimatedKeyboard.js.map +1 -1
  37. package/lib/commonjs/reanimated2/hook/useAnimatedRef.js +18 -8
  38. package/lib/commonjs/reanimated2/hook/useAnimatedRef.js.map +1 -1
  39. package/lib/commonjs/reanimated2/hook/useAnimatedSensor.js +27 -36
  40. package/lib/commonjs/reanimated2/hook/useAnimatedSensor.js.map +1 -1
  41. package/lib/commonjs/reanimated2/hook/useAnimatedStyle.js +24 -50
  42. package/lib/commonjs/reanimated2/hook/useAnimatedStyle.js.map +1 -1
  43. package/lib/commonjs/reanimated2/hook/useSharedValue.js +3 -2
  44. package/lib/commonjs/reanimated2/hook/useSharedValue.js.map +1 -1
  45. package/lib/commonjs/reanimated2/hook/utils.js.map +1 -1
  46. package/lib/commonjs/reanimated2/interpolateColor.js.map +1 -1
  47. package/lib/commonjs/reanimated2/jestUtils.js +8 -33
  48. package/lib/commonjs/reanimated2/jestUtils.js.map +1 -1
  49. package/lib/commonjs/reanimated2/js-reanimated/JSReanimated.js +16 -100
  50. package/lib/commonjs/reanimated2/js-reanimated/JSReanimated.js.map +1 -1
  51. package/lib/commonjs/reanimated2/js-reanimated/index.js +5 -0
  52. package/lib/commonjs/reanimated2/js-reanimated/index.js.map +1 -1
  53. package/lib/commonjs/reanimated2/layoutReanimation/animationBuilder/commonTypes.js.map +1 -1
  54. package/lib/commonjs/reanimated2/layoutReanimation/animationBuilder/index.js.map +1 -1
  55. package/lib/commonjs/reanimated2/layoutReanimation/animationsManager.js +91 -0
  56. package/lib/commonjs/reanimated2/layoutReanimation/animationsManager.js.map +1 -0
  57. package/lib/commonjs/reanimated2/layoutReanimation/index.js +1 -1
  58. package/lib/commonjs/reanimated2/layoutReanimation/index.js.map +1 -1
  59. package/lib/commonjs/reanimated2/mappers.js +186 -0
  60. package/lib/commonjs/reanimated2/mappers.js.map +1 -0
  61. package/lib/commonjs/reanimated2/mutables.js +173 -0
  62. package/lib/commonjs/reanimated2/mutables.js.map +1 -0
  63. package/lib/commonjs/reanimated2/shareables.js +153 -0
  64. package/lib/commonjs/reanimated2/shareables.js.map +1 -0
  65. package/lib/commonjs/reanimated2/threads.js +58 -0
  66. package/lib/commonjs/reanimated2/threads.js.map +1 -0
  67. package/lib/commonjs/reanimated2/time.js +55 -0
  68. package/lib/commonjs/reanimated2/time.js.map +1 -0
  69. package/lib/commonjs/reanimated2/valueSetter.js +81 -0
  70. package/lib/commonjs/reanimated2/valueSetter.js.map +1 -0
  71. package/lib/module/createAnimatedComponent.js +7 -12
  72. package/lib/module/createAnimatedComponent.js.map +1 -1
  73. package/lib/module/reanimated2/NativeReanimated/NativeReanimated.js +24 -25
  74. package/lib/module/reanimated2/NativeReanimated/NativeReanimated.js.map +1 -1
  75. package/lib/module/reanimated2/ViewDescriptorsSet.js +27 -53
  76. package/lib/module/reanimated2/ViewDescriptorsSet.js.map +1 -1
  77. package/lib/module/reanimated2/WorkletEventHandler.js +4 -3
  78. package/lib/module/reanimated2/WorkletEventHandler.js.map +1 -1
  79. package/lib/module/reanimated2/animation/util.js.map +1 -1
  80. package/lib/module/reanimated2/commonTypes.js +7 -0
  81. package/lib/module/reanimated2/commonTypes.js.map +1 -1
  82. package/lib/module/reanimated2/core.js +66 -188
  83. package/lib/module/reanimated2/core.js.map +1 -1
  84. package/lib/module/reanimated2/globals.d.js.map +1 -1
  85. package/lib/module/reanimated2/hook/useAnimatedKeyboard.js +16 -6
  86. package/lib/module/reanimated2/hook/useAnimatedKeyboard.js.map +1 -1
  87. package/lib/module/reanimated2/hook/useAnimatedRef.js +17 -8
  88. package/lib/module/reanimated2/hook/useAnimatedRef.js.map +1 -1
  89. package/lib/module/reanimated2/hook/useAnimatedSensor.js +29 -34
  90. package/lib/module/reanimated2/hook/useAnimatedSensor.js.map +1 -1
  91. package/lib/module/reanimated2/hook/useAnimatedStyle.js +26 -52
  92. package/lib/module/reanimated2/hook/useAnimatedStyle.js.map +1 -1
  93. package/lib/module/reanimated2/hook/useSharedValue.js +3 -2
  94. package/lib/module/reanimated2/hook/useSharedValue.js.map +1 -1
  95. package/lib/module/reanimated2/hook/utils.js.map +1 -1
  96. package/lib/module/reanimated2/interpolateColor.js.map +1 -1
  97. package/lib/module/reanimated2/jestUtils.js +8 -31
  98. package/lib/module/reanimated2/jestUtils.js.map +1 -1
  99. package/lib/module/reanimated2/js-reanimated/JSReanimated.js +16 -95
  100. package/lib/module/reanimated2/js-reanimated/JSReanimated.js.map +1 -1
  101. package/lib/module/reanimated2/js-reanimated/index.js +5 -0
  102. package/lib/module/reanimated2/js-reanimated/index.js.map +1 -1
  103. package/lib/module/reanimated2/layoutReanimation/animationBuilder/commonTypes.js.map +1 -1
  104. package/lib/module/reanimated2/layoutReanimation/animationBuilder/index.js.map +1 -1
  105. package/lib/module/reanimated2/layoutReanimation/animationsManager.js +84 -0
  106. package/lib/module/reanimated2/layoutReanimation/animationsManager.js.map +1 -0
  107. package/lib/module/reanimated2/layoutReanimation/index.js +1 -1
  108. package/lib/module/reanimated2/layoutReanimation/index.js.map +1 -1
  109. package/lib/module/reanimated2/mappers.js +173 -0
  110. package/lib/module/reanimated2/mappers.js.map +1 -0
  111. package/lib/module/reanimated2/mutables.js +149 -0
  112. package/lib/module/reanimated2/mutables.js.map +1 -0
  113. package/lib/module/reanimated2/shareables.js +133 -0
  114. package/lib/module/reanimated2/shareables.js.map +1 -0
  115. package/lib/module/reanimated2/threads.js +45 -0
  116. package/lib/module/reanimated2/threads.js.map +1 -0
  117. package/lib/module/reanimated2/time.js +37 -0
  118. package/lib/module/reanimated2/time.js.map +1 -0
  119. package/lib/module/reanimated2/valueSetter.js +66 -0
  120. package/lib/module/reanimated2/valueSetter.js.map +1 -0
  121. package/package.json +1 -1
  122. package/plugin.js +31 -15
  123. package/src/createAnimatedComponent.tsx +8 -24
  124. package/src/reanimated2/NativeReanimated/NativeReanimated.ts +34 -53
  125. package/src/reanimated2/ViewDescriptorsSet.ts +30 -69
  126. package/src/reanimated2/WorkletEventHandler.ts +4 -11
  127. package/src/reanimated2/animation/util.ts +1 -0
  128. package/src/reanimated2/commonTypes.ts +29 -4
  129. package/src/reanimated2/core.ts +103 -230
  130. package/src/reanimated2/globals.d.ts +26 -26
  131. package/src/reanimated2/hook/useAnimatedKeyboard.ts +18 -9
  132. package/src/reanimated2/hook/useAnimatedRef.ts +27 -9
  133. package/src/reanimated2/hook/useAnimatedSensor.ts +33 -31
  134. package/src/reanimated2/hook/useAnimatedStyle.ts +26 -64
  135. package/src/reanimated2/hook/useSharedValue.ts +6 -3
  136. package/src/reanimated2/hook/utils.ts +1 -1
  137. package/src/reanimated2/interpolateColor.ts +2 -2
  138. package/src/reanimated2/jestUtils.ts +14 -29
  139. package/src/reanimated2/js-reanimated/JSReanimated.ts +19 -93
  140. package/src/reanimated2/js-reanimated/index.ts +4 -0
  141. package/src/reanimated2/layoutReanimation/animationBuilder/commonTypes.ts +7 -0
  142. package/src/reanimated2/layoutReanimation/animationBuilder/index.ts +1 -0
  143. package/src/reanimated2/layoutReanimation/animationsManager.ts +94 -0
  144. package/src/reanimated2/layoutReanimation/index.ts +1 -1
  145. package/src/reanimated2/mappers.ts +179 -0
  146. package/src/reanimated2/mutables.ts +151 -0
  147. package/src/reanimated2/shareables.ts +121 -0
  148. package/src/reanimated2/threads.ts +46 -0
  149. package/src/reanimated2/time.ts +30 -0
  150. package/src/reanimated2/valueSetter.ts +64 -0
  151. package/Common/cpp/LayoutAnimations/LayoutAnimationsProxy.cpp +0 -127
  152. package/Common/cpp/LayoutAnimations/LayoutAnimationsProxy.h +0 -56
  153. package/Common/cpp/Registries/MapperRegistry.cpp +0 -147
  154. package/Common/cpp/Registries/MapperRegistry.h +0 -29
  155. package/Common/cpp/Registries/WorkletsCache.cpp +0 -45
  156. package/Common/cpp/Registries/WorkletsCache.h +0 -24
  157. package/Common/cpp/SharedItems/FrozenObject.cpp +0 -35
  158. package/Common/cpp/SharedItems/FrozenObject.h +0 -36
  159. package/Common/cpp/SharedItems/HostFunctionHandler.h +0 -28
  160. package/Common/cpp/SharedItems/MutableValue.cpp +0 -134
  161. package/Common/cpp/SharedItems/MutableValue.h +0 -54
  162. package/Common/cpp/SharedItems/MutableValueSetterProxy.cpp +0 -47
  163. package/Common/cpp/SharedItems/MutableValueSetterProxy.h +0 -25
  164. package/Common/cpp/SharedItems/RemoteObject.cpp +0 -45
  165. package/Common/cpp/SharedItems/RemoteObject.h +0 -35
  166. package/Common/cpp/SharedItems/ShareableValue.cpp +0 -529
  167. package/Common/cpp/SharedItems/ShareableValue.h +0 -63
  168. package/Common/cpp/SharedItems/ValueWrapper.h +0 -183
  169. package/Common/cpp/Tools/JSIStoreValueUser.cpp +0 -55
  170. package/Common/cpp/Tools/JSIStoreValueUser.h +0 -37
  171. package/Common/cpp/Tools/Mapper.cpp +0 -86
  172. package/Common/cpp/Tools/Mapper.h +0 -51
  173. package/lib/commonjs/reanimated2/layoutReanimation/LayoutAnimationRepository.js +0 -58
  174. package/lib/commonjs/reanimated2/layoutReanimation/LayoutAnimationRepository.js.map +0 -1
  175. package/lib/module/reanimated2/layoutReanimation/LayoutAnimationRepository.js +0 -52
  176. package/lib/module/reanimated2/layoutReanimation/LayoutAnimationRepository.js.map +0 -1
  177. package/src/reanimated2/layoutReanimation/LayoutAnimationRepository.ts +0 -49
@@ -1,19 +1,17 @@
1
1
  #include "AnimatedSensorModule.h"
2
- #include "MutableValue.h"
3
- #include "ValueWrapper.h"
4
2
 
5
3
  #include <memory>
6
4
  #include <utility>
7
5
 
6
+ #include "Shareables.h"
7
+
8
8
  namespace reanimated {
9
9
 
10
10
  AnimatedSensorModule::AnimatedSensorModule(
11
- const PlatformDepMethodsHolder &platformDepMethodsHolder,
12
- RuntimeManager *runtimeManager)
11
+ const PlatformDepMethodsHolder &platformDepMethodsHolder)
13
12
  : platformRegisterSensorFunction_(platformDepMethodsHolder.registerSensor),
14
13
  platformUnregisterSensorFunction_(
15
- platformDepMethodsHolder.unregisterSensor),
16
- runtimeManager_(runtimeManager) {}
14
+ platformDepMethodsHolder.unregisterSensor) {}
17
15
 
18
16
  AnimatedSensorModule::~AnimatedSensorModule() {
19
17
  // It is called during app reload because app reload doesn't call hooks
@@ -25,41 +23,38 @@ AnimatedSensorModule::~AnimatedSensorModule() {
25
23
 
26
24
  jsi::Value AnimatedSensorModule::registerSensor(
27
25
  jsi::Runtime &rt,
28
- const jsi::Value &sensorType,
26
+ const std::shared_ptr<JSRuntimeHelper> &runtimeHelper,
27
+ const jsi::Value &sensorTypeValue,
29
28
  const jsi::Value &interval,
30
- const jsi::Value &sensorDataContainer) {
31
- std::shared_ptr<ShareableValue> sensorsData = ShareableValue::adapt(
32
- rt, sensorDataContainer.getObject(rt), runtimeManager_);
33
- auto &mutableObject =
34
- ValueWrapper::asMutableValue(sensorsData->valueContainer);
29
+ const jsi::Value &sensorDataHandler) {
30
+ SensorType sensorType = static_cast<SensorType>(sensorTypeValue.asNumber());
35
31
 
36
- std::function<void(double[])> setter;
37
- if (sensorType.asNumber() == SensorType::ROTATION_VECTOR) {
38
- setter = [&, mutableObject](double newValues[]) {
39
- jsi::Runtime &runtime = *runtimeManager_->runtime.get();
40
- jsi::Object value(runtime);
41
- value.setProperty(runtime, "qx", newValues[0]);
42
- value.setProperty(runtime, "qy", newValues[1]);
43
- value.setProperty(runtime, "qz", newValues[2]);
44
- value.setProperty(runtime, "qw", newValues[3]);
45
- value.setProperty(runtime, "yaw", newValues[4]);
46
- value.setProperty(runtime, "pitch", newValues[5]);
47
- value.setProperty(runtime, "roll", newValues[6]);
48
- mutableObject->setValue(runtime, std::move(value));
49
- };
50
- } else {
51
- setter = [&, mutableObject](double newValues[]) {
52
- jsi::Runtime &runtime = *runtimeManager_->runtime.get();
53
- jsi::Object value(runtime);
54
- value.setProperty(runtime, "x", newValues[0]);
55
- value.setProperty(runtime, "y", newValues[1]);
56
- value.setProperty(runtime, "z", newValues[2]);
57
- mutableObject->setValue(runtime, std::move(value));
58
- };
59
- }
32
+ auto shareableHandler = extractShareableOrThrow(rt, sensorDataHandler);
33
+ auto uiRuntime = runtimeHelper->uiRuntime();
60
34
 
61
35
  int sensorId = platformRegisterSensorFunction_(
62
- sensorType.asNumber(), interval.asNumber(), setter);
36
+ sensorType, interval.asNumber(), [=](double newValues[]) {
37
+ jsi::Runtime &rt = *uiRuntime;
38
+ auto handler =
39
+ shareableHandler->getJSValue(rt).asObject(rt).asFunction(rt);
40
+ if (sensorType == SensorType::ROTATION_VECTOR) {
41
+ jsi::Object value(rt);
42
+ value.setProperty(rt, "qx", newValues[0]);
43
+ value.setProperty(rt, "qy", newValues[1]);
44
+ value.setProperty(rt, "qz", newValues[2]);
45
+ value.setProperty(rt, "qw", newValues[3]);
46
+ value.setProperty(rt, "yaw", newValues[4]);
47
+ value.setProperty(rt, "pitch", newValues[5]);
48
+ value.setProperty(rt, "roll", newValues[6]);
49
+ handler.call(rt, value);
50
+ } else {
51
+ jsi::Object value(rt);
52
+ value.setProperty(rt, "x", newValues[0]);
53
+ value.setProperty(rt, "y", newValues[1]);
54
+ value.setProperty(rt, "z", newValues[2]);
55
+ handler.call(rt, value);
56
+ }
57
+ });
63
58
  if (sensorId != -1) {
64
59
  sensorsIds_.insert(sensorId);
65
60
  }
@@ -1,10 +1,12 @@
1
1
  #pragma once
2
2
 
3
3
  #include <jsi/jsi.h>
4
+ #include <memory>
4
5
  #include <unordered_set>
5
6
 
6
7
  #include "PlatformDepMethodsHolder.h"
7
8
  #include "RuntimeManager.h"
9
+ #include "Shareables.h"
8
10
 
9
11
  namespace reanimated {
10
12
 
@@ -22,16 +24,15 @@ class AnimatedSensorModule {
22
24
  std::unordered_set<int> sensorsIds_;
23
25
  RegisterSensorFunction platformRegisterSensorFunction_;
24
26
  UnregisterSensorFunction platformUnregisterSensorFunction_;
25
- RuntimeManager *runtimeManager_;
26
27
 
27
28
  public:
28
29
  AnimatedSensorModule(
29
- const PlatformDepMethodsHolder &platformDepMethodsHolder,
30
- RuntimeManager *runtimeManager);
30
+ const PlatformDepMethodsHolder &platformDepMethodsHolder);
31
31
  ~AnimatedSensorModule();
32
32
 
33
33
  jsi::Value registerSensor(
34
34
  jsi::Runtime &rt,
35
+ const std::shared_ptr<JSRuntimeHelper> &runtimeHelper,
35
36
  const jsi::Value &sensorType,
36
37
  const jsi::Value &interval,
37
38
  const jsi::Value &sensorDataContainer);
@@ -0,0 +1,76 @@
1
+ #include "LayoutAnimationsManager.h"
2
+ #include "Shareables.h"
3
+
4
+ #include <utility>
5
+
6
+ namespace reanimated {
7
+
8
+ void LayoutAnimationsManager::configureAnimation(
9
+ int tag,
10
+ const std::string &type,
11
+ std::shared_ptr<Shareable> config) {
12
+ auto lock = std::unique_lock<std::mutex>(animationsMutex_);
13
+ if (type == "entering") {
14
+ enteringAnimations_[tag] = config;
15
+ } else if (type == "exiting") {
16
+ exitingAnimations_[tag] = config;
17
+ } else if (type == "layout") {
18
+ layoutAnimations_[tag] = config;
19
+ }
20
+ }
21
+
22
+ bool LayoutAnimationsManager::hasLayoutAnimation(
23
+ int tag,
24
+ const std::string &type) {
25
+ auto lock = std::unique_lock<std::mutex>(animationsMutex_);
26
+ if (type == "entering") {
27
+ return enteringAnimations_.find(tag) != enteringAnimations_.end();
28
+ } else if (type == "exiting") {
29
+ return exitingAnimations_.find(tag) != exitingAnimations_.end();
30
+ } else if (type == "layout") {
31
+ return layoutAnimations_.find(tag) != layoutAnimations_.end();
32
+ }
33
+ return false;
34
+ }
35
+
36
+ void LayoutAnimationsManager::clearLayoutAnimationConfig(int tag) {
37
+ auto lock = std::unique_lock<std::mutex>(animationsMutex_);
38
+ enteringAnimations_.erase(tag);
39
+ exitingAnimations_.erase(tag);
40
+ layoutAnimations_.erase(tag);
41
+ }
42
+
43
+ void LayoutAnimationsManager::startLayoutAnimation(
44
+ jsi::Runtime &rt,
45
+ int tag,
46
+ const std::string &type,
47
+ const jsi::Object &values) {
48
+ std::shared_ptr<Shareable> config, viewShareable;
49
+ {
50
+ auto lock = std::unique_lock<std::mutex>(animationsMutex_);
51
+ if (type == "entering") {
52
+ config = enteringAnimations_[tag];
53
+ } else if (type == "exiting") {
54
+ config = exitingAnimations_[tag];
55
+ } else if (type == "layout") {
56
+ config = layoutAnimations_[tag];
57
+ }
58
+ }
59
+
60
+ // TODO: cache the following!!
61
+ jsi::Value layoutAnimationRepositoryAsValue =
62
+ rt.global()
63
+ .getPropertyAsObject(rt, "global")
64
+ .getProperty(rt, "LayoutAnimationsManager");
65
+ jsi::Function startAnimationForTag =
66
+ layoutAnimationRepositoryAsValue.getObject(rt).getPropertyAsFunction(
67
+ rt, "start");
68
+ startAnimationForTag.call(
69
+ rt,
70
+ jsi::Value(tag),
71
+ jsi::String::createFromAscii(rt, type),
72
+ values,
73
+ config->getJSValue(rt));
74
+ }
75
+
76
+ } // namespace reanimated
@@ -0,0 +1,41 @@
1
+ #pragma once
2
+
3
+ #include "ErrorHandler.h"
4
+ #include "Shareables.h"
5
+
6
+ #include <jsi/jsi.h>
7
+ #include <stdio.h>
8
+ #include <functional>
9
+ #include <memory>
10
+ #include <mutex>
11
+ #include <string>
12
+ #include <unordered_map>
13
+
14
+ namespace reanimated {
15
+
16
+ using namespace facebook;
17
+
18
+ class LayoutAnimationsManager {
19
+ public:
20
+ void configureAnimation(
21
+ int tag,
22
+ const std::string &type,
23
+ std::shared_ptr<Shareable> config);
24
+ bool hasLayoutAnimation(int tag, const std::string &type);
25
+ void startLayoutAnimation(
26
+ jsi::Runtime &rt,
27
+ int tag,
28
+ const std::string &type,
29
+ const jsi::Object &values);
30
+ void clearLayoutAnimationConfig(int tag);
31
+
32
+ private:
33
+ std::unordered_map<int, std::shared_ptr<Shareable>> enteringAnimations_;
34
+ std::unordered_map<int, std::shared_ptr<Shareable>> exitingAnimations_;
35
+ std::unordered_map<int, std::shared_ptr<Shareable>> layoutAnimations_;
36
+ mutable std::mutex
37
+ animationsMutex_; // Protects `enteringAnimations_`, `exitingAnimations_`,
38
+ // `layoutAnimations_` and `viewSharedValues_`.
39
+ };
40
+
41
+ } // namespace reanimated
@@ -19,84 +19,37 @@
19
19
 
20
20
  #include "EventHandlerRegistry.h"
21
21
  #include "FeaturesConfig.h"
22
- #include "FrozenObject.h"
23
- #include "JSIStoreValueUser.h"
24
- #include "Mapper.h"
25
- #include "MapperRegistry.h"
26
- #include "MutableValue.h"
27
22
  #include "ReanimatedHiddenHeaders.h"
28
23
  #include "RuntimeDecorator.h"
29
- #include "ShareableValue.h"
24
+ #include "Shareables.h"
30
25
  #include "WorkletEventHandler.h"
31
26
 
32
27
  using namespace facebook;
33
28
 
34
29
  namespace reanimated {
35
30
 
36
- void extractMutables(
37
- jsi::Runtime &rt,
38
- std::shared_ptr<ShareableValue> sv,
39
- std::vector<std::shared_ptr<MutableValue>> &res) {
40
- switch (sv->type) {
41
- case ValueType::MutableValueType: {
42
- auto &mutableValue = ValueWrapper::asMutableValue(sv->valueContainer);
43
- res.push_back(mutableValue);
44
- break;
45
- }
46
- case ValueType::FrozenArrayType:
47
- for (auto &it : ValueWrapper::asFrozenArray(sv->valueContainer)) {
48
- extractMutables(rt, it, res);
49
- }
50
- break;
51
- case ValueType::RemoteObjectType:
52
- case ValueType::FrozenObjectType:
53
- for (auto &it : ValueWrapper::asFrozenObject(sv->valueContainer)->map) {
54
- extractMutables(rt, it.second, res);
55
- }
56
- break;
57
- default:
58
- break;
59
- }
60
- }
61
-
62
- std::vector<std::shared_ptr<MutableValue>> extractMutablesFromArray(
63
- jsi::Runtime &rt,
64
- const jsi::Array &array,
65
- NativeReanimatedModule *module) {
66
- std::vector<std::shared_ptr<MutableValue>> res;
67
- for (size_t i = 0, size = array.size(rt); i < size; i++) {
68
- auto shareable =
69
- ShareableValue::adapt(rt, array.getValueAtIndex(rt, i), module);
70
- extractMutables(rt, shareable, res);
71
- }
72
- return res;
73
- }
74
-
75
31
  NativeReanimatedModule::NativeReanimatedModule(
76
- std::shared_ptr<CallInvoker> jsInvoker,
77
- std::shared_ptr<Scheduler> scheduler,
78
- std::shared_ptr<jsi::Runtime> rt,
79
- std::shared_ptr<ErrorHandler> errorHandler,
32
+ const std::shared_ptr<CallInvoker> &jsInvoker,
33
+ const std::shared_ptr<Scheduler> &scheduler,
34
+ const std::shared_ptr<jsi::Runtime> &rt,
35
+ const std::shared_ptr<ErrorHandler> &errorHandler,
80
36
  #ifdef RCT_NEW_ARCH_ENABLED
81
37
  // nothing
82
38
  #else
83
39
  std::function<jsi::Value(jsi::Runtime &, const int, const jsi::String &)>
84
40
  propObtainer,
85
41
  #endif
86
- std::shared_ptr<LayoutAnimationsProxy> layoutAnimationsProxy,
87
42
  PlatformDepMethodsHolder platformDepMethodsHolder)
88
43
  : NativeReanimatedModuleSpec(jsInvoker),
89
44
  RuntimeManager(rt, errorHandler, scheduler, RuntimeType::UI),
90
- layoutAnimationsProxy_(layoutAnimationsProxy),
91
- mapperRegistry(std::make_shared<MapperRegistry>()),
92
- eventHandlerRegistry(std::make_shared<EventHandlerRegistry>()),
45
+ eventHandlerRegistry(std::make_unique<EventHandlerRegistry>()),
93
46
  requestRender(platformDepMethodsHolder.requestRender),
94
47
  #ifdef RCT_NEW_ARCH_ENABLED
95
48
  // nothing
96
49
  #else
97
50
  propObtainer(propObtainer),
98
51
  #endif
99
- animatedSensorModule(platformDepMethodsHolder, this),
52
+ animatedSensorModule(platformDepMethodsHolder),
100
53
  #ifdef RCT_NEW_ARCH_ENABLED
101
54
  synchronouslyUpdateUIPropsFunction(
102
55
  platformDepMethodsHolder.synchronouslyUpdateUIPropsFunction)
@@ -110,7 +63,50 @@ NativeReanimatedModule::NativeReanimatedModule(
110
63
  maybeRequestRender();
111
64
  };
112
65
 
113
- this->layoutAnimationsProxy_ = layoutAnimationsProxy;
66
+ auto scheduleOnJS = [this](
67
+ jsi::Runtime &rt,
68
+ const jsi::Value &remoteFun,
69
+ const jsi::Value &argsValue) {
70
+ auto shareableRemoteFun = extractShareableOrThrow<ShareableRemoteFunction>(
71
+ rt,
72
+ remoteFun,
73
+ "Incompatible object passed to scheduleOnJS. It is only allowed to schedule functions defined on the React Native JS runtime this way.");
74
+ auto shareableArgs = argsValue.isUndefined()
75
+ ? nullptr
76
+ : extractShareableOrThrow(rt, argsValue);
77
+ auto jsRuntime = this->runtimeHelper->rnRuntime();
78
+ this->scheduler->scheduleOnJS([=] {
79
+ jsi::Runtime &rt = *jsRuntime;
80
+ auto remoteFun = shareableRemoteFun->getJSValue(rt);
81
+ if (shareableArgs == nullptr) {
82
+ // fast path for remote function w/o arguments
83
+ remoteFun.asObject(rt).asFunction(rt).call(rt);
84
+ } else {
85
+ auto argsArray = shareableArgs->getJSValue(rt).asObject(rt).asArray(rt);
86
+ auto argsSize = argsArray.size(rt);
87
+ // number of arguments is typically relatively small so it is ok to
88
+ // to use VLAs here, hence disabling the lint rule
89
+ jsi::Value args[argsSize]; // NOLINT(runtime/arrays)
90
+ for (size_t i = 0; i < argsSize; i++) {
91
+ args[i] = argsArray.getValueAtIndex(rt, i);
92
+ }
93
+ remoteFun.asObject(rt).asFunction(rt).call(rt, args, argsSize);
94
+ }
95
+ });
96
+ };
97
+
98
+ auto makeShareableClone = [this](jsi::Runtime &rt, const jsi::Value &value) {
99
+ return this->makeShareableClone(rt, value);
100
+ };
101
+
102
+ auto updateDataSynchronously =
103
+ [this](
104
+ jsi::Runtime &rt,
105
+ const jsi::Value &synchronizedDataHolderRef,
106
+ const jsi::Value &newData) {
107
+ return this->updateDataSynchronously(
108
+ rt, synchronizedDataHolderRef, newData);
109
+ };
114
110
 
115
111
  #ifdef RCT_NEW_ARCH_ENABLED
116
112
  auto updateProps = [this](
@@ -151,11 +147,15 @@ NativeReanimatedModule::NativeReanimatedModule(
151
147
  platformDepMethodsHolder.scrollToFunction,
152
148
  #endif
153
149
  requestAnimationFrame,
150
+ scheduleOnJS,
151
+ makeShareableClone,
152
+ updateDataSynchronously,
154
153
  platformDepMethodsHolder.getCurrentTime,
155
154
  platformDepMethodsHolder.registerSensor,
156
155
  platformDepMethodsHolder.unregisterSensor,
157
156
  platformDepMethodsHolder.setGestureStateFunction,
158
- layoutAnimationsProxy);
157
+ platformDepMethodsHolder.progressLayoutAnimation,
158
+ platformDepMethodsHolder.endLayoutAnimation);
159
159
  onRenderCallback = [this](double timestampMs) {
160
160
  this->renderRequested = false;
161
161
  this->onRender(timestampMs);
@@ -174,87 +174,109 @@ NativeReanimatedModule::NativeReanimatedModule(
174
174
 
175
175
  void NativeReanimatedModule::installCoreFunctions(
176
176
  jsi::Runtime &rt,
177
- const jsi::Value &valueSetter) {
178
- this->valueSetter = ShareableValue::adapt(rt, valueSetter, this);
177
+ const jsi::Value &valueUnpacker,
178
+ const jsi::Value &layoutAnimationStartFunction) {
179
+ if (!runtimeHelper) {
180
+ // initialize runtimeHelper here if not already present. We expect only one
181
+ // instace of the helper to exists.
182
+ runtimeHelper =
183
+ std::make_shared<JSRuntimeHelper>(&rt, this->runtime.get(), scheduler);
184
+ }
185
+ runtimeHelper->valueUnpacker =
186
+ std::make_shared<CoreFunction>(runtimeHelper.get(), valueUnpacker);
187
+ }
188
+
189
+ NativeReanimatedModule::~NativeReanimatedModule() {
190
+ if (runtimeHelper) {
191
+ runtimeHelper->valueUnpacker = nullptr;
192
+ // event handler registry stores some JSI values from UI runtime, so it has
193
+ // to go away before we tear down the runtime
194
+ eventHandlerRegistry.reset();
195
+ runtime.reset();
196
+ // make sure uiRuntimeDestroyed is set after the runtime is deallocated
197
+ runtimeHelper->uiRuntimeDestroyed = true;
198
+ }
179
199
  }
180
200
 
181
- jsi::Value NativeReanimatedModule::makeShareable(
201
+ void NativeReanimatedModule::scheduleOnUI(
182
202
  jsi::Runtime &rt,
183
- const jsi::Value &value) {
184
- return ShareableValue::adapt(rt, value, this)->getValue(rt);
203
+ const jsi::Value &worklet) {
204
+ auto shareableWorklet = extractShareableOrThrow(rt, worklet);
205
+ assert(
206
+ shareableWorklet->valueType() == Shareable::WorkletType &&
207
+ "only worklets can be scheduled to run on UI");
208
+ auto uiRuntime = runtimeHelper->uiRuntime();
209
+ frameCallbacks.push_back([=](double timestamp) {
210
+ jsi::Runtime &rt = *uiRuntime;
211
+ auto workletValue = shareableWorklet->getJSValue(rt);
212
+ workletValue.asObject(rt).asFunction(rt).call(rt);
213
+ });
214
+ maybeRequestRender();
185
215
  }
186
216
 
187
- jsi::Value NativeReanimatedModule::makeMutable(
217
+ jsi::Value NativeReanimatedModule::makeSynchronizedDataHolder(
188
218
  jsi::Runtime &rt,
189
- const jsi::Value &value) {
190
- return ShareableValue::adapt(rt, value, this, ValueType::MutableValueType)
191
- ->getValue(rt);
219
+ const jsi::Value &initialShareable) {
220
+ auto dataHolder = std::make_shared<ShareableSynchronizedDataHolder>(
221
+ runtimeHelper, rt, initialShareable);
222
+ return dataHolder->getJSValue(rt);
192
223
  }
193
224
 
194
- jsi::Value NativeReanimatedModule::makeRemote(
225
+ void NativeReanimatedModule::updateDataSynchronously(
195
226
  jsi::Runtime &rt,
196
- const jsi::Value &value) {
197
- return ShareableValue::adapt(rt, value, this, ValueType::RemoteObjectType)
198
- ->getValue(rt);
227
+ const jsi::Value &synchronizedDataHolderRef,
228
+ const jsi::Value &newData) {
229
+ auto dataHolder = extractShareableOrThrow<ShareableSynchronizedDataHolder>(
230
+ rt, synchronizedDataHolderRef);
231
+ dataHolder->set(rt, newData);
199
232
  }
200
233
 
201
- jsi::Value NativeReanimatedModule::startMapper(
234
+ jsi::Value NativeReanimatedModule::getDataSynchronously(
202
235
  jsi::Runtime &rt,
203
- const jsi::Value &worklet,
204
- const jsi::Value &inputs,
205
- const jsi::Value &outputs,
206
- const jsi::Value &updater,
207
- const jsi::Value &viewDescriptors) {
208
- static unsigned long MAPPER_ID = 1;
209
-
210
- unsigned long newMapperId = MAPPER_ID++;
211
- auto mapperShareable = ShareableValue::adapt(rt, worklet, this);
212
- auto inputMutables =
213
- extractMutablesFromArray(rt, inputs.asObject(rt).asArray(rt), this);
214
- auto outputMutables =
215
- extractMutablesFromArray(rt, outputs.asObject(rt).asArray(rt), this);
216
-
217
- int optimalizationLvl = 0;
218
- auto optimalization =
219
- updater.asObject(rt).getProperty(rt, "__optimalization");
220
- if (optimalization.isNumber()) {
221
- optimalizationLvl = optimalization.asNumber();
222
- }
223
- auto updaterSV = ShareableValue::adapt(rt, updater, this);
224
- auto viewDescriptorsSV = ShareableValue::adapt(rt, viewDescriptors, this);
225
-
226
- scheduler->scheduleOnUI([=] {
227
- auto mapperFunction =
228
- mapperShareable->getValue(*runtime).asObject(*runtime).asFunction(
229
- *runtime);
230
- std::shared_ptr<jsi::Function> mapperFunctionPointer =
231
- std::make_shared<jsi::Function>(std::move(mapperFunction));
232
-
233
- std::shared_ptr<Mapper> mapperPointer = std::make_shared<Mapper>(
234
- this,
235
- newMapperId,
236
- mapperFunctionPointer,
237
- inputMutables,
238
- outputMutables);
239
- if (optimalizationLvl > 0) {
240
- mapperPointer->enableFastMode(
241
- optimalizationLvl, updaterSV, viewDescriptorsSV);
242
- }
243
- mapperRegistry->startMapper(mapperPointer);
244
- maybeRequestRender();
245
- });
246
-
247
- return jsi::Value(static_cast<double>(newMapperId));
236
+ const jsi::Value &synchronizedDataHolderRef) {
237
+ auto dataHolder = extractShareableOrThrow<ShareableSynchronizedDataHolder>(
238
+ rt, synchronizedDataHolderRef);
239
+ return dataHolder->get(rt);
248
240
  }
249
241
 
250
- void NativeReanimatedModule::stopMapper(
242
+ jsi::Value NativeReanimatedModule::makeShareableClone(
251
243
  jsi::Runtime &rt,
252
- const jsi::Value &mapperId) {
253
- unsigned long id = mapperId.asNumber();
254
- scheduler->scheduleOnUI([=] {
255
- mapperRegistry->stopMapper(id);
256
- maybeRequestRender();
257
- });
244
+ const jsi::Value &value) {
245
+ std::shared_ptr<Shareable> shareable;
246
+ if (value.isObject()) {
247
+ auto object = value.asObject(rt);
248
+ if (!object.getProperty(rt, "__workletHash").isUndefined()) {
249
+ shareable = std::make_shared<ShareableWorklet>(runtimeHelper, rt, object);
250
+ } else if (!object.getProperty(rt, "__init").isUndefined()) {
251
+ shareable = std::make_shared<ShareableHandle>(runtimeHelper, rt, object);
252
+ } else if (object.isFunction(rt)) {
253
+ shareable = std::make_shared<ShareableRemoteFunction>(
254
+ runtimeHelper, rt, object.asFunction(rt));
255
+ } else if (object.isArray(rt)) {
256
+ shareable = std::make_shared<ShareableArray>(
257
+ runtimeHelper, rt, object.asArray(rt));
258
+ #ifdef RCT_NEW_ARCH_ENABLED
259
+ } else if (object.isHostObject<ShadowNodeWrapper>(rt)) {
260
+ shareable = std::make_shared<ShareableShadowNodeWrapper>(
261
+ runtimeHelper, rt, object);
262
+ #endif
263
+ } else {
264
+ shareable = std::make_shared<ShareableObject>(runtimeHelper, rt, object);
265
+ }
266
+ } else if (value.isString()) {
267
+ shareable = std::make_shared<ShareableString>(rt, value.asString(rt));
268
+ } else if (value.isUndefined()) {
269
+ shareable = std::make_shared<ShareableScalar>();
270
+ } else if (value.isNull()) {
271
+ shareable = std::make_shared<ShareableScalar>(nullptr);
272
+ } else if (value.isBool()) {
273
+ shareable = std::make_shared<ShareableScalar>(value.getBool());
274
+ } else if (value.isNumber()) {
275
+ shareable = std::make_shared<ShareableScalar>(value.getNumber());
276
+ } else {
277
+ throw std::runtime_error("attempted to convert an unsupported value type");
278
+ }
279
+ return ShareableJSRef::newHostObject(rt, shareable);
258
280
  }
259
281
 
260
282
  jsi::Value NativeReanimatedModule::registerEventHandler(
@@ -265,12 +287,12 @@ jsi::Value NativeReanimatedModule::registerEventHandler(
265
287
 
266
288
  unsigned long newRegistrationId = EVENT_HANDLER_ID++;
267
289
  auto eventName = eventHash.asString(rt).utf8(rt);
268
- auto handlerShareable = ShareableValue::adapt(rt, worklet, this);
290
+ auto handlerShareable = extractShareableOrThrow(rt, worklet);
269
291
 
270
292
  scheduler->scheduleOnUI([=] {
293
+ jsi::Runtime &rt = *runtimeHelper->uiRuntime();
271
294
  auto handlerFunction =
272
- handlerShareable->getValue(*runtime).asObject(*runtime).asFunction(
273
- *runtime);
295
+ handlerShareable->getJSValue(rt).asObject(rt).asFunction(rt);
274
296
  auto handler = std::make_shared<WorkletEventHandler>(
275
297
  newRegistrationId, eventName, std::move(handlerFunction));
276
298
  eventHandlerRegistry->registerEventHandler(handler);
@@ -342,13 +364,11 @@ jsi::Value NativeReanimatedModule::configureLayoutAnimation(
342
364
  jsi::Runtime &rt,
343
365
  const jsi::Value &viewTag,
344
366
  const jsi::Value &type,
345
- const jsi::Value &config,
346
- const jsi::Value &viewSharedValue) {
347
- layoutAnimationsProxy_->configureAnimation(
367
+ const jsi::Value &config) {
368
+ layoutAnimationsManager_.configureAnimation(
348
369
  viewTag.asNumber(),
349
370
  type.asString(rt).utf8(rt),
350
- ShareableValue::adapt(rt, config, this),
351
- ShareableValue::adapt(rt, viewSharedValue, this));
371
+ extractShareableOrThrow(rt, config));
352
372
  return jsi::Value::undefined();
353
373
  }
354
374
 
@@ -366,10 +386,6 @@ void NativeReanimatedModule::onEvent(
366
386
  #else
367
387
  eventHandlerRegistry->processEvent(*runtime, eventName, eventAsString);
368
388
  #endif
369
- mapperRegistry->execute(*runtime);
370
- if (mapperRegistry->needRunOnRender()) {
371
- maybeRequestRender();
372
- }
373
389
  } catch (std::exception &e) {
374
390
  std::string str = e.what();
375
391
  this->errorHandler->setError(str);
@@ -400,11 +416,6 @@ void NativeReanimatedModule::onRender(double timestampMs) {
400
416
  for (auto &callback : callbacks) {
401
417
  callback(timestampMs);
402
418
  }
403
- mapperRegistry->execute(*runtime);
404
-
405
- if (mapperRegistry->needRunOnRender()) {
406
- maybeRequestRender();
407
- }
408
419
  } catch (std::exception &e) {
409
420
  std::string str = e.what();
410
421
  this->errorHandler->setError(str);
@@ -420,9 +431,9 @@ jsi::Value NativeReanimatedModule::registerSensor(
420
431
  jsi::Runtime &rt,
421
432
  const jsi::Value &sensorType,
422
433
  const jsi::Value &interval,
423
- const jsi::Value &sensorDataContainer) {
434
+ const jsi::Value &sensorDataHandler) {
424
435
  return animatedSensorModule.registerSensor(
425
- rt, sensorType, interval, sensorDataContainer);
436
+ rt, runtimeHelper, sensorType, interval, sensorDataHandler);
426
437
  }
427
438
 
428
439
  void NativeReanimatedModule::unregisterSensor(
@@ -645,30 +656,15 @@ void NativeReanimatedModule::setNewestShadowNodesRegistry(
645
656
 
646
657
  jsi::Value NativeReanimatedModule::subscribeForKeyboardEvents(
647
658
  jsi::Runtime &rt,
648
- const jsi::Value &keyboardEventContainer) {
649
- jsi::Object keyboardEventObj = keyboardEventContainer.getObject(rt);
650
- std::unordered_map<std::string, std::shared_ptr<ShareableValue>>
651
- sharedProperties;
652
- std::shared_ptr<ShareableValue> keyboardStateShared = ShareableValue::adapt(
653
- rt, keyboardEventObj.getProperty(rt, "state"), this);
654
- std::shared_ptr<ShareableValue> heightShared = ShareableValue::adapt(
655
- rt, keyboardEventObj.getProperty(rt, "height"), this);
656
-
657
- auto keyboardEventDataUpdater =
658
- [this, &rt, keyboardStateShared, heightShared](
659
- int keyboardState, int height) {
660
- auto &keyboardStateValue =
661
- ValueWrapper::asMutableValue(keyboardStateShared->valueContainer);
662
- keyboardStateValue->setValue(rt, jsi::Value(keyboardState));
663
-
664
- auto &heightMutableValue =
665
- ValueWrapper::asMutableValue(heightShared->valueContainer);
666
- heightMutableValue->setValue(rt, jsi::Value(height));
667
-
668
- this->mapperRegistry->execute(*this->runtime);
669
- };
670
-
671
- return subscribeForKeyboardEventsFunction(keyboardEventDataUpdater);
659
+ const jsi::Value &handlerWorklet) {
660
+ auto shareableHandler = extractShareableOrThrow(rt, handlerWorklet);
661
+ auto uiRuntime = runtimeHelper->uiRuntime();
662
+ return subscribeForKeyboardEventsFunction([=](int keyboardState, int height) {
663
+ jsi::Runtime &rt = *uiRuntime;
664
+ auto handler = shareableHandler->getJSValue(rt);
665
+ handler.asObject(rt).asFunction(rt).call(
666
+ rt, jsi::Value(keyboardState), jsi::Value(height));
667
+ });
672
668
  }
673
669
 
674
670
  void NativeReanimatedModule::unsubscribeFromKeyboardEvents(