1*fe5cfb17STomasz Sapeta #pragma once
2*fe5cfb17STomasz Sapeta 
3*fe5cfb17STomasz Sapeta #include <unistd.h>
4*fe5cfb17STomasz Sapeta #include <memory>
5*fe5cfb17STomasz Sapeta #include <string>
6*fe5cfb17STomasz Sapeta #include <vector>
7*fe5cfb17STomasz Sapeta 
8*fe5cfb17STomasz Sapeta #include "AnimatedSensorModule.h"
9*fe5cfb17STomasz Sapeta #include "ErrorHandler.h"
10*fe5cfb17STomasz Sapeta #include "LayoutAnimationsProxy.h"
11*fe5cfb17STomasz Sapeta #include "NativeReanimatedModuleSpec.h"
12*fe5cfb17STomasz Sapeta #include "PlatformDepMethodsHolder.h"
13*fe5cfb17STomasz Sapeta #include "RuntimeDecorator.h"
14*fe5cfb17STomasz Sapeta #include "RuntimeManager.h"
15*fe5cfb17STomasz Sapeta #include "Scheduler.h"
16*fe5cfb17STomasz Sapeta #include "SingleInstanceChecker.h"
17*fe5cfb17STomasz Sapeta 
18*fe5cfb17STomasz Sapeta namespace ABI48_0_0reanimated {
19*fe5cfb17STomasz Sapeta 
20*fe5cfb17STomasz Sapeta using FrameCallback = std::function<void(double)>;
21*fe5cfb17STomasz Sapeta 
22*fe5cfb17STomasz Sapeta class ShareableValue;
23*fe5cfb17STomasz Sapeta class MutableValue;
24*fe5cfb17STomasz Sapeta class MapperRegistry;
25*fe5cfb17STomasz Sapeta class EventHandlerRegistry;
26*fe5cfb17STomasz Sapeta 
27*fe5cfb17STomasz Sapeta class NativeReanimatedModule : public NativeReanimatedModuleSpec,
28*fe5cfb17STomasz Sapeta                                public RuntimeManager {
29*fe5cfb17STomasz Sapeta   friend ShareableValue;
30*fe5cfb17STomasz Sapeta   friend MutableValue;
31*fe5cfb17STomasz Sapeta 
32*fe5cfb17STomasz Sapeta  public:
33*fe5cfb17STomasz Sapeta   NativeReanimatedModule(
34*fe5cfb17STomasz Sapeta       std::shared_ptr<CallInvoker> jsInvoker,
35*fe5cfb17STomasz Sapeta       std::shared_ptr<Scheduler> scheduler,
36*fe5cfb17STomasz Sapeta       std::shared_ptr<jsi::Runtime> rt,
37*fe5cfb17STomasz Sapeta       std::shared_ptr<ErrorHandler> errorHandler,
38*fe5cfb17STomasz Sapeta       std::function<jsi::Value(jsi::Runtime &, const int, const jsi::String &)>
39*fe5cfb17STomasz Sapeta           propObtainer,
40*fe5cfb17STomasz Sapeta       std::shared_ptr<LayoutAnimationsProxy> layoutAnimationsProxy,
41*fe5cfb17STomasz Sapeta       PlatformDepMethodsHolder platformDepMethodsHolder);
42*fe5cfb17STomasz Sapeta 
43*fe5cfb17STomasz Sapeta   void installCoreFunctions(jsi::Runtime &rt, const jsi::Value &valueSetter)
44*fe5cfb17STomasz Sapeta       override;
45*fe5cfb17STomasz Sapeta 
46*fe5cfb17STomasz Sapeta   jsi::Value makeShareable(jsi::Runtime &rt, const jsi::Value &value) override;
47*fe5cfb17STomasz Sapeta   jsi::Value makeMutable(jsi::Runtime &rt, const jsi::Value &value) override;
48*fe5cfb17STomasz Sapeta   jsi::Value makeRemote(jsi::Runtime &rt, const jsi::Value &value) override;
49*fe5cfb17STomasz Sapeta 
50*fe5cfb17STomasz Sapeta   jsi::Value startMapper(
51*fe5cfb17STomasz Sapeta       jsi::Runtime &rt,
52*fe5cfb17STomasz Sapeta       const jsi::Value &worklet,
53*fe5cfb17STomasz Sapeta       const jsi::Value &inputs,
54*fe5cfb17STomasz Sapeta       const jsi::Value &outputs,
55*fe5cfb17STomasz Sapeta       const jsi::Value &updater,
56*fe5cfb17STomasz Sapeta       const jsi::Value &viewDescriptors) override;
57*fe5cfb17STomasz Sapeta   void stopMapper(jsi::Runtime &rt, const jsi::Value &mapperId) override;
58*fe5cfb17STomasz Sapeta 
59*fe5cfb17STomasz Sapeta   jsi::Value registerEventHandler(
60*fe5cfb17STomasz Sapeta       jsi::Runtime &rt,
61*fe5cfb17STomasz Sapeta       const jsi::Value &eventHash,
62*fe5cfb17STomasz Sapeta       const jsi::Value &worklet) override;
63*fe5cfb17STomasz Sapeta   void unregisterEventHandler(
64*fe5cfb17STomasz Sapeta       jsi::Runtime &rt,
65*fe5cfb17STomasz Sapeta       const jsi::Value &registrationId) override;
66*fe5cfb17STomasz Sapeta 
67*fe5cfb17STomasz Sapeta   jsi::Value getViewProp(
68*fe5cfb17STomasz Sapeta       jsi::Runtime &rt,
69*fe5cfb17STomasz Sapeta       const jsi::Value &viewTag,
70*fe5cfb17STomasz Sapeta       const jsi::Value &propName,
71*fe5cfb17STomasz Sapeta       const jsi::Value &callback) override;
72*fe5cfb17STomasz Sapeta 
73*fe5cfb17STomasz Sapeta   jsi::Value enableLayoutAnimations(jsi::Runtime &rt, const jsi::Value &config)
74*fe5cfb17STomasz Sapeta       override;
75*fe5cfb17STomasz Sapeta   jsi::Value configureProps(
76*fe5cfb17STomasz Sapeta       jsi::Runtime &rt,
77*fe5cfb17STomasz Sapeta       const jsi::Value &uiProps,
78*fe5cfb17STomasz Sapeta       const jsi::Value &nativeProps) override;
79*fe5cfb17STomasz Sapeta 
80*fe5cfb17STomasz Sapeta   void onRender(double timestampMs);
81*fe5cfb17STomasz Sapeta   void onEvent(std::string eventName, std::string eventAsString);
82*fe5cfb17STomasz Sapeta   bool isAnyHandlerWaitingForEvent(std::string eventName);
83*fe5cfb17STomasz Sapeta 
84*fe5cfb17STomasz Sapeta   void maybeRequestRender();
85*fe5cfb17STomasz Sapeta   UpdaterFunction updaterFunction;
86*fe5cfb17STomasz Sapeta 
87*fe5cfb17STomasz Sapeta   jsi::Value registerSensor(
88*fe5cfb17STomasz Sapeta       jsi::Runtime &rt,
89*fe5cfb17STomasz Sapeta       const jsi::Value &sensorType,
90*fe5cfb17STomasz Sapeta       const jsi::Value &interval,
91*fe5cfb17STomasz Sapeta       const jsi::Value &sensorDataContainer) override;
92*fe5cfb17STomasz Sapeta   void unregisterSensor(jsi::Runtime &rt, const jsi::Value &sensorId) override;
93*fe5cfb17STomasz Sapeta   jsi::Value subscribeForKeyboardEvents(
94*fe5cfb17STomasz Sapeta       jsi::Runtime &rt,
95*fe5cfb17STomasz Sapeta       const jsi::Value &keyboardEventContainer) override;
96*fe5cfb17STomasz Sapeta   void unsubscribeFromKeyboardEvents(
97*fe5cfb17STomasz Sapeta       jsi::Runtime &rt,
98*fe5cfb17STomasz Sapeta       const jsi::Value &listenerId) override;
99*fe5cfb17STomasz Sapeta 
100*fe5cfb17STomasz Sapeta  private:
101*fe5cfb17STomasz Sapeta   std::shared_ptr<MapperRegistry> mapperRegistry;
102*fe5cfb17STomasz Sapeta   std::shared_ptr<EventHandlerRegistry> eventHandlerRegistry;
103*fe5cfb17STomasz Sapeta   std::function<void(FrameCallback &, jsi::Runtime &)> requestRender;
104*fe5cfb17STomasz Sapeta   std::shared_ptr<jsi::Value> dummyEvent;
105*fe5cfb17STomasz Sapeta   std::vector<FrameCallback> frameCallbacks;
106*fe5cfb17STomasz Sapeta   bool renderRequested = false;
107*fe5cfb17STomasz Sapeta   std::function<jsi::Value(jsi::Runtime &, const int, const jsi::String &)>
108*fe5cfb17STomasz Sapeta       propObtainer;
109*fe5cfb17STomasz Sapeta   std::function<void(double)> onRenderCallback;
110*fe5cfb17STomasz Sapeta   std::shared_ptr<LayoutAnimationsProxy> layoutAnimationsProxy;
111*fe5cfb17STomasz Sapeta   AnimatedSensorModule animatedSensorModule;
112*fe5cfb17STomasz Sapeta   ConfigurePropsFunction configurePropsPlatformFunction;
113*fe5cfb17STomasz Sapeta   KeyboardEventSubscribeFunction subscribeForKeyboardEventsFunction;
114*fe5cfb17STomasz Sapeta   KeyboardEventUnsubscribeFunction unsubscribeFromKeyboardEventsFunction;
115*fe5cfb17STomasz Sapeta 
116*fe5cfb17STomasz Sapeta #ifdef DEBUG
117*fe5cfb17STomasz Sapeta   SingleInstanceChecker<NativeReanimatedModule> singleInstanceChecker_;
118*fe5cfb17STomasz Sapeta #endif
119*fe5cfb17STomasz Sapeta };
120*fe5cfb17STomasz Sapeta 
121*fe5cfb17STomasz Sapeta } // namespace reanimated
122