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