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