|
| 1 | +#include "NativeReanimatedModule.h" |
| 2 | +#include <memory> |
| 3 | +#include "Logger.h" |
| 4 | +#include <functional> |
| 5 | +#include <thread> |
| 6 | +#include "SpeedChecker.h" |
| 7 | +#include "Shareable.h" |
| 8 | +#include "MapperRegistry.h" |
| 9 | +#include "Mapper.h" |
| 10 | +#include "RuntimeDecorator.h" |
| 11 | +#include "EventHandlerRegistry.h" |
| 12 | +#include "EventHandler.h" |
| 13 | + |
| 14 | +using namespace facebook; |
| 15 | + |
| 16 | +namespace reanimated { |
| 17 | + |
| 18 | +void extractMutables(jsi::Runtime &rt, |
| 19 | + std::shared_ptr<ShareableValue> sv, |
| 20 | + std::vector<std::shared_ptr<MutableValue>> &res) { |
| 21 | + switch (sv->type) { |
| 22 | + case MutableValueType: |
| 23 | + res.push_back(sv->mutableValue); |
| 24 | + break; |
| 25 | + case ArrayType: |
| 26 | + for (auto &it : sv->frozenArray) { |
| 27 | + extractMutables(rt, it, res); |
| 28 | + } |
| 29 | + break; |
| 30 | + case RemoteObjectType: |
| 31 | + case ObjectType: |
| 32 | + for (auto &it : sv->frozenObject->map) { |
| 33 | + extractMutables(rt, it.second, res); |
| 34 | + } |
| 35 | + break; |
| 36 | + default: |
| 37 | + break; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +std::vector<std::shared_ptr<MutableValue>> extractMutablesFromArray(jsi::Runtime &rt, const jsi::Array &array, NativeReanimatedModule *module) { |
| 42 | + std::vector<std::shared_ptr<MutableValue>> res; |
| 43 | + for (size_t i = 0, size = array.size(rt); i < size; i++) { |
| 44 | + auto shareable = ShareableValue::adapt(rt, array.getValueAtIndex(rt, i), module); |
| 45 | + extractMutables(rt, shareable, res); |
| 46 | + } |
| 47 | + return res; |
| 48 | +} |
| 49 | + |
| 50 | +NativeReanimatedModule::NativeReanimatedModule(std::shared_ptr<CallInvoker> jsInvoker, |
| 51 | + std::shared_ptr<Scheduler> scheduler, |
| 52 | + std::unique_ptr<jsi::Runtime> rt, |
| 53 | + std::function<void(std::function<void(double)>)> requestRender, |
| 54 | + std::function<void(jsi::Runtime&, int, const jsi::Object&)> propUpdater): |
| 55 | +NativeReanimatedModuleSpec(jsInvoker), |
| 56 | +scheduler(scheduler), |
| 57 | +runtime(std::move(rt)), |
| 58 | +mapperRegistry(new MapperRegistry()), |
| 59 | +eventHandlerRegistry(new EventHandlerRegistry()), |
| 60 | +requestRender(requestRender) { |
| 61 | + RuntimeDecorator::addNativeObjects(*runtime, propUpdater, [=](FrameCallback callback) { |
| 62 | + frameCallbacks.push_back(callback); |
| 63 | + maybeRequestRender(); |
| 64 | + }); |
| 65 | +} |
| 66 | + |
| 67 | +bool NativeReanimatedModule::isUIRuntime(jsi::Runtime &rt) { |
| 68 | + return runtime.get() == &rt; |
| 69 | +} |
| 70 | + |
| 71 | +bool NativeReanimatedModule::isHostRuntime(jsi::Runtime &rt) { |
| 72 | + return !isUIRuntime(rt); |
| 73 | +} |
| 74 | + |
| 75 | +void NativeReanimatedModule::installCoreFunctions(jsi::Runtime &rt, const jsi::Value &valueSetter) { |
| 76 | + this->valueSetter = ShareableValue::adapt(rt, valueSetter, this); |
| 77 | +} |
| 78 | + |
| 79 | +jsi::Value NativeReanimatedModule::makeShareable(jsi::Runtime &rt, const jsi::Value &value) { |
| 80 | + return ShareableValue::adapt(rt, value, this)->getValue(rt); |
| 81 | +} |
| 82 | + |
| 83 | +jsi::Value NativeReanimatedModule::makeMutable(jsi::Runtime &rt, const jsi::Value &value) { |
| 84 | + return ShareableValue::adapt(rt, value, this, MutableValueType)->getValue(rt); |
| 85 | +} |
| 86 | + |
| 87 | +jsi::Value NativeReanimatedModule::makeRemote(jsi::Runtime &rt, const jsi::Value &value) { |
| 88 | + return ShareableValue::adapt(rt, value, this, RemoteObjectType)->getValue(rt); |
| 89 | +} |
| 90 | + |
| 91 | +jsi::Value NativeReanimatedModule::startMapper(jsi::Runtime &rt, const jsi::Value &worklet, const jsi::Value &inputs, const jsi::Value &outputs) { |
| 92 | + static unsigned long MAPPER_ID = 1; |
| 93 | + |
| 94 | + unsigned long newMapperId = MAPPER_ID++; |
| 95 | + auto mapperShareable = ShareableValue::adapt(rt, worklet, this); |
| 96 | + auto inputMutables = extractMutablesFromArray(rt, inputs.asObject(rt).asArray(rt), this); |
| 97 | + auto outputMutables = extractMutablesFromArray(rt, outputs.asObject(rt).asArray(rt), this); |
| 98 | + |
| 99 | + scheduler->scheduleOnUI([=] { |
| 100 | + auto mapperFunction = mapperShareable->getValue(*runtime).asObject(*runtime).asFunction(*runtime); |
| 101 | + auto mapper = std::make_shared<Mapper>(this, newMapperId, std::move(mapperFunction), inputMutables, outputMutables); |
| 102 | + mapperRegistry->startMapper(mapper); |
| 103 | + maybeRequestRender(); |
| 104 | + }); |
| 105 | + |
| 106 | + return jsi::Value((double)newMapperId); |
| 107 | +} |
| 108 | + |
| 109 | +void NativeReanimatedModule::stopMapper(jsi::Runtime &rt, const jsi::Value &mapperId) { |
| 110 | + unsigned long id = mapperId.asNumber(); |
| 111 | + scheduler->scheduleOnUI([=] { |
| 112 | + mapperRegistry->stopMapper(id); |
| 113 | + }); |
| 114 | +} |
| 115 | + |
| 116 | +jsi::Value NativeReanimatedModule::registerEventHandler(jsi::Runtime &rt, const jsi::Value &eventHash, const jsi::Value &worklet) { |
| 117 | + static unsigned long EVENT_HANDLER_ID = 1; |
| 118 | + |
| 119 | + unsigned long newRegistrationId = EVENT_HANDLER_ID++; |
| 120 | + auto eventName = eventHash.asString(rt).utf8(rt); |
| 121 | + auto handlerShareable = ShareableValue::adapt(rt, worklet, this); |
| 122 | + |
| 123 | + scheduler->scheduleOnUI([=] { |
| 124 | + auto handlerFunction = handlerShareable->getValue(*runtime).asObject(*runtime).asFunction(*runtime); |
| 125 | + auto handler = std::make_shared<EventHandler>(newRegistrationId, eventName, std::move(handlerFunction)); |
| 126 | + eventHandlerRegistry->registerEventHandler(handler); |
| 127 | + }); |
| 128 | + |
| 129 | + return jsi::Value((double)newRegistrationId); |
| 130 | +} |
| 131 | + |
| 132 | +void NativeReanimatedModule::unregisterEventHandler(jsi::Runtime &rt, const jsi::Value ®istrationId) { |
| 133 | + unsigned long id = registrationId.asNumber(); |
| 134 | + scheduler->scheduleOnUI([=] { |
| 135 | + eventHandlerRegistry->unregisterEventHandler(id); |
| 136 | + }); |
| 137 | +} |
| 138 | + |
| 139 | +void NativeReanimatedModule::onEvent(std::string eventName, std::string eventAsString) { |
| 140 | + eventHandlerRegistry->processEvent(*runtime, eventName, eventAsString); |
| 141 | +} |
| 142 | + |
| 143 | +void NativeReanimatedModule::maybeRequestRender() { |
| 144 | + if (!renderRequested) { |
| 145 | + renderRequested = true; |
| 146 | + requestRender([this](double timestampMs) { |
| 147 | + this->renderRequested = false; |
| 148 | + this->onRender(timestampMs); |
| 149 | + }); |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +void NativeReanimatedModule::onRender(double timestampMs) { |
| 154 | + mapperRegistry->execute(*runtime); |
| 155 | + |
| 156 | + std::vector<FrameCallback> callbacks = frameCallbacks; |
| 157 | + frameCallbacks.clear(); |
| 158 | + for (auto callback : callbacks) { |
| 159 | + callback(timestampMs); |
| 160 | + } |
| 161 | + |
| 162 | + if (mapperRegistry->needRunOnRender()) { |
| 163 | + maybeRequestRender(); |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +NativeReanimatedModule::~NativeReanimatedModule() { |
| 168 | + // noop |
| 169 | +} |
| 170 | + |
| 171 | +} |
0 commit comments