Skip to content

Commit 8391354

Browse files
committed
Inherit all EventEmitter's functions
And correctly set `this` to the instance of OZW when calling `emit`.
1 parent b27f5f0 commit 8391354

File tree

5 files changed

+32
-49
lines changed

5 files changed

+32
-49
lines changed

lib/openzwave-shared.js

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,10 @@ var EventEmitter = require('events').EventEmitter;
2121
var debugAddon = __dirname + '/../build/Debug/openzwave_shared.node';
2222
var releaseAddon = __dirname + '/../build/Release/openzwave_shared.node';
2323
var addonFileName = ((fs.existsSync(debugAddon)) ? debugAddon : releaseAddon);
24+
var addonModule = require(addonFileName);
2425

25-
console.log("initialising OpenZWave addon ("+addonFileName+")");
26-
var addonModule = require(addonFileName);
27-
28-
/*
29-
* we need a proxy EventEmitter instance because apparently there's
30-
* no (easy?) way to inherit an EventEmitter (JS code) from C++
31-
**/
32-
var ee = new EventEmitter();
33-
34-
addonModule.Emitter.prototype.addListener = function(evt, callback) {
35-
ee.addListener(evt, callback);
36-
return this;
37-
}
38-
addonModule.Emitter.prototype.on = addonModule.Emitter.prototype.addListener;
39-
addonModule.Emitter.prototype.emit = function(evt, arg1, arg2, arg3, arg4) {
40-
return ee.emit(evt, arg1, arg2, arg3, arg4);
41-
}
42-
addonModule.Emitter.prototype.removeListener = function(evt, callback) {
43-
ee.removeListener(evt, callback);
44-
return this;
45-
}
46-
addonModule.Emitter.prototype.removeAllListeners = function(evt) {
47-
ee.removeAllListeners(evt);
48-
return this;
26+
for (var k in EventEmitter.prototype) {
27+
addonModule.Emitter.prototype[k] = EventEmitter.prototype[k];
4928
}
5029

5130
module.exports = addonModule.Emitter;

src/callbacks.cc

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ uv_async_t async;
2828

2929
//
3030
Nan::Callback *emit_cb;
31+
Nan::CopyablePersistentTraits<v8::Object>::CopyablePersistent ctx_obj;
3132

3233
// Message passing queue between OpenZWave callback and v8 async handler.
3334
mutex zqueue_mutex;
@@ -136,7 +137,7 @@ void handleControllerCommand(NotifInfo *notif)
136137
info[2] = Nan::New<Integer>(notif->event); // Driver::ControllerCommand
137138
info[3] = Nan::New<Integer>(notif->notification); // Driver::ControllerCommand
138139
info[4] = Nan::New<String>(notif->help.c_str()).ToLocalChecked();
139-
emit_cb->Call(5, info);
140+
emit_cb->Call(Nan::New(ctx_obj), 5, info);
140141
}
141142
// ##### END OF LEGACY MODE ###### //
142143
#endif
@@ -170,7 +171,7 @@ void handleNotification(NotifInfo *notif)
170171
emitinfo[1] = Nan::New<Integer>(notif->nodeid);
171172
emitinfo[2] = Nan::New<Integer>(value.GetCommandClassId());
172173
emitinfo[3] = valobj;
173-
emit_cb->Call(4, emitinfo);
174+
emit_cb->Call(Nan::New(ctx_obj), 4, emitinfo);
174175
break;
175176
}
176177
// ##################
@@ -191,7 +192,7 @@ void handleNotification(NotifInfo *notif)
191192
emitinfo[2] = Nan::New<Integer>(value.GetCommandClassId());
192193
emitinfo[3] = Nan::New<Integer>(value.GetInstance());
193194
emitinfo[4] = Nan::New<Integer>(value.GetIndex());
194-
emit_cb->Call(5, emitinfo);
195+
emit_cb->Call(Nan::New(ctx_obj), 5, emitinfo);
195196
break;
196197
}
197198
// ##################
@@ -203,7 +204,7 @@ void handleNotification(NotifInfo *notif)
203204
emitinfo[1] = Nan::New<Integer>(notif->nodeid);
204205
emitinfo[2] = Nan::New<Integer>(value.GetCommandClassId());
205206
emitinfo[3] = valobj;
206-
emit_cb->Call(4, emitinfo);
207+
emit_cb->Call(Nan::New(ctx_obj), 4, emitinfo);
207208
break;
208209
}
209210
// ####################
@@ -215,7 +216,7 @@ void handleNotification(NotifInfo *notif)
215216
emitinfo[1] = Nan::New<Integer>(notif->nodeid);
216217
emitinfo[2] = Nan::New<Integer>(value.GetCommandClassId());
217218
emitinfo[3] = valobj;
218-
emit_cb->Call(4, emitinfo);
219+
emit_cb->Call(Nan::New(ctx_obj), 4, emitinfo);
219220
break;
220221
}
221222
// #############
@@ -241,7 +242,7 @@ void handleNotification(NotifInfo *notif)
241242
}
242243
emitinfo[0] = Nan::New<String>("node added").ToLocalChecked();
243244
emitinfo[1] = Nan::New<Integer>(notif->nodeid);
244-
emit_cb->Call(2, emitinfo);
245+
emit_cb->Call(Nan::New(ctx_obj), 2, emitinfo);
245246
break;
246247
}
247248
// #################
@@ -253,7 +254,7 @@ void handleNotification(NotifInfo *notif)
253254
}
254255
emitinfo[0] = Nan::New<String>("node removed").ToLocalChecked();
255256
emitinfo[1] = Nan::New<Integer>(notif->nodeid);
256-
emit_cb->Call(2, emitinfo);
257+
emit_cb->Call(Nan::New(ctx_obj), 2, emitinfo);
257258
break;
258259
}
259260
// ######################
@@ -272,7 +273,7 @@ void handleNotification(NotifInfo *notif)
272273
emitinfo[0] = Nan::New<String>("node naming").ToLocalChecked();
273274
emitinfo[1] = Nan::New<Integer>(notif->nodeid);
274275
emitinfo[2] = cbinfo;
275-
emit_cb->Call(3, emitinfo);
276+
emit_cb->Call(Nan::New(ctx_obj), 3, emitinfo);
276277
break;
277278
}
278279
// ###############
@@ -282,7 +283,7 @@ void handleNotification(NotifInfo *notif)
282283
emitinfo[1] = Nan::New<Integer>(notif->nodeid);
283284
emitinfo[2] = Nan::New<Integer>(notif->event);
284285
emitinfo[3] = Nan::New<String>(notif->help.c_str()).ToLocalChecked();
285-
emit_cb->Call(4, emitinfo);
286+
emit_cb->Call(Nan::New(ctx_obj), 4, emitinfo);
286287
break;
287288
}
288289
// #####################
@@ -292,7 +293,7 @@ void handleNotification(NotifInfo *notif)
292293
node->polled = false;
293294
emitinfo[0] = Nan::New<String>("polling disabled").ToLocalChecked();
294295
emitinfo[1] = Nan::New<Integer>(notif->nodeid);
295-
emit_cb->Call(2, emitinfo);
296+
emit_cb->Call(Nan::New(ctx_obj), 2, emitinfo);
296297
}
297298
break;
298299
}
@@ -303,7 +304,7 @@ void handleNotification(NotifInfo *notif)
303304
node->polled = true;
304305
emitinfo[0] = Nan::New<String>("polling enabled").ToLocalChecked();
305306
emitinfo[1] = Nan::New<Integer>(notif->nodeid);
306-
emit_cb->Call(2, emitinfo);
307+
emit_cb->Call(Nan::New(ctx_obj), 2, emitinfo);
307308
}
308309
break;
309310
}
@@ -313,7 +314,7 @@ void handleNotification(NotifInfo *notif)
313314
emitinfo[0] = Nan::New<String>("scene event").ToLocalChecked();
314315
emitinfo[1] = Nan::New<Integer>(notif->nodeid);
315316
emitinfo[2] = Nan::New<Integer>(notif->sceneid);
316-
emit_cb->Call(3, emitinfo);
317+
emit_cb->Call(Nan::New(ctx_obj), 3, emitinfo);
317318
break;
318319
}
319320
// ##################
@@ -322,7 +323,7 @@ void handleNotification(NotifInfo *notif)
322323
emitinfo[0] = Nan::New<String>("create button").ToLocalChecked();
323324
emitinfo[1] = Nan::New<Integer>(notif->nodeid);
324325
emitinfo[2] = Nan::New<Integer>(notif->buttonid);
325-
emit_cb->Call(3, emitinfo);
326+
emit_cb->Call(Nan::New(ctx_obj), 3, emitinfo);
326327
break;
327328
}
328329
// ##################
@@ -331,7 +332,7 @@ void handleNotification(NotifInfo *notif)
331332
emitinfo[0] = Nan::New<String>("delete button").ToLocalChecked();
332333
emitinfo[1] = Nan::New<Integer>(notif->nodeid);
333334
emitinfo[2] = Nan::New<Integer>(notif->buttonid);
334-
emit_cb->Call(3, emitinfo);
335+
emit_cb->Call(Nan::New(ctx_obj), 3, emitinfo);
335336
break;
336337
}
337338
// ##############
@@ -340,7 +341,7 @@ void handleNotification(NotifInfo *notif)
340341
emitinfo[0] = Nan::New<String>("button on").ToLocalChecked();
341342
emitinfo[1] = Nan::New<Integer>(notif->nodeid);
342343
emitinfo[2] = Nan::New<Integer>(notif->buttonid);
343-
emit_cb->Call(3, emitinfo);
344+
emit_cb->Call(Nan::New(ctx_obj), 3, emitinfo);
344345
break;
345346
}
346347
// ###############
@@ -349,7 +350,7 @@ void handleNotification(NotifInfo *notif)
349350
emitinfo[0] = Nan::New<String>("button off").ToLocalChecked();
350351
emitinfo[1] = Nan::New<Integer>(notif->nodeid);
351352
emitinfo[2] = Nan::New<Integer>(notif->buttonid);
352-
emit_cb->Call(3, emitinfo);
353+
emit_cb->Call(Nan::New(ctx_obj), 3, emitinfo);
353354
break;
354355
}
355356
// #################
@@ -359,14 +360,14 @@ void handleNotification(NotifInfo *notif)
359360
homeid = notif->homeid;
360361
emitinfo[0] = Nan::New<String>("driver ready").ToLocalChecked();
361362
emitinfo[1] = Nan::New<Integer>(homeid);
362-
emit_cb->Call(2, emitinfo);
363+
emit_cb->Call(Nan::New(ctx_obj), 2, emitinfo);
363364
break;
364365
}
365366
// ##################
366367
case OpenZWave::Notification::Type_DriverFailed: {
367368
// ##################
368369
emitinfo[0] = Nan::New<String>("driver failed").ToLocalChecked();
369-
emit_cb->Call(1, emitinfo);
370+
emit_cb->Call(Nan::New(ctx_obj), 1, emitinfo);
370371
break;
371372
}
372373
// ##################################
@@ -376,7 +377,7 @@ void handleNotification(NotifInfo *notif)
376377
emitinfo[0] = Nan::New<String>("node available").ToLocalChecked();
377378
emitinfo[1] = Nan::New<Integer>(notif->nodeid);
378379
emitinfo[2] = cbinfo;
379-
emit_cb->Call(3, emitinfo);
380+
emit_cb->Call(Nan::New(ctx_obj), 3, emitinfo);
380381
break;
381382
}
382383
/*
@@ -389,7 +390,7 @@ void handleNotification(NotifInfo *notif)
389390
emitinfo[0] = Nan::New<String>("node ready").ToLocalChecked();
390391
emitinfo[1] = Nan::New<Integer>(notif->nodeid);
391392
emitinfo[2] = cbinfo;
392-
emit_cb->Call(3, emitinfo);
393+
emit_cb->Call(Nan::New(ctx_obj), 3, emitinfo);
393394
break;
394395
}
395396
/*
@@ -402,7 +403,7 @@ void handleNotification(NotifInfo *notif)
402403
case OpenZWave::Notification::Type_AllNodesQueriedSomeDead: {
403404
// #############################
404405
emitinfo[0] = Nan::New<String>("scan complete").ToLocalChecked();
405-
emit_cb->Call(1, emitinfo);
406+
emit_cb->Call(Nan::New(ctx_obj), 1, emitinfo);
406407
break;
407408
}
408409
// ##################
@@ -412,7 +413,7 @@ void handleNotification(NotifInfo *notif)
412413
emitinfo[1] = Nan::New<Integer>(notif->nodeid);
413414
emitinfo[2] = Nan::New<Integer>(notif->notification);
414415
emitinfo[3] = Nan::New<String>(notif->help.c_str()).ToLocalChecked();
415-
emit_cb->Call(4, emitinfo);
416+
emit_cb->Call(Nan::New(ctx_obj), 4, emitinfo);
416417
break;
417418
}
418419
case OpenZWave::Notification::Type_DriverRemoved:
@@ -431,15 +432,15 @@ void handleNotification(NotifInfo *notif)
431432
emitinfo[3] =
432433
Nan::New<Integer>(notif->notification); // Driver::ControllerState
433434
emitinfo[4] = Nan::New<String>(notif->help.c_str()).ToLocalChecked();
434-
emit_cb->Call(5, emitinfo);
435+
emit_cb->Call(Nan::New(ctx_obj), 5, emitinfo);
435436
break;
436437
case OpenZWave::Notification::Type_NodeReset:
437438
emitinfo[0] = Nan::New<String>("node reset").ToLocalChecked();
438439
emitinfo[1] = Nan::New<Integer>(notif->nodeid);
439440
emitinfo[2] = Nan::New<Integer>(notif->event); // Driver::ControllerCommand
440441
emitinfo[3] =
441442
Nan::New<Integer>(notif->notification); // Driver::ControllerState
442-
emit_cb->Call(4, emitinfo);
443+
emit_cb->Call(Nan::New(ctx_obj), 4, emitinfo);
443444
break;
444445
#endif
445446
default:

src/callbacks.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ namespace OZW {
6161

6262
//extern Handle<Object> context_obj;
6363
extern Nan::Callback *emit_cb;
64+
extern Nan::CopyablePersistentTraits<v8::Object>::CopyablePersistent ctx_obj;
6465

6566
/*
6667
* uv_async to let the OpenZWave callback wake up the main V8 thread

src/openzwave-driver.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ namespace OZW {
5555
cbinfo[0] = Nan::New<String>("connected").ToLocalChecked();
5656
cbinfo[1] = Nan::New<String>(version).ToLocalChecked();
5757

58-
emit_cb->Call(2, cbinfo);
58+
emit_cb->Call(Nan::New(ctx_obj), 2, cbinfo);
5959
}
6060

6161
// ===================================================================

src/openzwave.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,8 @@ namespace OZW {
249249
self->userpath = ozw_userpath;
250250
self->option_overrides = option_overrides;
251251

252+
ctx_obj = Nan::Persistent<Object>(info.This());
253+
252254
//
253255
info.GetReturnValue().Set(info.This());
254256
}

0 commit comments

Comments
 (0)