1414
1515#ifndef SRC_RCL_HANDLE_HPP_
1616#define SRC_RCL_HANDLE_HPP_
17+
1718#include < napi.h>
1819
1920#include < functional>
20- #include < iostream>
2121#include < map>
22- #include < mutex>
2322#include < set>
2423#include < string>
25- # include < vector >
24+
2625namespace rclnodejs {
2726
2827class RclHandle : public Napi ::ObjectWrap<RclHandle> {
@@ -43,7 +42,6 @@ class RclHandle : public Napi::ObjectWrap<RclHandle> {
4342 void Reset ();
4443 void AddChild (RclHandle* child) { children_.insert (child); }
4544 void RemoveChild (RclHandle* child) { children_.erase (child); }
46- // void SetBoolProperty(const std::string& name, bool value);
4745 void SyncProperties ();
4846
4947 explicit RclHandle (const Napi::CallbackInfo& info);
@@ -57,88 +55,9 @@ class RclHandle : public Napi::ObjectWrap<RclHandle> {
5755 // Property getter
5856 Napi::Value PropertiesGetter (const Napi::CallbackInfo& info);
5957
60- // Store property updates to be applied later
61- struct PropertyUpdate {
62- std::string name;
63- bool value;
64- };
65-
66- std::mutex property_mutex_;
67- std::vector<PropertyUpdate> pending_property_updates_;
68-
69- // ThreadSafeFunction to process updates on main thread
70- static Napi::ThreadSafeFunction tsfn_;
71-
72- // Thread-safe version to queue property updates
73- // void SetBoolProperty(const std::string& name, bool value) {
74-
75- // std::lock_guard<std::mutex> lock(property_mutex_);
76- // pending_property_updates_.push_back({name, value});
77- // if (tsfn_) {
78- // // Queue work to be processed on main thread
79- // tsfn_.BlockingCall(this, []( Napi::Env env, Napi::Function
80- // jsCallback, RclHandle* handle) {
81- // std::cout << "====ProcessPropertyUpdates" << std::endl;
82- // handle->ProcessPropertyUpdates();
83- // });
84- // // napi_status status = tsfn_.BlockingCall(this, [](Napi::Env env,
85- // Napi::Function jsCallback, RclHandle* handle) {
86- // // std::cout << "====SetBoolProperty" << std::endl;
87- // // });
88- // // if ( status != napi_ok )
89- // // {
90- // // std::cout << "===ERRRRRRRRRRRRRrr" << std::endl;
91- // // }
92- // }
93- // }
9458 void SetBoolProperty (const std::string& name, bool value) {
9559 properties_[name] = value;
9660 }
97- // Process pending property updates on main thread
98- void ProcessPropertyUpdates () {
99- std::vector<PropertyUpdate> updates;
100- {
101- std::lock_guard<std::mutex> lock (property_mutex_);
102- updates.swap (pending_property_updates_);
103- }
104-
105- Napi::Env env = Env (); // Get env from ObjectWrap
106- for (const auto & update : updates) {
107- if (!properties_obj_.IsEmpty ()) {
108- Napi::Object props = properties_obj_.Value ().As <Napi::Object>();
109- props.Set (update.name , Napi::Boolean::New (env, update.value ));
110- }
111- }
112- }
113-
114- // Initialize thread-safe function
115- static void InitThreadSafeFunction (Napi::Env env) {
116- if (!tsfn_) {
117- // std::cout << "====InitThreadSafeFunction" << std::endl;
118- tsfn_ = Napi::ThreadSafeFunction::New (
119- env,
120- Napi::Function::New (env,
121- [](const Napi::CallbackInfo& info) {
122- // This runs on the main thread
123- // Napi::Env env = info.Env();
124- // RclHandle* handle =
125- // RclHandle::Unwrap(info[0].As<Napi::Object>());
126- // handle->ProcessPropertyUpdates();
127- // std::cout << "====ProcessPropertyUpdates" <<
128- // std::endl;
129- }),
130- " SetBoolProperty" ,
131- 0 , // Max queue size (0 = unlimited)
132- 1 ); // Number of threads to use);
133- }
134- }
135-
136- // Finalize the thread-safe function
137- static void CleanupThreadSafeFunction () {
138- if (tsfn_) {
139- tsfn_.Release ();
140- }
141- }
14261
14362 private:
14463 void * pointer_;
0 commit comments