react-native-nitro-ark 0.0.97 → 0.0.99

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -17,88 +17,111 @@ class BarkNotificationSubscription final : public HybridBarkNotificationSubscrip
17
17
  public:
18
18
  BarkNotificationSubscription(rust::Box<bark_cxx::NotificationSubscription> subscription,
19
19
  std::function<void(const BarkNotificationEvent&)>&& onEvent)
20
- : HybridObject(TAG), subscription_(std::move(subscription)), onEvent_(std::move(onEvent)),
21
- worker_([this]() { pumpEvents(); }) {}
20
+ : HybridObject(TAG), state_(std::make_shared<State>(std::move(subscription), std::move(onEvent))) {
21
+ state_->worker = std::thread([state = state_]() { pumpEvents(state); });
22
+ }
22
23
 
23
24
  ~BarkNotificationSubscription() override {
24
- stopInternal(false);
25
+ stopInternal();
25
26
  }
26
27
 
27
28
  void stop() override {
28
- stopInternal(true);
29
+ stopInternal();
29
30
  }
30
31
 
31
32
  bool isActive() override {
32
- if (!isActive_.load()) {
33
+ if (!state_ || !state_->isActive.load()) {
33
34
  return false;
34
35
  }
35
36
 
36
- std::lock_guard<std::mutex> lock(subscriptionMutex_);
37
- return subscription_->is_active();
37
+ std::lock_guard<std::mutex> lock(state_->subscriptionMutex);
38
+ return state_->subscription->is_active();
38
39
  }
39
40
 
40
41
  private:
41
- void stopInternal(bool rethrowErrors) {
42
- const bool wasActive = isActive_.exchange(false);
42
+ struct State {
43
+ State(rust::Box<bark_cxx::NotificationSubscription> nextSubscription,
44
+ std::function<void(const BarkNotificationEvent&)>&& nextOnEvent)
45
+ : subscription(std::move(nextSubscription)), onEvent(std::move(nextOnEvent)) {}
46
+
47
+ rust::Box<bark_cxx::NotificationSubscription> subscription;
48
+ std::function<void(const BarkNotificationEvent&)> onEvent;
49
+ std::thread worker;
50
+ std::atomic<bool> isActive{true};
51
+ std::atomic<bool> cleanupScheduled{false};
52
+ std::mutex subscriptionMutex;
53
+ };
54
+
55
+ void stopInternal() {
56
+ if (!state_) {
57
+ return;
58
+ }
59
+
60
+ state_->isActive.store(false);
61
+ scheduleCleanup(state_);
62
+ }
43
63
 
44
- if (wasActive) {
64
+ static void scheduleCleanup(const std::shared_ptr<State>& state) {
65
+ if (state->cleanupScheduled.exchange(true)) {
66
+ return;
67
+ }
68
+
69
+ std::thread([state]() {
45
70
  try {
46
- std::lock_guard<std::mutex> lock(subscriptionMutex_);
47
- subscription_->stop();
48
- } catch (const std::exception& error) {
49
- if (rethrowErrors) {
50
- throw std::runtime_error(error.what());
71
+ std::lock_guard<std::mutex> lock(state->subscriptionMutex);
72
+ if (state->subscription->is_active()) {
73
+ state->subscription->stop();
51
74
  }
75
+ } catch (...) {
52
76
  }
53
- }
54
77
 
55
- joinWorker();
78
+ joinWorker(state);
79
+ }).detach();
56
80
  }
57
81
 
58
- void joinWorker() {
59
- if (!worker_.joinable()) {
82
+ static void joinWorker(const std::shared_ptr<State>& state) {
83
+ if (!state->worker.joinable()) {
60
84
  return;
61
85
  }
62
86
 
63
- if (worker_.get_id() == std::this_thread::get_id()) {
64
- worker_.detach();
87
+ if (state->worker.get_id() == std::this_thread::get_id()) {
88
+ state->worker.detach();
65
89
  return;
66
90
  }
67
91
 
68
- worker_.join();
92
+ state->worker.join();
69
93
  }
70
94
 
71
- void pumpEvents() {
95
+ static void pumpEvents(const std::shared_ptr<State>& state) {
72
96
  try {
73
- while (isActive_.load()) {
97
+ while (state->isActive.load()) {
74
98
  bark_cxx::NotificationPollResult result;
75
99
  {
76
- std::lock_guard<std::mutex> lock(subscriptionMutex_);
77
- result = subscription_->wait_next(250);
100
+ std::lock_guard<std::mutex> lock(state->subscriptionMutex);
101
+ result = state->subscription->wait_next(250);
78
102
  }
79
103
 
80
- if (!isActive_.load()) {
104
+ if (!state->isActive.load()) {
81
105
  break;
82
106
  }
83
107
 
84
108
  if (result.has_event) {
85
- onEvent_(convertEvent(result.event));
109
+ state->onEvent(convertEvent(result.event));
86
110
  }
87
111
 
88
112
  if (!result.is_active) {
89
- isActive_.store(false);
113
+ state->isActive.store(false);
90
114
  break;
91
115
  }
92
116
  }
93
117
  } catch (...) {
94
- isActive_.store(false);
118
+ state->isActive.store(false);
95
119
  }
96
120
  }
97
121
 
98
122
  static BarkMovementDestination convertDestination(const bark_cxx::BarkMovementDestination& destinationRs) {
99
123
  BarkMovementDestination destination;
100
- destination.destination =
101
- std::string(destinationRs.destination.data(), destinationRs.destination.length());
124
+ destination.destination = std::string(destinationRs.destination.data(), destinationRs.destination.length());
102
125
  destination.payment_method =
103
126
  std::string(destinationRs.payment_method.data(), destinationRs.payment_method.length());
104
127
  destination.amount_sat = static_cast<double>(destinationRs.amount_sat);
@@ -122,10 +145,8 @@ private:
122
145
  movement.completed_at = std::string(movementRs.completed_at.data(), movementRs.completed_at.length());
123
146
  }
124
147
 
125
- movement.subsystem.name =
126
- std::string(movementRs.subsystem_name.data(), movementRs.subsystem_name.length());
127
- movement.subsystem.kind =
128
- std::string(movementRs.subsystem_kind.data(), movementRs.subsystem_kind.length());
148
+ movement.subsystem.name = std::string(movementRs.subsystem_name.data(), movementRs.subsystem_name.length());
149
+ movement.subsystem.kind = std::string(movementRs.subsystem_kind.data(), movementRs.subsystem_kind.length());
129
150
 
130
151
  movement.sent_to.reserve(movementRs.sent_to.size());
131
152
  for (const auto& destinationRs : movementRs.sent_to) {
@@ -169,11 +190,7 @@ private:
169
190
  }
170
191
 
171
192
  private:
172
- rust::Box<bark_cxx::NotificationSubscription> subscription_;
173
- std::function<void(const BarkNotificationEvent&)> onEvent_;
174
- std::thread worker_;
175
- std::atomic<bool> isActive_{true};
176
- std::mutex subscriptionMutex_;
193
+ std::shared_ptr<State> state_;
177
194
  };
178
195
 
179
196
  } // namespace margelo::nitro::nitroark
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-nitro-ark",
3
- "version": "0.0.97",
3
+ "version": "0.0.99",
4
4
  "description": "Pure C++ Nitro Modules for Ark client",
5
5
  "source": "./src/index.tsx",
6
6
  "main": "./lib/module/index.js",