1*023bc8eaSKudo Chien #include "NativeReanimatedModule.h"
2*023bc8eaSKudo Chien #include <functional>
3*023bc8eaSKudo Chien #include <memory>
4*023bc8eaSKudo Chien #include <thread>
5*023bc8eaSKudo Chien #include <unordered_map>
6*023bc8eaSKudo Chien 
7*023bc8eaSKudo Chien #include "EventHandlerRegistry.h"
8*023bc8eaSKudo Chien #include "FeaturesConfig.h"
9*023bc8eaSKudo Chien #include "FrozenObject.h"
10*023bc8eaSKudo Chien #include "JSIStoreValueUser.h"
11*023bc8eaSKudo Chien #include "Mapper.h"
12*023bc8eaSKudo Chien #include "MapperRegistry.h"
13*023bc8eaSKudo Chien #include "MutableValue.h"
14*023bc8eaSKudo Chien #include "ReanimatedHiddenHeaders.h"
15*023bc8eaSKudo Chien #include "RuntimeDecorator.h"
16*023bc8eaSKudo Chien #include "ShareableValue.h"
17*023bc8eaSKudo Chien #include "WorkletEventHandler.h"
18*023bc8eaSKudo Chien 
19*023bc8eaSKudo Chien using namespace facebook;
20*023bc8eaSKudo Chien 
21*023bc8eaSKudo Chien namespace reanimated {
22*023bc8eaSKudo Chien 
extractMutables(jsi::Runtime & rt,std::shared_ptr<ShareableValue> sv,std::vector<std::shared_ptr<MutableValue>> & res)23*023bc8eaSKudo Chien void extractMutables(
24*023bc8eaSKudo Chien     jsi::Runtime &rt,
25*023bc8eaSKudo Chien     std::shared_ptr<ShareableValue> sv,
26*023bc8eaSKudo Chien     std::vector<std::shared_ptr<MutableValue>> &res) {
27*023bc8eaSKudo Chien   switch (sv->type) {
28*023bc8eaSKudo Chien     case ValueType::MutableValueType: {
29*023bc8eaSKudo Chien       auto &mutableValue = ValueWrapper::asMutableValue(sv->valueContainer);
30*023bc8eaSKudo Chien       res.push_back(mutableValue);
31*023bc8eaSKudo Chien       break;
32*023bc8eaSKudo Chien     }
33*023bc8eaSKudo Chien     case ValueType::FrozenArrayType:
34*023bc8eaSKudo Chien       for (auto &it : ValueWrapper::asFrozenArray(sv->valueContainer)) {
35*023bc8eaSKudo Chien         extractMutables(rt, it, res);
36*023bc8eaSKudo Chien       }
37*023bc8eaSKudo Chien       break;
38*023bc8eaSKudo Chien     case ValueType::RemoteObjectType:
39*023bc8eaSKudo Chien     case ValueType::FrozenObjectType:
40*023bc8eaSKudo Chien       for (auto &it : ValueWrapper::asFrozenObject(sv->valueContainer)->map) {
41*023bc8eaSKudo Chien         extractMutables(rt, it.second, res);
42*023bc8eaSKudo Chien       }
43*023bc8eaSKudo Chien       break;
44*023bc8eaSKudo Chien     default:
45*023bc8eaSKudo Chien       break;
46*023bc8eaSKudo Chien   }
47*023bc8eaSKudo Chien }
48*023bc8eaSKudo Chien 
extractMutablesFromArray(jsi::Runtime & rt,const jsi::Array & array,NativeReanimatedModule * module)49*023bc8eaSKudo Chien std::vector<std::shared_ptr<MutableValue>> extractMutablesFromArray(
50*023bc8eaSKudo Chien     jsi::Runtime &rt,
51*023bc8eaSKudo Chien     const jsi::Array &array,
52*023bc8eaSKudo Chien     NativeReanimatedModule *module) {
53*023bc8eaSKudo Chien   std::vector<std::shared_ptr<MutableValue>> res;
54*023bc8eaSKudo Chien   for (size_t i = 0, size = array.size(rt); i < size; i++) {
55*023bc8eaSKudo Chien     auto shareable =
56*023bc8eaSKudo Chien         ShareableValue::adapt(rt, array.getValueAtIndex(rt, i), module);
57*023bc8eaSKudo Chien     extractMutables(rt, shareable, res);
58*023bc8eaSKudo Chien   }
59*023bc8eaSKudo Chien   return res;
60*023bc8eaSKudo Chien }
61*023bc8eaSKudo Chien 
NativeReanimatedModule(std::shared_ptr<CallInvoker> jsInvoker,std::shared_ptr<Scheduler> scheduler,std::shared_ptr<jsi::Runtime> rt,std::shared_ptr<ErrorHandler> errorHandler,std::function<jsi::Value (jsi::Runtime &,const int,const jsi::String &)> propObtainer,std::shared_ptr<LayoutAnimationsProxy> layoutAnimationsProxy,PlatformDepMethodsHolder platformDepMethodsHolder)62*023bc8eaSKudo Chien NativeReanimatedModule::NativeReanimatedModule(
63*023bc8eaSKudo Chien     std::shared_ptr<CallInvoker> jsInvoker,
64*023bc8eaSKudo Chien     std::shared_ptr<Scheduler> scheduler,
65*023bc8eaSKudo Chien     std::shared_ptr<jsi::Runtime> rt,
66*023bc8eaSKudo Chien     std::shared_ptr<ErrorHandler> errorHandler,
67*023bc8eaSKudo Chien     std::function<jsi::Value(jsi::Runtime &, const int, const jsi::String &)>
68*023bc8eaSKudo Chien         propObtainer,
69*023bc8eaSKudo Chien     std::shared_ptr<LayoutAnimationsProxy> layoutAnimationsProxy,
70*023bc8eaSKudo Chien     PlatformDepMethodsHolder platformDepMethodsHolder)
71*023bc8eaSKudo Chien     : NativeReanimatedModuleSpec(jsInvoker),
72*023bc8eaSKudo Chien       RuntimeManager(rt, errorHandler, scheduler, RuntimeType::UI),
73*023bc8eaSKudo Chien       mapperRegistry(std::make_shared<MapperRegistry>()),
74*023bc8eaSKudo Chien       eventHandlerRegistry(std::make_shared<EventHandlerRegistry>()),
75*023bc8eaSKudo Chien       requestRender(platformDepMethodsHolder.requestRender),
76*023bc8eaSKudo Chien       propObtainer(propObtainer),
77*023bc8eaSKudo Chien       animatedSensorModule(platformDepMethodsHolder, this),
78*023bc8eaSKudo Chien       configurePropsPlatformFunction(
79*023bc8eaSKudo Chien           platformDepMethodsHolder.configurePropsFunction) {
80*023bc8eaSKudo Chien   auto requestAnimationFrame = [=](FrameCallback callback) {
81*023bc8eaSKudo Chien     frameCallbacks.push_back(callback);
82*023bc8eaSKudo Chien     maybeRequestRender();
83*023bc8eaSKudo Chien   };
84*023bc8eaSKudo Chien 
85*023bc8eaSKudo Chien   this->layoutAnimationsProxy = layoutAnimationsProxy;
86*023bc8eaSKudo Chien 
87*023bc8eaSKudo Chien   RuntimeDecorator::decorateUIRuntime(
88*023bc8eaSKudo Chien       *runtime,
89*023bc8eaSKudo Chien       platformDepMethodsHolder.updaterFunction,
90*023bc8eaSKudo Chien       requestAnimationFrame,
91*023bc8eaSKudo Chien       platformDepMethodsHolder.scrollToFunction,
92*023bc8eaSKudo Chien       platformDepMethodsHolder.measuringFunction,
93*023bc8eaSKudo Chien       platformDepMethodsHolder.getCurrentTime,
94*023bc8eaSKudo Chien       platformDepMethodsHolder.registerSensor,
95*023bc8eaSKudo Chien       platformDepMethodsHolder.unregisterSensor,
96*023bc8eaSKudo Chien       platformDepMethodsHolder.setGestureStateFunction,
97*023bc8eaSKudo Chien       layoutAnimationsProxy);
98*023bc8eaSKudo Chien   onRenderCallback = [this](double timestampMs) {
99*023bc8eaSKudo Chien     this->renderRequested = false;
100*023bc8eaSKudo Chien     this->onRender(timestampMs);
101*023bc8eaSKudo Chien   };
102*023bc8eaSKudo Chien   updaterFunction = platformDepMethodsHolder.updaterFunction;
103*023bc8eaSKudo Chien   subscribeForKeyboardEventsFunction =
104*023bc8eaSKudo Chien       platformDepMethodsHolder.subscribeForKeyboardEvents;
105*023bc8eaSKudo Chien   unsubscribeFromKeyboardEventsFunction =
106*023bc8eaSKudo Chien       platformDepMethodsHolder.unsubscribeFromKeyboardEvents;
107*023bc8eaSKudo Chien }
108*023bc8eaSKudo Chien 
installCoreFunctions(jsi::Runtime & rt,const jsi::Value & valueSetter)109*023bc8eaSKudo Chien void NativeReanimatedModule::installCoreFunctions(
110*023bc8eaSKudo Chien     jsi::Runtime &rt,
111*023bc8eaSKudo Chien     const jsi::Value &valueSetter) {
112*023bc8eaSKudo Chien   this->valueSetter = ShareableValue::adapt(rt, valueSetter, this);
113*023bc8eaSKudo Chien }
114*023bc8eaSKudo Chien 
makeShareable(jsi::Runtime & rt,const jsi::Value & value)115*023bc8eaSKudo Chien jsi::Value NativeReanimatedModule::makeShareable(
116*023bc8eaSKudo Chien     jsi::Runtime &rt,
117*023bc8eaSKudo Chien     const jsi::Value &value) {
118*023bc8eaSKudo Chien   return ShareableValue::adapt(rt, value, this)->getValue(rt);
119*023bc8eaSKudo Chien }
120*023bc8eaSKudo Chien 
makeMutable(jsi::Runtime & rt,const jsi::Value & value)121*023bc8eaSKudo Chien jsi::Value NativeReanimatedModule::makeMutable(
122*023bc8eaSKudo Chien     jsi::Runtime &rt,
123*023bc8eaSKudo Chien     const jsi::Value &value) {
124*023bc8eaSKudo Chien   return ShareableValue::adapt(rt, value, this, ValueType::MutableValueType)
125*023bc8eaSKudo Chien       ->getValue(rt);
126*023bc8eaSKudo Chien }
127*023bc8eaSKudo Chien 
makeRemote(jsi::Runtime & rt,const jsi::Value & value)128*023bc8eaSKudo Chien jsi::Value NativeReanimatedModule::makeRemote(
129*023bc8eaSKudo Chien     jsi::Runtime &rt,
130*023bc8eaSKudo Chien     const jsi::Value &value) {
131*023bc8eaSKudo Chien   return ShareableValue::adapt(rt, value, this, ValueType::RemoteObjectType)
132*023bc8eaSKudo Chien       ->getValue(rt);
133*023bc8eaSKudo Chien }
134*023bc8eaSKudo Chien 
startMapper(jsi::Runtime & rt,const jsi::Value & worklet,const jsi::Value & inputs,const jsi::Value & outputs,const jsi::Value & updater,const jsi::Value & viewDescriptors)135*023bc8eaSKudo Chien jsi::Value NativeReanimatedModule::startMapper(
136*023bc8eaSKudo Chien     jsi::Runtime &rt,
137*023bc8eaSKudo Chien     const jsi::Value &worklet,
138*023bc8eaSKudo Chien     const jsi::Value &inputs,
139*023bc8eaSKudo Chien     const jsi::Value &outputs,
140*023bc8eaSKudo Chien     const jsi::Value &updater,
141*023bc8eaSKudo Chien     const jsi::Value &viewDescriptors) {
142*023bc8eaSKudo Chien   static unsigned long MAPPER_ID = 1;
143*023bc8eaSKudo Chien 
144*023bc8eaSKudo Chien   unsigned long newMapperId = MAPPER_ID++;
145*023bc8eaSKudo Chien   auto mapperShareable = ShareableValue::adapt(rt, worklet, this);
146*023bc8eaSKudo Chien   auto inputMutables =
147*023bc8eaSKudo Chien       extractMutablesFromArray(rt, inputs.asObject(rt).asArray(rt), this);
148*023bc8eaSKudo Chien   auto outputMutables =
149*023bc8eaSKudo Chien       extractMutablesFromArray(rt, outputs.asObject(rt).asArray(rt), this);
150*023bc8eaSKudo Chien 
151*023bc8eaSKudo Chien   int optimalizationLvl = 0;
152*023bc8eaSKudo Chien   auto optimalization =
153*023bc8eaSKudo Chien       updater.asObject(rt).getProperty(rt, "__optimalization");
154*023bc8eaSKudo Chien   if (optimalization.isNumber()) {
155*023bc8eaSKudo Chien     optimalizationLvl = optimalization.asNumber();
156*023bc8eaSKudo Chien   }
157*023bc8eaSKudo Chien   auto updaterSV = ShareableValue::adapt(rt, updater, this);
158*023bc8eaSKudo Chien   auto viewDescriptorsSV = ShareableValue::adapt(rt, viewDescriptors, this);
159*023bc8eaSKudo Chien 
160*023bc8eaSKudo Chien   scheduler->scheduleOnUI([=] {
161*023bc8eaSKudo Chien     auto mapperFunction =
162*023bc8eaSKudo Chien         mapperShareable->getValue(*runtime).asObject(*runtime).asFunction(
163*023bc8eaSKudo Chien             *runtime);
164*023bc8eaSKudo Chien     std::shared_ptr<jsi::Function> mapperFunctionPointer =
165*023bc8eaSKudo Chien         std::make_shared<jsi::Function>(std::move(mapperFunction));
166*023bc8eaSKudo Chien 
167*023bc8eaSKudo Chien     std::shared_ptr<Mapper> mapperPointer = std::make_shared<Mapper>(
168*023bc8eaSKudo Chien         this,
169*023bc8eaSKudo Chien         newMapperId,
170*023bc8eaSKudo Chien         mapperFunctionPointer,
171*023bc8eaSKudo Chien         inputMutables,
172*023bc8eaSKudo Chien         outputMutables);
173*023bc8eaSKudo Chien     if (optimalizationLvl > 0) {
174*023bc8eaSKudo Chien       mapperPointer->enableFastMode(
175*023bc8eaSKudo Chien           optimalizationLvl, updaterSV, viewDescriptorsSV);
176*023bc8eaSKudo Chien     }
177*023bc8eaSKudo Chien     mapperRegistry->startMapper(mapperPointer);
178*023bc8eaSKudo Chien     maybeRequestRender();
179*023bc8eaSKudo Chien   });
180*023bc8eaSKudo Chien 
181*023bc8eaSKudo Chien   return jsi::Value(static_cast<double>(newMapperId));
182*023bc8eaSKudo Chien }
183*023bc8eaSKudo Chien 
stopMapper(jsi::Runtime & rt,const jsi::Value & mapperId)184*023bc8eaSKudo Chien void NativeReanimatedModule::stopMapper(
185*023bc8eaSKudo Chien     jsi::Runtime &rt,
186*023bc8eaSKudo Chien     const jsi::Value &mapperId) {
187*023bc8eaSKudo Chien   unsigned long id = mapperId.asNumber();
188*023bc8eaSKudo Chien   scheduler->scheduleOnUI([=] {
189*023bc8eaSKudo Chien     mapperRegistry->stopMapper(id);
190*023bc8eaSKudo Chien     maybeRequestRender();
191*023bc8eaSKudo Chien   });
192*023bc8eaSKudo Chien }
193*023bc8eaSKudo Chien 
registerEventHandler(jsi::Runtime & rt,const jsi::Value & eventHash,const jsi::Value & worklet)194*023bc8eaSKudo Chien jsi::Value NativeReanimatedModule::registerEventHandler(
195*023bc8eaSKudo Chien     jsi::Runtime &rt,
196*023bc8eaSKudo Chien     const jsi::Value &eventHash,
197*023bc8eaSKudo Chien     const jsi::Value &worklet) {
198*023bc8eaSKudo Chien   static unsigned long EVENT_HANDLER_ID = 1;
199*023bc8eaSKudo Chien 
200*023bc8eaSKudo Chien   unsigned long newRegistrationId = EVENT_HANDLER_ID++;
201*023bc8eaSKudo Chien   auto eventName = eventHash.asString(rt).utf8(rt);
202*023bc8eaSKudo Chien   auto handlerShareable = ShareableValue::adapt(rt, worklet, this);
203*023bc8eaSKudo Chien 
204*023bc8eaSKudo Chien   scheduler->scheduleOnUI([=] {
205*023bc8eaSKudo Chien     auto handlerFunction =
206*023bc8eaSKudo Chien         handlerShareable->getValue(*runtime).asObject(*runtime).asFunction(
207*023bc8eaSKudo Chien             *runtime);
208*023bc8eaSKudo Chien     auto handler = std::make_shared<WorkletEventHandler>(
209*023bc8eaSKudo Chien         newRegistrationId, eventName, std::move(handlerFunction));
210*023bc8eaSKudo Chien     eventHandlerRegistry->registerEventHandler(handler);
211*023bc8eaSKudo Chien   });
212*023bc8eaSKudo Chien 
213*023bc8eaSKudo Chien   return jsi::Value(static_cast<double>(newRegistrationId));
214*023bc8eaSKudo Chien }
215*023bc8eaSKudo Chien 
unregisterEventHandler(jsi::Runtime & rt,const jsi::Value & registrationId)216*023bc8eaSKudo Chien void NativeReanimatedModule::unregisterEventHandler(
217*023bc8eaSKudo Chien     jsi::Runtime &rt,
218*023bc8eaSKudo Chien     const jsi::Value &registrationId) {
219*023bc8eaSKudo Chien   unsigned long id = registrationId.asNumber();
220*023bc8eaSKudo Chien   scheduler->scheduleOnUI(
221*023bc8eaSKudo Chien       [=] { eventHandlerRegistry->unregisterEventHandler(id); });
222*023bc8eaSKudo Chien }
223*023bc8eaSKudo Chien 
getViewProp(jsi::Runtime & rt,const jsi::Value & viewTag,const jsi::Value & propName,const jsi::Value & callback)224*023bc8eaSKudo Chien jsi::Value NativeReanimatedModule::getViewProp(
225*023bc8eaSKudo Chien     jsi::Runtime &rt,
226*023bc8eaSKudo Chien     const jsi::Value &viewTag,
227*023bc8eaSKudo Chien     const jsi::Value &propName,
228*023bc8eaSKudo Chien     const jsi::Value &callback) {
229*023bc8eaSKudo Chien   const int viewTagInt = static_cast<int>(viewTag.asNumber());
230*023bc8eaSKudo Chien   std::string propNameStr = propName.asString(rt).utf8(rt);
231*023bc8eaSKudo Chien   jsi::Function fun = callback.getObject(rt).asFunction(rt);
232*023bc8eaSKudo Chien   std::shared_ptr<jsi::Function> funPtr =
233*023bc8eaSKudo Chien       std::make_shared<jsi::Function>(std::move(fun));
234*023bc8eaSKudo Chien 
235*023bc8eaSKudo Chien   scheduler->scheduleOnUI([&rt, viewTagInt, funPtr, this, propNameStr]() {
236*023bc8eaSKudo Chien     const jsi::String propNameValue =
237*023bc8eaSKudo Chien         jsi::String::createFromUtf8(rt, propNameStr);
238*023bc8eaSKudo Chien     jsi::Value result = propObtainer(rt, viewTagInt, propNameValue);
239*023bc8eaSKudo Chien     std::string resultStr = result.asString(rt).utf8(rt);
240*023bc8eaSKudo Chien 
241*023bc8eaSKudo Chien     scheduler->scheduleOnJS([&rt, resultStr, funPtr]() {
242*023bc8eaSKudo Chien       const jsi::String resultValue =
243*023bc8eaSKudo Chien           jsi::String::createFromUtf8(rt, resultStr);
244*023bc8eaSKudo Chien       funPtr->call(rt, resultValue);
245*023bc8eaSKudo Chien     });
246*023bc8eaSKudo Chien   });
247*023bc8eaSKudo Chien 
248*023bc8eaSKudo Chien   return jsi::Value::undefined();
249*023bc8eaSKudo Chien }
250*023bc8eaSKudo Chien 
enableLayoutAnimations(jsi::Runtime & rt,const jsi::Value & config)251*023bc8eaSKudo Chien jsi::Value NativeReanimatedModule::enableLayoutAnimations(
252*023bc8eaSKudo Chien     jsi::Runtime &rt,
253*023bc8eaSKudo Chien     const jsi::Value &config) {
254*023bc8eaSKudo Chien   FeaturesConfig::setLayoutAnimationEnabled(config.getBool());
255*023bc8eaSKudo Chien   return jsi::Value::undefined();
256*023bc8eaSKudo Chien }
257*023bc8eaSKudo Chien 
configureProps(jsi::Runtime & rt,const jsi::Value & uiProps,const jsi::Value & nativeProps)258*023bc8eaSKudo Chien jsi::Value NativeReanimatedModule::configureProps(
259*023bc8eaSKudo Chien     jsi::Runtime &rt,
260*023bc8eaSKudo Chien     const jsi::Value &uiProps,
261*023bc8eaSKudo Chien     const jsi::Value &nativeProps) {
262*023bc8eaSKudo Chien   configurePropsPlatformFunction(rt, uiProps, nativeProps);
263*023bc8eaSKudo Chien   return jsi::Value::undefined();
264*023bc8eaSKudo Chien }
265*023bc8eaSKudo Chien 
onEvent(std::string eventName,std::string eventAsString)266*023bc8eaSKudo Chien void NativeReanimatedModule::onEvent(
267*023bc8eaSKudo Chien     std::string eventName,
268*023bc8eaSKudo Chien     std::string eventAsString) {
269*023bc8eaSKudo Chien   try {
270*023bc8eaSKudo Chien     eventHandlerRegistry->processEvent(*runtime, eventName, eventAsString);
271*023bc8eaSKudo Chien     mapperRegistry->execute(*runtime);
272*023bc8eaSKudo Chien     if (mapperRegistry->needRunOnRender()) {
273*023bc8eaSKudo Chien       maybeRequestRender();
274*023bc8eaSKudo Chien     }
275*023bc8eaSKudo Chien   } catch (std::exception &e) {
276*023bc8eaSKudo Chien     std::string str = e.what();
277*023bc8eaSKudo Chien     this->errorHandler->setError(str);
278*023bc8eaSKudo Chien     this->errorHandler->raise();
279*023bc8eaSKudo Chien   } catch (...) {
280*023bc8eaSKudo Chien     std::string str = "OnEvent error";
281*023bc8eaSKudo Chien     this->errorHandler->setError(str);
282*023bc8eaSKudo Chien     this->errorHandler->raise();
283*023bc8eaSKudo Chien   }
284*023bc8eaSKudo Chien }
285*023bc8eaSKudo Chien 
isAnyHandlerWaitingForEvent(std::string eventName)286*023bc8eaSKudo Chien bool NativeReanimatedModule::isAnyHandlerWaitingForEvent(
287*023bc8eaSKudo Chien     std::string eventName) {
288*023bc8eaSKudo Chien   return eventHandlerRegistry->isAnyHandlerWaitingForEvent(eventName);
289*023bc8eaSKudo Chien }
290*023bc8eaSKudo Chien 
maybeRequestRender()291*023bc8eaSKudo Chien void NativeReanimatedModule::maybeRequestRender() {
292*023bc8eaSKudo Chien   if (!renderRequested) {
293*023bc8eaSKudo Chien     renderRequested = true;
294*023bc8eaSKudo Chien     requestRender(onRenderCallback, *this->runtime);
295*023bc8eaSKudo Chien   }
296*023bc8eaSKudo Chien }
297*023bc8eaSKudo Chien 
onRender(double timestampMs)298*023bc8eaSKudo Chien void NativeReanimatedModule::onRender(double timestampMs) {
299*023bc8eaSKudo Chien   try {
300*023bc8eaSKudo Chien     std::vector<FrameCallback> callbacks = frameCallbacks;
301*023bc8eaSKudo Chien     frameCallbacks.clear();
302*023bc8eaSKudo Chien     for (auto &callback : callbacks) {
303*023bc8eaSKudo Chien       callback(timestampMs);
304*023bc8eaSKudo Chien     }
305*023bc8eaSKudo Chien     mapperRegistry->execute(*runtime);
306*023bc8eaSKudo Chien 
307*023bc8eaSKudo Chien     if (mapperRegistry->needRunOnRender()) {
308*023bc8eaSKudo Chien       maybeRequestRender();
309*023bc8eaSKudo Chien     }
310*023bc8eaSKudo Chien   } catch (std::exception &e) {
311*023bc8eaSKudo Chien     std::string str = e.what();
312*023bc8eaSKudo Chien     this->errorHandler->setError(str);
313*023bc8eaSKudo Chien     this->errorHandler->raise();
314*023bc8eaSKudo Chien   } catch (...) {
315*023bc8eaSKudo Chien     std::string str = "OnRender error";
316*023bc8eaSKudo Chien     this->errorHandler->setError(str);
317*023bc8eaSKudo Chien     this->errorHandler->raise();
318*023bc8eaSKudo Chien   }
319*023bc8eaSKudo Chien }
320*023bc8eaSKudo Chien 
registerSensor(jsi::Runtime & rt,const jsi::Value & sensorType,const jsi::Value & interval,const jsi::Value & sensorDataContainer)321*023bc8eaSKudo Chien jsi::Value NativeReanimatedModule::registerSensor(
322*023bc8eaSKudo Chien     jsi::Runtime &rt,
323*023bc8eaSKudo Chien     const jsi::Value &sensorType,
324*023bc8eaSKudo Chien     const jsi::Value &interval,
325*023bc8eaSKudo Chien     const jsi::Value &sensorDataContainer) {
326*023bc8eaSKudo Chien   return animatedSensorModule.registerSensor(
327*023bc8eaSKudo Chien       rt, sensorType, interval, sensorDataContainer);
328*023bc8eaSKudo Chien }
329*023bc8eaSKudo Chien 
unregisterSensor(jsi::Runtime & rt,const jsi::Value & sensorId)330*023bc8eaSKudo Chien void NativeReanimatedModule::unregisterSensor(
331*023bc8eaSKudo Chien     jsi::Runtime &rt,
332*023bc8eaSKudo Chien     const jsi::Value &sensorId) {
333*023bc8eaSKudo Chien   animatedSensorModule.unregisterSensor(sensorId);
334*023bc8eaSKudo Chien }
335*023bc8eaSKudo Chien 
subscribeForKeyboardEvents(jsi::Runtime & rt,const jsi::Value & keyboardEventContainer)336*023bc8eaSKudo Chien jsi::Value NativeReanimatedModule::subscribeForKeyboardEvents(
337*023bc8eaSKudo Chien     jsi::Runtime &rt,
338*023bc8eaSKudo Chien     const jsi::Value &keyboardEventContainer) {
339*023bc8eaSKudo Chien   jsi::Object keyboardEventObj = keyboardEventContainer.getObject(rt);
340*023bc8eaSKudo Chien   std::unordered_map<std::string, std::shared_ptr<ShareableValue>>
341*023bc8eaSKudo Chien       sharedProperties;
342*023bc8eaSKudo Chien   std::shared_ptr<ShareableValue> keyboardStateShared = ShareableValue::adapt(
343*023bc8eaSKudo Chien       rt, keyboardEventObj.getProperty(rt, "state"), this);
344*023bc8eaSKudo Chien   std::shared_ptr<ShareableValue> heightShared = ShareableValue::adapt(
345*023bc8eaSKudo Chien       rt, keyboardEventObj.getProperty(rt, "height"), this);
346*023bc8eaSKudo Chien 
347*023bc8eaSKudo Chien   auto keyboardEventDataUpdater =
348*023bc8eaSKudo Chien       [this, &rt, keyboardStateShared, heightShared](
349*023bc8eaSKudo Chien           int keyboardState, int height) {
350*023bc8eaSKudo Chien         auto &keyboardStateValue =
351*023bc8eaSKudo Chien             ValueWrapper::asMutableValue(keyboardStateShared->valueContainer);
352*023bc8eaSKudo Chien         keyboardStateValue->setValue(rt, jsi::Value(keyboardState));
353*023bc8eaSKudo Chien 
354*023bc8eaSKudo Chien         auto &heightMutableValue =
355*023bc8eaSKudo Chien             ValueWrapper::asMutableValue(heightShared->valueContainer);
356*023bc8eaSKudo Chien         heightMutableValue->setValue(rt, jsi::Value(height));
357*023bc8eaSKudo Chien 
358*023bc8eaSKudo Chien         this->mapperRegistry->execute(*this->runtime);
359*023bc8eaSKudo Chien       };
360*023bc8eaSKudo Chien 
361*023bc8eaSKudo Chien   return subscribeForKeyboardEventsFunction(keyboardEventDataUpdater);
362*023bc8eaSKudo Chien }
363*023bc8eaSKudo Chien 
unsubscribeFromKeyboardEvents(jsi::Runtime & rt,const jsi::Value & listenerId)364*023bc8eaSKudo Chien void NativeReanimatedModule::unsubscribeFromKeyboardEvents(
365*023bc8eaSKudo Chien     jsi::Runtime &rt,
366*023bc8eaSKudo Chien     const jsi::Value &listenerId) {
367*023bc8eaSKudo Chien   unsubscribeFromKeyboardEventsFunction(listenerId.asNumber());
368*023bc8eaSKudo Chien }
369*023bc8eaSKudo Chien 
370*023bc8eaSKudo Chien } // namespace reanimated
371