1*fe5cfb17STomasz Sapeta #include "Mapper.h"
2*fe5cfb17STomasz Sapeta #include "MutableValue.h"
3*fe5cfb17STomasz Sapeta #include "SharedParent.h"
4*fe5cfb17STomasz Sapeta 
5*fe5cfb17STomasz Sapeta namespace ABI48_0_0reanimated {
6*fe5cfb17STomasz Sapeta 
Mapper(NativeReanimatedModule * module,unsigned long id,std::shared_ptr<jsi::Function> mapper,std::vector<std::shared_ptr<MutableValue>> inputs,std::vector<std::shared_ptr<MutableValue>> outputs)7*fe5cfb17STomasz Sapeta Mapper::Mapper(
8*fe5cfb17STomasz Sapeta     NativeReanimatedModule *module,
9*fe5cfb17STomasz Sapeta     unsigned long id,
10*fe5cfb17STomasz Sapeta     std::shared_ptr<jsi::Function> mapper,
11*fe5cfb17STomasz Sapeta     std::vector<std::shared_ptr<MutableValue>> inputs,
12*fe5cfb17STomasz Sapeta     std::vector<std::shared_ptr<MutableValue>> outputs)
13*fe5cfb17STomasz Sapeta     : id(id), module(module), mapper(mapper), inputs(inputs), outputs(outputs) {
14*fe5cfb17STomasz Sapeta   auto markDirty = [this, module]() {
15*fe5cfb17STomasz Sapeta     this->dirty = true;
16*fe5cfb17STomasz Sapeta     module->maybeRequestRender();
17*fe5cfb17STomasz Sapeta   };
18*fe5cfb17STomasz Sapeta   for (auto input : inputs) {
19*fe5cfb17STomasz Sapeta     input->addListener(id, markDirty);
20*fe5cfb17STomasz Sapeta   }
21*fe5cfb17STomasz Sapeta }
22*fe5cfb17STomasz Sapeta 
execute(jsi::Runtime & rt)23*fe5cfb17STomasz Sapeta void Mapper::execute(jsi::Runtime &rt) {
24*fe5cfb17STomasz Sapeta   dirty = false;
25*fe5cfb17STomasz Sapeta   if (optimalizationLvl == 0) {
26*fe5cfb17STomasz Sapeta     mapper->callWithThis(rt, *mapper); // call styleUpdater
27*fe5cfb17STomasz Sapeta   } else {
28*fe5cfb17STomasz Sapeta     jsi::Object newStyle = userUpdater->call(rt).asObject(rt);
29*fe5cfb17STomasz Sapeta     auto jsViewDescriptorArray = viewDescriptors->getValue(rt)
30*fe5cfb17STomasz Sapeta                                      .getObject(rt)
31*fe5cfb17STomasz Sapeta                                      .getProperty(rt, "value")
32*fe5cfb17STomasz Sapeta                                      .asObject(rt)
33*fe5cfb17STomasz Sapeta                                      .getArray(rt);
34*fe5cfb17STomasz Sapeta     for (int i = 0; i < jsViewDescriptorArray.length(rt); ++i) {
35*fe5cfb17STomasz Sapeta       auto jsViewDescriptor =
36*fe5cfb17STomasz Sapeta           jsViewDescriptorArray.getValueAtIndex(rt, i).getObject(rt);
37*fe5cfb17STomasz Sapeta       (*updateProps)(
38*fe5cfb17STomasz Sapeta           rt,
39*fe5cfb17STomasz Sapeta           static_cast<int>(jsViewDescriptor.getProperty(rt, "tag").asNumber()),
40*fe5cfb17STomasz Sapeta           jsViewDescriptor.getProperty(rt, "name"),
41*fe5cfb17STomasz Sapeta           newStyle);
42*fe5cfb17STomasz Sapeta     }
43*fe5cfb17STomasz Sapeta   }
44*fe5cfb17STomasz Sapeta }
45*fe5cfb17STomasz Sapeta 
enableFastMode(const int optimalizationLvl,const std::shared_ptr<ShareableValue> & updater,const std::shared_ptr<ShareableValue> & jsViewDescriptors)46*fe5cfb17STomasz Sapeta void Mapper::enableFastMode(
47*fe5cfb17STomasz Sapeta     const int optimalizationLvl,
48*fe5cfb17STomasz Sapeta     const std::shared_ptr<ShareableValue> &updater,
49*fe5cfb17STomasz Sapeta     const std::shared_ptr<ShareableValue> &jsViewDescriptors) {
50*fe5cfb17STomasz Sapeta   if (optimalizationLvl == 0) {
51*fe5cfb17STomasz Sapeta     return;
52*fe5cfb17STomasz Sapeta   }
53*fe5cfb17STomasz Sapeta   viewDescriptors = jsViewDescriptors;
54*fe5cfb17STomasz Sapeta   this->optimalizationLvl = optimalizationLvl;
55*fe5cfb17STomasz Sapeta   updateProps = &module->updaterFunction;
56*fe5cfb17STomasz Sapeta   jsi::Runtime *rt = module->runtime.get();
57*fe5cfb17STomasz Sapeta   userUpdater = std::make_shared<jsi::Function>(
58*fe5cfb17STomasz Sapeta       updater->getValue(*rt).asObject(*rt).asFunction(*rt));
59*fe5cfb17STomasz Sapeta }
60*fe5cfb17STomasz Sapeta 
~Mapper()61*fe5cfb17STomasz Sapeta Mapper::~Mapper() {
62*fe5cfb17STomasz Sapeta   for (auto input : inputs) {
63*fe5cfb17STomasz Sapeta     input->removeListener(id);
64*fe5cfb17STomasz Sapeta   }
65*fe5cfb17STomasz Sapeta }
66*fe5cfb17STomasz Sapeta 
67*fe5cfb17STomasz Sapeta } // namespace reanimated
68