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
@@ -0,0 +1,455 @@
1
+ #pragma once
2
+
3
+ #include <jsi/jsi.h>
4
+ #include <memory>
5
+ #include <string>
6
+ #include <utility>
7
+ #include <vector>
8
+
9
+ #ifdef RCT_NEW_ARCH_ENABLED
10
+ #include <react/renderer/uimanager/primitives.h>
11
+ #endif
12
+
13
+ #include "ReanimatedRuntime.h"
14
+ #include "RuntimeManager.h"
15
+ #include "Scheduler.h"
16
+
17
+ #define HAS_JS_WEAK_OBJECTS JS_RUNTIME_HERMES
18
+
19
+ using namespace facebook;
20
+
21
+ namespace reanimated {
22
+
23
+ class CoreFunction;
24
+
25
+ class JSRuntimeHelper {
26
+ private:
27
+ jsi::Runtime *rnRuntime_; // React-Native's main JS runtime
28
+ jsi::Runtime *uiRuntime_; // UI runtime created by Reanimated
29
+ std::shared_ptr<Scheduler> scheduler_;
30
+
31
+ public:
32
+ JSRuntimeHelper(
33
+ jsi::Runtime *rnRuntime,
34
+ jsi::Runtime *uiRuntime,
35
+ const std::shared_ptr<Scheduler> &scheduler)
36
+ : rnRuntime_(rnRuntime), uiRuntime_(uiRuntime), scheduler_(scheduler) {}
37
+
38
+ volatile bool uiRuntimeDestroyed;
39
+ std::shared_ptr<CoreFunction> valueUnpacker;
40
+
41
+ inline jsi::Runtime *uiRuntime() const {
42
+ return uiRuntime_;
43
+ }
44
+
45
+ inline jsi::Runtime *rnRuntime() const {
46
+ return rnRuntime_;
47
+ }
48
+
49
+ inline bool isUIRuntime(const jsi::Runtime &rt) const {
50
+ return &rt == uiRuntime_;
51
+ }
52
+
53
+ inline bool isRNRuntime(const jsi::Runtime &rt) const {
54
+ return &rt == rnRuntime_;
55
+ }
56
+
57
+ void scheduleOnUI(std::function<void()> job) {
58
+ scheduler_->scheduleOnUI(job);
59
+ }
60
+
61
+ void scheduleOnJS(std::function<void()> job) {
62
+ scheduler_->scheduleOnJS(job);
63
+ }
64
+ };
65
+
66
+ // Core functions are not allowed to capture outside variables, otherwise they'd
67
+ // try to access _closure variable which is something we want to avoid for
68
+ // simplicity reasons.
69
+ class CoreFunction {
70
+ private:
71
+ std::unique_ptr<jsi::Function> rnFunction_;
72
+ std::unique_ptr<jsi::Function> uiFunction_;
73
+ std::string functionBody_;
74
+ std::string location_;
75
+ JSRuntimeHelper
76
+ *runtimeHelper_; // runtime helper holds core function references, so we
77
+ // use normal pointer here to avoid ref cycles.
78
+ std::unique_ptr<jsi::Function> &getFunction(jsi::Runtime &rt);
79
+
80
+ public:
81
+ CoreFunction(JSRuntimeHelper *runtimeHelper, const jsi::Value &workletObject);
82
+ template <typename... Args>
83
+ jsi::Value call(jsi::Runtime &rt, Args &&...args) {
84
+ return getFunction(rt)->call(rt, args...);
85
+ }
86
+ };
87
+
88
+ class Shareable {
89
+ protected:
90
+ virtual jsi::Value toJSValue(jsi::Runtime &rt) = 0;
91
+
92
+ public:
93
+ virtual ~Shareable();
94
+
95
+ enum ValueType {
96
+ UndefinedType,
97
+ NullType,
98
+ BooleanType,
99
+ NumberType,
100
+ // SymbolType, TODO
101
+ // BigIntType, TODO
102
+ StringType,
103
+ ObjectType,
104
+ ArrayType,
105
+ WorkletType,
106
+ RemoteFunctionType,
107
+ HandleType,
108
+ SynchronizedDataHolder,
109
+ #ifdef RCT_NEW_ARCH_ENABLED
110
+ ShadowNode,
111
+ #endif
112
+ };
113
+
114
+ explicit Shareable(ValueType valueType) : valueType_(valueType) {}
115
+ virtual jsi::Value getJSValue(jsi::Runtime &rt) {
116
+ return toJSValue(rt);
117
+ }
118
+
119
+ inline ValueType valueType() const {
120
+ return valueType_;
121
+ }
122
+
123
+ static std::shared_ptr<Shareable> undefined();
124
+
125
+ protected:
126
+ ValueType valueType_;
127
+ };
128
+
129
+ class RetainingShareable : public Shareable {
130
+ protected:
131
+ std::shared_ptr<JSRuntimeHelper> runtimeHelper_;
132
+
133
+ #if HAS_JS_WEAK_OBJECTS
134
+
135
+ private:
136
+ std::unique_ptr<jsi::WeakObject> remoteValue_;
137
+
138
+ public:
139
+ jsi::Value getJSValue(jsi::Runtime &rt) final;
140
+
141
+ RetainingShareable(
142
+ const std::shared_ptr<JSRuntimeHelper> &runtimeHelper,
143
+ jsi::Runtime &rt,
144
+ ValueType valueType)
145
+ : Shareable(valueType), runtimeHelper_(runtimeHelper) {}
146
+
147
+ ~RetainingShareable();
148
+ #else
149
+
150
+ public:
151
+ RetainingShareable(
152
+ const std::shared_ptr<JSRuntimeHelper> &runtimeHelper,
153
+ jsi::Runtime &rt,
154
+ ValueType valueType)
155
+ : Shareable(valueType), runtimeHelper_(runtimeHelper) {}
156
+ #endif
157
+ };
158
+
159
+ class ShareableJSRef : public jsi::HostObject {
160
+ private:
161
+ std::shared_ptr<Shareable> value_;
162
+
163
+ public:
164
+ explicit ShareableJSRef(std::shared_ptr<Shareable> value) : value_(value) {}
165
+ std::shared_ptr<Shareable> value() const {
166
+ return value_;
167
+ }
168
+
169
+ static jsi::Object newHostObject(
170
+ jsi::Runtime &rt,
171
+ const std::shared_ptr<Shareable> &value) {
172
+ return jsi::Object::createFromHostObject(
173
+ rt, std::make_shared<ShareableJSRef>(value));
174
+ }
175
+ };
176
+
177
+ std::shared_ptr<Shareable> extractShareableOrThrow(
178
+ jsi::Runtime &rt,
179
+ const jsi::Value &maybeShareableValue,
180
+ const char *errorMessage = nullptr);
181
+
182
+ template <typename T>
183
+ std::shared_ptr<T> extractShareableOrThrow(
184
+ jsi::Runtime &rt,
185
+ const jsi::Value &shareableRef,
186
+ const char *errorMessage = nullptr) {
187
+ auto res = std::dynamic_pointer_cast<T>(
188
+ extractShareableOrThrow(rt, shareableRef, errorMessage));
189
+ if (!res) {
190
+ throw new std::runtime_error(
191
+ errorMessage != nullptr
192
+ ? errorMessage
193
+ : "provided shareable object is of an incompatible type");
194
+ }
195
+ return res;
196
+ }
197
+
198
+ class ShareableArray : public RetainingShareable {
199
+ public:
200
+ ShareableArray(
201
+ const std::shared_ptr<JSRuntimeHelper> &runtimeHelper,
202
+ jsi::Runtime &rt,
203
+ const jsi::Array &array);
204
+
205
+ jsi::Value toJSValue(jsi::Runtime &rt) override {
206
+ auto size = data_.size();
207
+ auto ary = jsi::Array(rt, size);
208
+ for (size_t i = 0; i < size; i++) {
209
+ ary.setValueAtIndex(rt, i, data_[i]->getJSValue(rt));
210
+ }
211
+ return ary;
212
+ }
213
+
214
+ protected:
215
+ std::vector<std::shared_ptr<Shareable>> data_;
216
+ };
217
+
218
+ class ShareableObject : public RetainingShareable {
219
+ public:
220
+ ShareableObject(
221
+ const std::shared_ptr<JSRuntimeHelper> &runtimeHelper,
222
+ jsi::Runtime &rt,
223
+ const jsi::Object &object);
224
+ jsi::Value toJSValue(jsi::Runtime &rt) override {
225
+ auto obj = jsi::Object(rt);
226
+ for (size_t i = 0, size = data_.size(); i < size; i++) {
227
+ obj.setProperty(
228
+ rt, data_[i].first.c_str(), data_[i].second->getJSValue(rt));
229
+ }
230
+ return obj;
231
+ }
232
+
233
+ protected:
234
+ std::vector<std::pair<std::string, std::shared_ptr<Shareable>>> data_;
235
+ };
236
+
237
+ #ifdef RCT_NEW_ARCH_ENABLED
238
+ class ShareableShadowNodeWrapper : public Shareable {
239
+ private:
240
+ react::ShadowNode::Shared shadowNode_;
241
+
242
+ public:
243
+ ShareableShadowNodeWrapper(
244
+ const std::shared_ptr<JSRuntimeHelper> &runtimeHelper,
245
+ jsi::Runtime &rt,
246
+ const jsi::Object &wrapperObject)
247
+ : Shareable(ShadowNode) {
248
+ shadowNode_ =
249
+ wrapperObject.getHostObject<ShadowNodeWrapper>(rt)->shadowNode;
250
+ }
251
+ jsi::Value toJSValue(jsi::Runtime &rt) override {
252
+ return jsi::Object::createFromHostObject(
253
+ rt, std::make_shared<ShadowNodeWrapper>(shadowNode_));
254
+ }
255
+ };
256
+ #endif
257
+
258
+ class ShareableWorklet : public ShareableObject {
259
+ public:
260
+ ShareableWorklet(
261
+ const std::shared_ptr<JSRuntimeHelper> &runtimeHelper,
262
+ jsi::Runtime &rt,
263
+ const jsi::Object &worklet)
264
+ : ShareableObject(runtimeHelper, rt, worklet) {
265
+ valueType_ = WorkletType;
266
+ }
267
+ jsi::Value toJSValue(jsi::Runtime &rt) override {
268
+ jsi::Value obj = ShareableObject::toJSValue(rt);
269
+ return runtimeHelper_->valueUnpacker->call(rt, obj);
270
+ }
271
+ };
272
+
273
+ class ShareableRemoteFunction
274
+ : public RetainingShareable,
275
+ public std::enable_shared_from_this<ShareableRemoteFunction> {
276
+ private:
277
+ jsi::Function function_;
278
+
279
+ public:
280
+ ShareableRemoteFunction(
281
+ const std::shared_ptr<JSRuntimeHelper> &runtimeHelper,
282
+ jsi::Runtime &rt,
283
+ jsi::Function &&function)
284
+ : RetainingShareable(runtimeHelper, rt, RemoteFunctionType),
285
+ function_(std::move(function)) {}
286
+ jsi::Value toJSValue(jsi::Runtime &rt) override {
287
+ if (runtimeHelper_->isUIRuntime(rt)) {
288
+ #ifdef DEBUG
289
+ return runtimeHelper_->valueUnpacker->call(
290
+ rt,
291
+ ShareableJSRef::newHostObject(rt, shared_from_this()),
292
+ jsi::String::createFromAscii(rt, "RemoteFunction"));
293
+ #else
294
+ return ShareableJSRef::newHostObject(rt, shared_from_this());
295
+ #endif
296
+ } else {
297
+ return jsi::Value(rt, function_);
298
+ }
299
+ }
300
+ };
301
+
302
+ class ShareableHandle : public Shareable {
303
+ private:
304
+ std::shared_ptr<JSRuntimeHelper> runtimeHelper_;
305
+ std::unique_ptr<ShareableObject> initializer_;
306
+ std::unique_ptr<jsi::Value> remoteValue_;
307
+
308
+ public:
309
+ ShareableHandle(
310
+ const std::shared_ptr<JSRuntimeHelper> runtimeHelper,
311
+ jsi::Runtime &rt,
312
+ const jsi::Object &initializerObject)
313
+ : Shareable(HandleType), runtimeHelper_(runtimeHelper) {
314
+ initializer_ =
315
+ std::make_unique<ShareableObject>(runtimeHelper, rt, initializerObject);
316
+ }
317
+ ~ShareableHandle() {
318
+ if (runtimeHelper_->uiRuntimeDestroyed) {
319
+ // The below use of unique_ptr.release prevents the smart pointer from
320
+ // calling the destructor of the kept object. This effectively results in
321
+ // leaking some memory. We do this on purpose, as sometimes we would keep
322
+ // references to JSI objects past the lifetime of its runtime (e.g.,
323
+ // shared values references from the RN VM holds reference to JSI objects
324
+ // on the UI runtime). When the UI runtime is terminated, the orphaned JSI
325
+ // objects would crash the app when their destructors are called, because
326
+ // they call into a memory that's managed by the terminated runtime. We
327
+ // accept the tradeoff of leaking memory here, as it has a limited impact.
328
+ // This scenario can only occur when the React instance is torn down which
329
+ // happens in development mode during app reloads, or in production when
330
+ // the app is being shut down gracefully by the system. An alternative
331
+ // solution would require us to keep track of all JSI values that are in
332
+ // use which would require additional data structure and compute spent on
333
+ // bookkeeping that only for the sake of destroying the values in time
334
+ // before the runtime is terminated. Note that the underlying memory that
335
+ // jsi::Value refers to is managed by the VM and gets freed along with the
336
+ // runtime.
337
+ remoteValue_.release();
338
+ }
339
+ }
340
+ jsi::Value toJSValue(jsi::Runtime &rt) override {
341
+ if (initializer_ != nullptr) {
342
+ auto initObj = initializer_->getJSValue(rt);
343
+ remoteValue_ = std::make_unique<jsi::Value>(
344
+ runtimeHelper_->valueUnpacker->call(rt, initObj));
345
+ initializer_ = nullptr; // we can release ref to initializer as this
346
+ // method should be called at most once
347
+ }
348
+ return jsi::Value(rt, *remoteValue_);
349
+ }
350
+ };
351
+
352
+ class ShareableSynchronizedDataHolder
353
+ : public Shareable,
354
+ public std::enable_shared_from_this<ShareableSynchronizedDataHolder> {
355
+ private:
356
+ std::shared_ptr<JSRuntimeHelper> runtimeHelper_;
357
+ std::shared_ptr<Shareable> data_;
358
+ std::shared_ptr<jsi::Value> uiValue_;
359
+ std::shared_ptr<jsi::Value> rnValue_;
360
+ std::mutex dataAccessLock_;
361
+
362
+ public:
363
+ ShareableSynchronizedDataHolder(
364
+ std::shared_ptr<JSRuntimeHelper> runtimeHelper,
365
+ jsi::Runtime &rt,
366
+ const jsi::Value &initialValue)
367
+ : Shareable(SynchronizedDataHolder),
368
+ runtimeHelper_(runtimeHelper),
369
+ data_(extractShareableOrThrow(rt, initialValue)) {}
370
+
371
+ jsi::Value get(jsi::Runtime &rt) {
372
+ std::unique_lock<std::mutex> read_lock(dataAccessLock_);
373
+ if (runtimeHelper_->isUIRuntime(rt)) {
374
+ if (uiValue_ == nullptr) {
375
+ auto value = data_->getJSValue(rt);
376
+ uiValue_ = std::make_shared<jsi::Value>(rt, value);
377
+ return value;
378
+ } else {
379
+ return jsi::Value(rt, *uiValue_);
380
+ }
381
+ } else {
382
+ if (rnValue_ == nullptr) {
383
+ auto value = data_->getJSValue(rt);
384
+ rnValue_ = std::make_shared<jsi::Value>(rt, value);
385
+ return value;
386
+ } else {
387
+ return jsi::Value(rt, *rnValue_);
388
+ }
389
+ }
390
+ }
391
+
392
+ void set(jsi::Runtime &rt, const jsi::Value &data) {
393
+ std::unique_lock<std::mutex> write_lock(dataAccessLock_);
394
+ data_ = extractShareableOrThrow(rt, data);
395
+ uiValue_.reset();
396
+ rnValue_.reset();
397
+ }
398
+
399
+ jsi::Value toJSValue(jsi::Runtime &rt) override {
400
+ return ShareableJSRef::newHostObject(rt, shared_from_this());
401
+ };
402
+ };
403
+
404
+ class ShareableString : public Shareable {
405
+ public:
406
+ ShareableString(jsi::Runtime &rt, const jsi::String &string)
407
+ : Shareable(StringType) {
408
+ data = string.utf8(rt);
409
+ }
410
+ jsi::Value toJSValue(jsi::Runtime &rt) override {
411
+ return jsi::String::createFromUtf8(rt, data);
412
+ }
413
+
414
+ protected:
415
+ std::string data;
416
+ };
417
+
418
+ class ShareableScalar : public Shareable {
419
+ public:
420
+ explicit ShareableScalar(double number) : Shareable(NumberType) {
421
+ data_.number = number;
422
+ }
423
+ explicit ShareableScalar(bool boolean) : Shareable(BooleanType) {
424
+ data_.boolean = boolean;
425
+ }
426
+ ShareableScalar() : Shareable(UndefinedType) {}
427
+ explicit ShareableScalar(std::nullptr_t) : Shareable(NullType) {}
428
+
429
+ jsi::Value toJSValue(jsi::Runtime &rt) override {
430
+ switch (valueType_) {
431
+ case Shareable::UndefinedType:
432
+ return jsi::Value();
433
+ case Shareable::NullType:
434
+ return jsi::Value(nullptr);
435
+ case Shareable::BooleanType:
436
+ return jsi::Value(data_.boolean);
437
+ case Shareable::NumberType:
438
+ return jsi::Value(data_.number);
439
+ default:
440
+ throw std::runtime_error(
441
+ "attempted to convert object that's not of a scalar type");
442
+ }
443
+ }
444
+
445
+ protected:
446
+ union Data {
447
+ bool boolean;
448
+ double number;
449
+ };
450
+
451
+ private:
452
+ Data data_;
453
+ };
454
+
455
+ } // namespace reanimated
@@ -53,6 +53,10 @@ using RequestRender =
53
53
  std::function<void(std::function<void(double)>, jsi::Runtime &rt)>;
54
54
  using TimeProviderFunction = std::function<double(void)>;
55
55
 
56
+ using ProgressLayoutAnimationFunction =
57
+ std::function<void(int, const jsi::Object &newProps)>;
58
+ using EndLayoutAnimationFunction = std::function<void(int, bool, bool)>;
59
+
56
60
  using RegisterSensorFunction =
57
61
  std::function<int(int, int, std::function<void(double[])>)>;
58
62
  using UnregisterSensorFunction = std::function<void(int)>;
@@ -76,6 +80,8 @@ struct PlatformDepMethodsHolder {
76
80
  ConfigurePropsFunction configurePropsFunction;
77
81
  #endif
78
82
  TimeProviderFunction getCurrentTime;
83
+ ProgressLayoutAnimationFunction progressLayoutAnimation;
84
+ EndLayoutAnimationFunction endLayoutAnimation;
79
85
  RegisterSensorFunction registerSensor;
80
86
  UnregisterSensorFunction unregisterSensor;
81
87
  SetGestureStateFunction setGestureStateFunction;
@@ -1,9 +1,9 @@
1
1
  #include "RuntimeDecorator.h"
2
+ #include <jsi/instrumentation.h>
2
3
  #include <chrono>
3
4
  #include <memory>
4
5
  #include <unordered_map>
5
- #include "LayoutAnimationsProxy.h"
6
- #include "MutableValue.h"
6
+ #include <utility>
7
7
  #include "ReanimatedHiddenHeaders.h"
8
8
 
9
9
  namespace reanimated {
@@ -31,6 +31,7 @@ void RuntimeDecorator::decorateRuntime(
31
31
  rt, "_LABEL", jsi::String::createFromAscii(rt, label));
32
32
 
33
33
  jsi::Object dummyGlobal(rt);
34
+ dummyGlobal.setProperty(rt, "gc", rt.global().getProperty(rt, "gc"));
34
35
  rt.global().setProperty(rt, "global", dummyGlobal);
35
36
 
36
37
  rt.global().setProperty(rt, "jsThis", jsi::Value::undefined());
@@ -105,11 +106,15 @@ void RuntimeDecorator::decorateUIRuntime(
105
106
  const ScrollToFunction scrollTo,
106
107
  #endif
107
108
  const RequestFrameFunction requestFrame,
109
+ const ScheduleOnJSFunction scheduleOnJS,
110
+ const MakeShareableCloneFunction makeShareableClone,
111
+ const UpdateDataSynchronouslyFunction updateDataSynchronously,
108
112
  const TimeProviderFunction getCurrentTime,
109
113
  const RegisterSensorFunction registerSensor,
110
114
  const UnregisterSensorFunction unregisterSensor,
111
115
  const SetGestureStateFunction setGestureState,
112
- std::shared_ptr<LayoutAnimationsProxy> layoutAnimationsProxy) {
116
+ const ProgressLayoutAnimationFunction progressLayoutAnimationFunction,
117
+ const EndLayoutAnimationFunction endLayoutAnimationFunction) {
113
118
  RuntimeDecorator::decorateRuntime(rt, "UI");
114
119
  rt.global().setProperty(rt, "_UI", jsi::Value(true));
115
120
 
@@ -232,6 +237,42 @@ void RuntimeDecorator::decorateUIRuntime(
232
237
  rt, jsi::PropNameID::forAscii(rt, "requestAnimationFrame"), 1, clb2);
233
238
  rt.global().setProperty(rt, "requestAnimationFrame", requestAnimationFrame);
234
239
 
240
+ auto clb4 = [scheduleOnJS](
241
+ jsi::Runtime &rt,
242
+ const jsi::Value &thisValue,
243
+ const jsi::Value *args,
244
+ const size_t count) -> jsi::Value {
245
+ scheduleOnJS(rt, args[0], args[1]);
246
+ return jsi::Value::undefined();
247
+ };
248
+ jsi::Value scheduleOnJSFun = jsi::Function::createFromHostFunction(
249
+ rt, jsi::PropNameID::forAscii(rt, "_scheduleOnJS"), 2, clb4);
250
+ rt.global().setProperty(rt, "_scheduleOnJS", scheduleOnJSFun);
251
+
252
+ auto clb5 = [makeShareableClone](
253
+ jsi::Runtime &rt,
254
+ const jsi::Value &thisValue,
255
+ const jsi::Value *args,
256
+ const size_t count) -> jsi::Value {
257
+ return makeShareableClone(rt, std::move(args[0]));
258
+ };
259
+ jsi::Value makeShareableCloneFun = jsi::Function::createFromHostFunction(
260
+ rt, jsi::PropNameID::forAscii(rt, "_makeShareableClone"), 1, clb5);
261
+ rt.global().setProperty(rt, "_makeShareableClone", makeShareableCloneFun);
262
+
263
+ auto clb51 = [updateDataSynchronously](
264
+ jsi::Runtime &rt,
265
+ const jsi::Value &thisValue,
266
+ const jsi::Value *args,
267
+ const size_t count) -> jsi::Value {
268
+ updateDataSynchronously(rt, std::move(args[0]), std::move(args[1]));
269
+ return jsi::Value::undefined();
270
+ };
271
+ jsi::Value updateDataSynchronouslyFun = jsi::Function::createFromHostFunction(
272
+ rt, jsi::PropNameID::forAscii(rt, "_updateDataSynchronously"), 1, clb51);
273
+ rt.global().setProperty(
274
+ rt, "_updateDataSynchronously", updateDataSynchronouslyFun);
275
+
235
276
  auto clb6 = [getCurrentTime](
236
277
  jsi::Runtime &rt,
237
278
  const jsi::Value &thisValue,
@@ -247,43 +288,30 @@ void RuntimeDecorator::decorateUIRuntime(
247
288
  rt.global().setProperty(rt, "_eventTimestamp", jsi::Value::undefined());
248
289
 
249
290
  // layout animation
250
- std::weak_ptr<LayoutAnimationsProxy> layoutProxy = layoutAnimationsProxy;
251
- auto clb7 = [layoutProxy](
291
+ auto clb7 = [progressLayoutAnimationFunction](
252
292
  jsi::Runtime &rt,
253
293
  const jsi::Value &thisValue,
254
294
  const jsi::Value *args,
255
295
  size_t count) -> jsi::Value {
256
- std::shared_ptr<LayoutAnimationsProxy> proxy = layoutProxy.lock();
257
- if (layoutProxy.expired()) {
258
- return jsi::Value::undefined();
259
- }
260
- proxy->startObserving(
261
- args[0].asNumber(),
262
- args[1].asObject(rt).getHostObject<MutableValue>(rt),
263
- rt);
296
+ progressLayoutAnimationFunction(args[0].asNumber(), args[1].asObject(rt));
264
297
  return jsi::Value::undefined();
265
298
  };
266
- jsi::Value _startObservingProgress = jsi::Function::createFromHostFunction(
267
- rt, jsi::PropNameID::forAscii(rt, "_startObservingProgress"), 0, clb7);
268
- rt.global().setProperty(
269
- rt, "_startObservingProgress", _startObservingProgress);
299
+ jsi::Value _notifyAboutProgress = jsi::Function::createFromHostFunction(
300
+ rt, jsi::PropNameID::forAscii(rt, "_notifyAboutProgress"), 2, clb7);
301
+ rt.global().setProperty(rt, "_notifyAboutProgress", _notifyAboutProgress);
270
302
 
271
- auto clb8 = [layoutProxy](
303
+ auto clb8 = [endLayoutAnimationFunction](
272
304
  jsi::Runtime &rt,
273
305
  const jsi::Value &thisValue,
274
306
  const jsi::Value *args,
275
307
  size_t count) -> jsi::Value {
276
- std::shared_ptr<LayoutAnimationsProxy> proxy = layoutProxy.lock();
277
- if (layoutProxy.expired()) {
278
- return jsi::Value::undefined();
279
- }
280
- proxy->stopObserving(
308
+ endLayoutAnimationFunction(
281
309
  args[0].asNumber(), args[1].getBool(), args[2].getBool());
282
310
  return jsi::Value::undefined();
283
311
  };
284
- jsi::Value _stopObservingProgress = jsi::Function::createFromHostFunction(
285
- rt, jsi::PropNameID::forAscii(rt, "_stopObservingProgress"), 0, clb8);
286
- rt.global().setProperty(rt, "_stopObservingProgress", _stopObservingProgress);
312
+ jsi::Value _notifyAboutEnd = jsi::Function::createFromHostFunction(
313
+ rt, jsi::PropNameID::forAscii(rt, "_notifyAboutEnd"), 2, clb8);
314
+ rt.global().setProperty(rt, "_notifyAboutEnd", _notifyAboutEnd);
287
315
 
288
316
  auto clb9 = [setGestureState](
289
317
  jsi::Runtime &rt,
@@ -5,7 +5,6 @@
5
5
  #include <memory>
6
6
  #include <string>
7
7
  #include <unordered_map>
8
- #include "LayoutAnimationsProxy.h"
9
8
  #include "PlatformDepMethodsHolder.h"
10
9
 
11
10
  using namespace facebook;
@@ -13,6 +12,12 @@ using namespace facebook;
13
12
  namespace reanimated {
14
13
 
15
14
  using RequestFrameFunction = std::function<void(std::function<void(double)>)>;
15
+ using ScheduleOnJSFunction =
16
+ std::function<void(jsi::Runtime &, const jsi::Value &, const jsi::Value &)>;
17
+ using MakeShareableCloneFunction =
18
+ std::function<jsi::Value(jsi::Runtime &, const jsi::Value &)>;
19
+ using UpdateDataSynchronouslyFunction =
20
+ std::function<void(jsi::Runtime &, const jsi::Value &, const jsi::Value &)>;
16
21
 
17
22
  enum RuntimeType {
18
23
  /**
@@ -40,11 +45,15 @@ class RuntimeDecorator {
40
45
  const ScrollToFunction scrollTo,
41
46
  #endif
42
47
  const RequestFrameFunction requestFrame,
48
+ const ScheduleOnJSFunction scheduleOnJS,
49
+ const MakeShareableCloneFunction makeShareableClone,
50
+ const UpdateDataSynchronouslyFunction updateDataSynchronously,
43
51
  const TimeProviderFunction getCurrentTime,
44
52
  const RegisterSensorFunction registerSensor,
45
53
  const UnregisterSensorFunction unregisterSensor,
46
54
  const SetGestureStateFunction setGestureState,
47
- std::shared_ptr<LayoutAnimationsProxy> layoutAnimationsProxy);
55
+ const ProgressLayoutAnimationFunction progressLayoutAnimationFunction,
56
+ const EndLayoutAnimationFunction endLayoutAnimationFunction);
48
57
 
49
58
  /**
50
59
  Returns true if the given Runtime is the Reanimated UI-Thread Runtime.
@@ -3,6 +3,8 @@
3
3
  #else
4
4
  #include "Scheduler.h"
5
5
  #endif
6
+ #include "ReanimatedRuntime.h"
7
+ #include "RuntimeManager.h"
6
8
 
7
9
  namespace reanimated {
8
10
 
@@ -16,6 +18,14 @@ void Scheduler::scheduleOnJS(std::function<void()> job) {
16
18
 
17
19
  void Scheduler::triggerUI() {
18
20
  scheduledOnUI = false;
21
+ #if JS_RUNTIME_HERMES
22
+ // JSI's scope defined here allows for JSI-objects to be cleared up after
23
+ // each runtime loop. Within these loops we typically create some temporary
24
+ // JSI objects and hence it allows for such objects to be garbage collected
25
+ // much sooner.
26
+ // Apparently the scope API is only supported on Hermes at the moment.
27
+ auto scope = jsi::Scope(*runtimeManager.lock()->runtime);
28
+ #endif
19
29
  while (uiJobs.getSize()) {
20
30
  auto job = uiJobs.pop();
21
31
  job();
@@ -29,7 +29,7 @@ class SingleInstanceChecker {
29
29
 
30
30
  // A static field will exist separately for every class template.
31
31
  // This has to be inline for automatic initialization.
32
- inline static int instanceCount_;
32
+ inline static volatile int instanceCount_;
33
33
  };
34
34
 
35
35
  template <class T>