1/*
2 * Copyright (c) Facebook, Inc. and its affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#import <ABI47_0_0EXAV/CallbackWrapper.h>
9
10/**
11 * NOTE: This file is a copy of ReactCommon/LongLivedObject.cpp
12 * Copying it here is needed until we upgrade RN to 0.66 that includes this commit:
13 * https://github.com/facebook/react-native/commit/32bfd7a857c23dd417f940d0c09843de257f6c61
14 * After that we can just use <ReactCommon/TurboModuleUtils.h>
15 *
16 * We need to wrap it in another napespace, because it would conflict with existing RN implementation
17 */
18namespace ABI47_0_0expo {
19
20// LongLivedObjectCollection
21LongLivedObjectCollection &LongLivedObjectCollection::get() {
22  static LongLivedObjectCollection instance;
23  return instance;
24}
25
26void LongLivedObjectCollection::add(std::shared_ptr<LongLivedObject> so) const {
27  std::lock_guard<std::mutex> lock(collectionMutex_);
28  collection_.insert(so);
29}
30
31void LongLivedObjectCollection::remove(const LongLivedObject *o) const {
32  std::lock_guard<std::mutex> lock(collectionMutex_);
33  auto p = collection_.begin();
34  for (; p != collection_.end(); p++) {
35    if (p->get() == o) {
36      break;
37    }
38  }
39  if (p != collection_.end()) {
40    collection_.erase(p);
41  }
42}
43
44void LongLivedObjectCollection::clear() const {
45  std::lock_guard<std::mutex> lock(collectionMutex_);
46  collection_.clear();
47}
48
49// LongLivedObject
50void LongLivedObject::allowRelease() {
51  LongLivedObjectCollection::get().remove(this);
52}
53
54}
55
56