1 #include "AnimatedSensorModule.h"
2 #include "MutableValue.h"
3 #include "ValueWrapper.h"
4 
5 #include <memory>
6 #include <utility>
7 
8 namespace ABI48_0_0reanimated {
9 
AnimatedSensorModule(const PlatformDepMethodsHolder & platformDepMethodsHolder,RuntimeManager * runtimeManager)10 AnimatedSensorModule::AnimatedSensorModule(
11     const PlatformDepMethodsHolder &platformDepMethodsHolder,
12     RuntimeManager *runtimeManager)
13     : platformRegisterSensorFunction_(platformDepMethodsHolder.registerSensor),
14       platformUnregisterSensorFunction_(
15           platformDepMethodsHolder.unregisterSensor),
16       runtimeManager_(runtimeManager) {}
17 
~AnimatedSensorModule()18 AnimatedSensorModule::~AnimatedSensorModule() {
19   // It is called during app reload because app reload doesn't call hooks
20   // unmounting
21   for (auto sensorId : sensorsIds_) {
22     platformUnregisterSensorFunction_(sensorId);
23   }
24 }
25 
registerSensor(jsi::Runtime & rt,const jsi::Value & sensorType,const jsi::Value & interval,const jsi::Value & sensorDataContainer)26 jsi::Value AnimatedSensorModule::registerSensor(
27     jsi::Runtime &rt,
28     const jsi::Value &sensorType,
29     const jsi::Value &interval,
30     const jsi::Value &sensorDataContainer) {
31   std::shared_ptr<ShareableValue> sensorsData = ShareableValue::adapt(
32       rt, sensorDataContainer.getObject(rt), runtimeManager_);
33   auto &mutableObject =
34       ValueWrapper::asMutableValue(sensorsData->valueContainer);
35 
36   std::function<void(double[])> setter;
37   if (sensorType.asNumber() == SensorType::ROTATION_VECTOR) {
38     setter = [&, mutableObject](double newValues[]) {
39       jsi::Runtime &runtime = *runtimeManager_->runtime.get();
40       jsi::Object value(runtime);
41       value.setProperty(runtime, "qx", newValues[0]);
42       value.setProperty(runtime, "qy", newValues[1]);
43       value.setProperty(runtime, "qz", newValues[2]);
44       value.setProperty(runtime, "qw", newValues[3]);
45       value.setProperty(runtime, "yaw", newValues[4]);
46       value.setProperty(runtime, "pitch", newValues[5]);
47       value.setProperty(runtime, "roll", newValues[6]);
48       mutableObject->setValue(runtime, std::move(value));
49     };
50   } else {
51     setter = [&, mutableObject](double newValues[]) {
52       jsi::Runtime &runtime = *runtimeManager_->runtime.get();
53       jsi::Object value(runtime);
54       value.setProperty(runtime, "x", newValues[0]);
55       value.setProperty(runtime, "y", newValues[1]);
56       value.setProperty(runtime, "z", newValues[2]);
57       mutableObject->setValue(runtime, std::move(value));
58     };
59   }
60 
61   int sensorId = platformRegisterSensorFunction_(
62       sensorType.asNumber(), interval.asNumber(), setter);
63   if (sensorId != -1) {
64     sensorsIds_.insert(sensorId);
65   }
66   return jsi::Value(sensorId);
67 }
68 
unregisterSensor(const jsi::Value & sensorId)69 void AnimatedSensorModule::unregisterSensor(const jsi::Value &sensorId) {
70   // It is called during sensor hook unmounting
71   sensorsIds_.erase(sensorId.getNumber());
72   platformUnregisterSensorFunction_(sensorId.asNumber());
73 }
74 
75 } // namespace reanimated
76