1*fe5cfb17STomasz Sapeta #include "JSIStoreValueUser.h"
2*fe5cfb17STomasz Sapeta #include "RuntimeManager.h"
3*fe5cfb17STomasz Sapeta #ifdef ANDROID
4*fe5cfb17STomasz Sapeta #include <AndroidScheduler.h>
5*fe5cfb17STomasz Sapeta #endif
6*fe5cfb17STomasz Sapeta 
7*fe5cfb17STomasz Sapeta namespace ABI48_0_0reanimated {
8*fe5cfb17STomasz Sapeta 
getWeakRef(jsi::Runtime & rt)9*fe5cfb17STomasz Sapeta std::weak_ptr<jsi::Value> StoreUser::getWeakRef(jsi::Runtime &rt) {
10*fe5cfb17STomasz Sapeta   const std::lock_guard<std::recursive_mutex> lock(storeUserData->storeMutex);
11*fe5cfb17STomasz Sapeta   if (storeUserData->store.count(identifier) == 0) {
12*fe5cfb17STomasz Sapeta     storeUserData->store[identifier] =
13*fe5cfb17STomasz Sapeta         std::vector<std::shared_ptr<jsi::Value>>();
14*fe5cfb17STomasz Sapeta   }
15*fe5cfb17STomasz Sapeta   std::shared_ptr<jsi::Value> sv =
16*fe5cfb17STomasz Sapeta       std::make_shared<jsi::Value>(rt, jsi::Value::undefined());
17*fe5cfb17STomasz Sapeta   storeUserData->store[identifier].push_back(sv);
18*fe5cfb17STomasz Sapeta 
19*fe5cfb17STomasz Sapeta   return sv;
20*fe5cfb17STomasz Sapeta }
21*fe5cfb17STomasz Sapeta 
StoreUser(std::shared_ptr<Scheduler> s,const RuntimeManager & runtimeManager)22*fe5cfb17STomasz Sapeta StoreUser::StoreUser(
23*fe5cfb17STomasz Sapeta     std::shared_ptr<Scheduler> s,
24*fe5cfb17STomasz Sapeta     const RuntimeManager &runtimeManager)
25*fe5cfb17STomasz Sapeta     : scheduler(s) {
26*fe5cfb17STomasz Sapeta   storeUserData = runtimeManager.storeUserData;
27*fe5cfb17STomasz Sapeta   identifier = storeUserData->ctr++;
28*fe5cfb17STomasz Sapeta }
29*fe5cfb17STomasz Sapeta 
~StoreUser()30*fe5cfb17STomasz Sapeta StoreUser::~StoreUser() {
31*fe5cfb17STomasz Sapeta   int id = identifier;
32*fe5cfb17STomasz Sapeta   std::shared_ptr<Scheduler> strongScheduler = scheduler.lock();
33*fe5cfb17STomasz Sapeta   if (strongScheduler != nullptr) {
34*fe5cfb17STomasz Sapeta     std::shared_ptr<StaticStoreUser> sud = storeUserData;
35*fe5cfb17STomasz Sapeta #ifdef ANDROID
36*fe5cfb17STomasz Sapeta     jni::ThreadScope::WithClassLoader([&] {
37*fe5cfb17STomasz Sapeta       strongScheduler->scheduleOnUI([id, sud]() {
38*fe5cfb17STomasz Sapeta         const std::lock_guard<std::recursive_mutex> lock(sud->storeMutex);
39*fe5cfb17STomasz Sapeta         if (sud->store.count(id) > 0) {
40*fe5cfb17STomasz Sapeta           sud->store.erase(id);
41*fe5cfb17STomasz Sapeta         }
42*fe5cfb17STomasz Sapeta       });
43*fe5cfb17STomasz Sapeta     });
44*fe5cfb17STomasz Sapeta #else
45*fe5cfb17STomasz Sapeta     strongScheduler->scheduleOnUI([id, sud]() {
46*fe5cfb17STomasz Sapeta       const std::lock_guard<std::recursive_mutex> lock(sud->storeMutex);
47*fe5cfb17STomasz Sapeta       if (sud->store.count(id) > 0) {
48*fe5cfb17STomasz Sapeta         sud->store.erase(id);
49*fe5cfb17STomasz Sapeta       }
50*fe5cfb17STomasz Sapeta     });
51*fe5cfb17STomasz Sapeta #endif
52*fe5cfb17STomasz Sapeta   }
53*fe5cfb17STomasz Sapeta }
54*fe5cfb17STomasz Sapeta 
55*fe5cfb17STomasz Sapeta } // namespace reanimated
56