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