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