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