1*753557f6STomasz Sapeta #include "LayoutAnimationsProxy.h"
2*753557f6STomasz Sapeta #include "FrozenObject.h"
3*753557f6STomasz Sapeta #include "MutableValue.h"
4*753557f6STomasz Sapeta #include "ShareableValue.h"
5*753557f6STomasz Sapeta #include "ValueWrapper.h"
6*753557f6STomasz Sapeta 
7*753557f6STomasz Sapeta #include <utility>
8*753557f6STomasz Sapeta 
9*753557f6STomasz Sapeta namespace ABI47_0_0reanimated {
10*753557f6STomasz Sapeta 
11*753557f6STomasz Sapeta const long long idOffset = 1e9;
12*753557f6STomasz Sapeta 
LayoutAnimationsProxy(std::function<void (int,jsi::Object newProps)> _notifyAboutProgress,std::function<void (int,bool)> _notifyAboutEnd)13*753557f6STomasz Sapeta LayoutAnimationsProxy::LayoutAnimationsProxy(
14*753557f6STomasz Sapeta     std::function<void(int, jsi::Object newProps)> _notifyAboutProgress,
15*753557f6STomasz Sapeta     std::function<void(int, bool)> _notifyAboutEnd)
16*753557f6STomasz Sapeta     : notifyAboutProgress(std::move(_notifyAboutProgress)),
17*753557f6STomasz Sapeta       notifyAboutEnd(std::move(_notifyAboutEnd)) {}
18*753557f6STomasz Sapeta 
startObserving(int tag,std::shared_ptr<MutableValue> sv,jsi::Runtime & rt)19*753557f6STomasz Sapeta void LayoutAnimationsProxy::startObserving(
20*753557f6STomasz Sapeta     int tag,
21*753557f6STomasz Sapeta     std::shared_ptr<MutableValue> sv,
22*753557f6STomasz Sapeta     jsi::Runtime &rt) {
23*753557f6STomasz Sapeta   observedValues[tag] = sv;
24*753557f6STomasz Sapeta   sv->addListener(tag + idOffset, [sv, tag, this, &rt]() {
25*753557f6STomasz Sapeta     std::shared_ptr<FrozenObject> newValue =
26*753557f6STomasz Sapeta         ValueWrapper::asFrozenObject(sv->value->valueContainer);
27*753557f6STomasz Sapeta     this->notifyAboutProgress(tag, newValue->shallowClone(rt));
28*753557f6STomasz Sapeta   });
29*753557f6STomasz Sapeta }
30*753557f6STomasz Sapeta 
stopObserving(int tag,bool finished)31*753557f6STomasz Sapeta void LayoutAnimationsProxy::stopObserving(int tag, bool finished) {
32*753557f6STomasz Sapeta   if (observedValues.count(tag) == 0) {
33*753557f6STomasz Sapeta     return;
34*753557f6STomasz Sapeta   }
35*753557f6STomasz Sapeta   std::shared_ptr<MutableValue> sv = observedValues[tag];
36*753557f6STomasz Sapeta   sv->removeListener(tag + idOffset);
37*753557f6STomasz Sapeta   observedValues.erase(tag);
38*753557f6STomasz Sapeta   this->notifyAboutEnd(tag, !finished);
39*753557f6STomasz Sapeta }
40*753557f6STomasz Sapeta 
notifyAboutCancellation(int tag)41*753557f6STomasz Sapeta void LayoutAnimationsProxy::notifyAboutCancellation(int tag) {
42*753557f6STomasz Sapeta   this->notifyAboutEnd(tag, false);
43*753557f6STomasz Sapeta }
44*753557f6STomasz Sapeta 
45*753557f6STomasz Sapeta } // namespace reanimated
46