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