@@ -12,6 +12,8 @@ template <typename T, typename L = void*>
1212class CallbackBridge {
1313 public:
1414 CallbackBridge (v8::Local<v8::Function>, bool );
15+ CallbackBridge (const CallbackBridge<T,L> &);
16+ CallbackBridge<T,L>& operator =(const CallbackBridge<T,L> &);
1517 virtual ~CallbackBridge ();
1618
1719 // Executes the callback
@@ -21,6 +23,8 @@ class CallbackBridge {
2123 // We will expose a bridge object to the JS callback that wraps this instance so we don't loose context.
2224 // This is the V8 constructor for such objects.
2325 static Nan::MaybeLocal<v8::Function> get_wrapper_constructor ();
26+ void init_uv (void );
27+ void init_wrapper (void );
2428 static void async_gone (uv_handle_t *handle);
2529 static NAN_METHOD (New);
2630 static NAN_METHOD (ReturnCallback);
@@ -59,15 +63,61 @@ CallbackBridge<T, L>::CallbackBridge(v8::Local<v8::Function> callback, bool is_s
5963 * This is invoked from the main JavaScript thread.
6064 * V8 context is available.
6165 */
62- Nan::HandleScope scope;
66+ init_uv ();
67+ init_wrapper ();
68+ }
69+
70+ template <typename T, typename L>
71+ CallbackBridge<T, L>::CallbackBridge(const CallbackBridge<T,L>& other) : callback(new Nan::Callback(other.callback->GetFunction ())), is_sync(other.is_sync) {
72+ /*
73+ * This is invoked from the main JavaScript thread.
74+ * V8 context is available.
75+ */
76+ init_uv ();
77+ init_wrapper ();
78+ }
79+
80+ template <typename T, typename L>
81+ CallbackBridge<T, L>&
82+ CallbackBridge<T, L>::operator = (const CallbackBridge<T,L>& other)
83+ {
84+ /*
85+ * This is invoked from the main JavaScript thread.
86+ * V8 context is available.
87+ */
88+ if (other != *this ) {
89+ delete this ->callback ;
90+ this ->wrapper .Reset ();
91+ uv_cond_destroy (&this ->condition_variable );
92+ uv_mutex_destroy (&this ->cv_mutex );
93+ if (!is_sync) {
94+ uv_close (this ->async , &async_gone);
95+ }
96+
97+ this ->callback = new Nan::Callback (other.callback ->GetFunction ());
98+ this ->is_sync = other.is_sync ;
99+ init_uv ();
100+ init_wrapper ();
101+ }
102+ return *this ;
103+ }
104+
105+ template <typename T, typename L>
106+ void
107+ CallbackBridge<T, L>::init_uv(void ) {
63108 uv_mutex_init (&this ->cv_mutex );
64109 uv_cond_init (&this ->condition_variable );
65110 if (!is_sync) {
66111 this ->async = new uv_async_t ;
67112 this ->async ->data = (void *) this ;
68113 uv_async_init (uv_default_loop (), this ->async , (uv_async_cb) dispatched_async_uv_callback);
69114 }
115+ }
70116
117+ template <typename T, typename L>
118+ void
119+ CallbackBridge<T, L>::init_wrapper(void ) {
120+ Nan::HandleScope scope;
71121 v8::Local<v8::Function> func = CallbackBridge<T, L>::get_wrapper_constructor ().ToLocalChecked ();
72122 wrapper.Reset (Nan::NewInstance (func).ToLocalChecked ());
73123 Nan::SetInternalFieldPointer (Nan::New (wrapper), 0 , this );
0 commit comments