Skip to content

Commit

Permalink
Fix problem with user store
Browse files Browse the repository at this point in the history
  • Loading branch information
piaskowyk committed Sep 9, 2021
1 parent ccb8ab6 commit ed2c575
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
40 changes: 19 additions & 21 deletions Common/cpp/Tools/JSIStoreValueUser.cpp
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();
}

}
11 changes: 8 additions & 3 deletions Common/cpp/headers/Tools/JSIStoreValueUser.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@ using namespace facebook;

namespace reanimated {

struct StaticStoreUser {
std::atomic<int> ctr;
std::unordered_map<int, std::vector<std::shared_ptr<jsi::Value>>> store;
std::recursive_mutex storeMutex;
};

class StoreUser {
int identifier = 0;
static std::atomic<int> ctr;
static std::unordered_map<int, std::vector<std::shared_ptr<jsi::Value>>> store;
static std::recursive_mutex storeMutex;
static std::shared_ptr<StaticStoreUser> staticStoreUserData;
std::shared_ptr<StaticStoreUser> storeUserData;
std::weak_ptr<Scheduler> scheduler;

public:
Expand Down

0 comments on commit ed2c575

Please sign in to comment.