-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
27 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,56 @@ | ||
#ifdef ONANDROID | ||
#include <AndroidScheduler.h> | ||
#endif | ||
#include <AndroidScheduler.h> | ||
#include "JSIStoreValueUser.h" | ||
|
||
namespace reanimated { | ||
|
||
std::atomic<int> StoreUser::ctr; | ||
std::recursive_mutex StoreUser::storeMutex; | ||
std::unordered_map<int, std::vector<std::shared_ptr<jsi::Value>>> StoreUser::store; | ||
std::shared_ptr<StaticStoreUser> StoreUser::staticStoreUserData = std::make_shared<StaticStoreUser>(); | ||
|
||
std::weak_ptr<jsi::Value> StoreUser::getWeakRef(jsi::Runtime &rt) { | ||
const std::lock_guard<std::recursive_mutex> lock(storeMutex); | ||
if (StoreUser::store.count(identifier) == 0) { | ||
StoreUser::store[identifier] = std::vector<std::shared_ptr<jsi::Value>>(); | ||
const std::lock_guard<std::recursive_mutex> lock(storeUserData->storeMutex); | ||
if (storeUserData->store.count(identifier) == 0) { | ||
storeUserData->store[identifier] = std::vector<std::shared_ptr<jsi::Value>>(); | ||
} | ||
std::shared_ptr<jsi::Value> sv = std::make_shared<jsi::Value>(rt, jsi::Value::undefined()); | ||
StoreUser::store[identifier].push_back(sv); | ||
storeUserData->store[identifier].push_back(sv); | ||
|
||
return sv; | ||
} | ||
|
||
StoreUser::StoreUser(std::shared_ptr<Scheduler> s): scheduler(s) { | ||
identifier = StoreUser::ctr++; | ||
storeUserData = StoreUser::staticStoreUserData; | ||
identifier = storeUserData->ctr++; | ||
} | ||
|
||
StoreUser::~StoreUser() { | ||
int id = identifier; | ||
std::shared_ptr<Scheduler> strongScheduler = scheduler.lock(); | ||
if (strongScheduler != nullptr) { | ||
std::shared_ptr<StaticStoreUser> sud = storeUserData; | ||
#ifdef ONANDROID | ||
jni::ThreadScope::WithClassLoader([&] { | ||
strongScheduler->scheduleOnUI([id]() { | ||
const std::lock_guard<std::recursive_mutex> lock(storeMutex); | ||
if (StoreUser::store.count(id) > 0) { | ||
StoreUser::store.erase(id); | ||
strongScheduler->scheduleOnUI([id, sud]() { | ||
const std::lock_guard<std::recursive_mutex> lock(sud->storeMutex); | ||
if (sud->store.count(id) > 0) { | ||
sud->store.erase(id); | ||
} | ||
}); | ||
}); | ||
#else | ||
strongScheduler->scheduleOnUI([id]() { | ||
const std::lock_guard<std::recursive_mutex> lock(storeMutex); | ||
if (StoreUser::store.count(id) > 0) { | ||
StoreUser::store.erase(id); | ||
#else | ||
strongScheduler->scheduleOnUI([id, sud]() { | ||
const std::lock_guard<std::recursive_mutex> lock(sud->storeMutex); | ||
if (sud->store.count(id) > 0) { | ||
sud->store.erase(id); | ||
} | ||
}); | ||
#endif | ||
} | ||
} | ||
|
||
|
||
void StoreUser::clearStore() { | ||
const std::lock_guard<std::recursive_mutex> lock(storeMutex); | ||
StoreUser::store.clear(); | ||
const std::lock_guard<std::recursive_mutex> lock(StoreUser::staticStoreUserData->storeMutex); | ||
StoreUser::staticStoreUserData->store.clear(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters