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