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