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