1 #pragma once
2 
3 #ifdef ABI49_0_0RCT_NEW_ARCH_ENABLED
4 #include <react/renderer/uimanager/UIManager.h>
5 #include "NewestShadowNodesRegistry.h"
6 #endif
7 
8 #include <memory>
9 #include <string>
10 #include <unordered_set>
11 #include <utility>
12 #include <vector>
13 
14 #include "AnimatedSensorModule.h"
15 #include "ErrorHandler.h"
16 #include "LayoutAnimationsManager.h"
17 #include "NativeReanimatedModuleSpec.h"
18 #include "PlatformDepMethodsHolder.h"
19 #include "RuntimeDecorator.h"
20 #include "RuntimeManager.h"
21 #include "Scheduler.h"
22 #include "SingleInstanceChecker.h"
23 
24 namespace ABI49_0_0reanimated {
25 
26 using FrameCallback = std::function<void(double)>;
27 
28 class EventHandlerRegistry;
29 
30 class NativeReanimatedModule : public NativeReanimatedModuleSpec,
31                                public RuntimeManager {
32  public:
33   NativeReanimatedModule(
34       const std::shared_ptr<CallInvoker> &jsInvoker,
35       const std::shared_ptr<Scheduler> &scheduler,
36       const std::shared_ptr<jsi::Runtime> &rt,
37       const std::shared_ptr<ErrorHandler> &errorHandler,
38 #ifdef ABI49_0_0RCT_NEW_ARCH_ENABLED
39   // nothing
40 #else
41       std::function<jsi::Value(jsi::Runtime &, const int, const jsi::String &)>
42           propObtainer,
43 #endif
44       PlatformDepMethodsHolder platformDepMethodsHolder);
45 
46   ~NativeReanimatedModule();
47 
48   std::shared_ptr<JSRuntimeHelper> runtimeHelper;
49 
50   void installCoreFunctions(
51       jsi::Runtime &rt,
52       const jsi::Value &callGuard,
53       const jsi::Value &valueUnpacker) override;
54 
55   jsi::Value makeShareableClone(
56       jsi::Runtime &rt,
57       const jsi::Value &value,
58       const jsi::Value &shouldRetainRemote) override;
59 
60   jsi::Value makeSynchronizedDataHolder(
61       jsi::Runtime &rt,
62       const jsi::Value &initialShareable) override;
63   jsi::Value getDataSynchronously(
64       jsi::Runtime &rt,
65       const jsi::Value &synchronizedDataHolderRef) override;
66   void updateDataSynchronously(
67       jsi::Runtime &rt,
68       const jsi::Value &synchronizedDataHolderRef,
69       const jsi::Value &newData);
70 
71   void scheduleOnUI(jsi::Runtime &rt, const jsi::Value &worklet) override;
72 
73   jsi::Value registerEventHandler(
74       jsi::Runtime &rt,
75       const jsi::Value &eventHash,
76       const jsi::Value &worklet) override;
77   void unregisterEventHandler(
78       jsi::Runtime &rt,
79       const jsi::Value &registrationId) override;
80 
81   jsi::Value getViewProp(
82       jsi::Runtime &rt,
83       const jsi::Value &viewTag,
84       const jsi::Value &propName,
85       const jsi::Value &callback) override;
86 
87   jsi::Value enableLayoutAnimations(jsi::Runtime &rt, const jsi::Value &config)
88       override;
89   jsi::Value configureProps(
90       jsi::Runtime &rt,
91       const jsi::Value &uiProps,
92       const jsi::Value &nativeProps) override;
93   jsi::Value configureLayoutAnimation(
94       jsi::Runtime &rt,
95       const jsi::Value &viewTag,
96       const jsi::Value &type,
97       const jsi::Value &sharedTransitionTag,
98       const jsi::Value &config) override;
99 
100   void onRender(double timestampMs);
101 
102   void onEvent(
103       double eventTimestamp,
104       const std::string &eventName,
105       const jsi::Value &payload);
106 
107   bool isAnyHandlerWaitingForEvent(std::string eventName);
108 
109   void maybeRequestRender();
110   UpdatePropsFunction updatePropsFunction;
111 
112   bool handleEvent(
113       const std::string &eventName,
114       const jsi::Value &payload,
115       double currentTime);
116 
117 #ifdef ABI49_0_0RCT_NEW_ARCH_ENABLED
118   bool handleRawEvent(const RawEvent &rawEvent, double currentTime);
119 
120   void updateProps(
121       jsi::Runtime &rt,
122       const jsi::Value &shadowNodeValue,
123       const jsi::Value &props);
124 
125   void removeShadowNodeFromRegistry(jsi::Runtime &rt, const jsi::Value &tag);
126 
127   void performOperations();
128 
129   void dispatchCommand(
130       jsi::Runtime &rt,
131       const jsi::Value &shadowNodeValue,
132       const jsi::Value &commandNameValue,
133       const jsi::Value &argsValue);
134 
135   jsi::Value measure(jsi::Runtime &rt, const jsi::Value &shadowNodeValue);
136 
137   void setUIManager(std::shared_ptr<UIManager> uiManager);
138 
139   void setNewestShadowNodesRegistry(
140       std::shared_ptr<NewestShadowNodesRegistry> newestShadowNodesRegistry);
141 #endif
142 
143   jsi::Value registerSensor(
144       jsi::Runtime &rt,
145       const jsi::Value &sensorType,
146       const jsi::Value &interval,
147       const jsi::Value &iosReferenceFrame,
148       const jsi::Value &sensorDataContainer) override;
149   void unregisterSensor(jsi::Runtime &rt, const jsi::Value &sensorId) override;
150 
151   void cleanupSensors();
152 
153   jsi::Value subscribeForKeyboardEvents(
154       jsi::Runtime &rt,
155       const jsi::Value &keyboardEventContainer,
156       const jsi::Value &isStatusBarTranslucent) override;
157   void unsubscribeFromKeyboardEvents(
158       jsi::Runtime &rt,
159       const jsi::Value &listenerId) override;
160 
layoutAnimationsManager()161   inline LayoutAnimationsManager &layoutAnimationsManager() {
162     return layoutAnimationsManager_;
163   }
164 
165  private:
166 #ifdef ABI49_0_0RCT_NEW_ARCH_ENABLED
167   bool isThereAnyLayoutProp(jsi::Runtime &rt, const jsi::Value &props);
168 #endif // ABI49_0_0RCT_NEW_ARCH_ENABLED
169 
170   std::unique_ptr<EventHandlerRegistry> eventHandlerRegistry;
171   std::function<void(FrameCallback &, jsi::Runtime &)> requestRender;
172   std::vector<FrameCallback> frameCallbacks;
173   bool renderRequested = false;
174   std::function<jsi::Value(jsi::Runtime &, const int, const jsi::String &)>
175       propObtainer;
176   std::function<void(double)> onRenderCallback;
177   AnimatedSensorModule animatedSensorModule;
178   ConfigurePropsFunction configurePropsPlatformFunction;
179 
180 #ifdef ABI49_0_0RCT_NEW_ARCH_ENABLED
181   SynchronouslyUpdateUIPropsFunction synchronouslyUpdateUIPropsFunction;
182 
183   std::shared_ptr<UIManager> uiManager_;
184 
185   // After app reload, surfaceId on iOS is still 1 but on Android it's 11.
186   // We can store surfaceId of the most recent ShadowNode as a workaround.
187   SurfaceId surfaceId_ = -1;
188 
189   std::vector<std::pair<ShadowNode::Shared, std::unique_ptr<jsi::Value>>>
190       operationsInBatch_; // TODO: refactor std::pair to custom struct
191 
192   std::shared_ptr<NewestShadowNodesRegistry> newestShadowNodesRegistry_;
193 
194   std::vector<Tag> tagsToRemove_; // from newestShadowNodesRegistry_
195 #endif
196 
197   std::unordered_set<std::string> nativePropNames_; // filled by configureProps
198   LayoutAnimationsManager layoutAnimationsManager_;
199 
200   KeyboardEventSubscribeFunction subscribeForKeyboardEventsFunction;
201   KeyboardEventUnsubscribeFunction unsubscribeFromKeyboardEventsFunction;
202 
203 #ifdef DEBUG
204   SingleInstanceChecker<NativeReanimatedModule> singleInstanceChecker_;
205 #endif
206 };
207 
208 } // namespace reanimated
209