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