Index: lib/messaging.js |
=================================================================== |
--- a/lib/messaging.js |
+++ b/lib/messaging.js |
@@ -19,6 +19,8 @@ |
"use strict"; |
+let {EventEmitter} = require("events"); |
+ |
/** |
* Communication port wrapping ext.onMessage to receive messages. |
* |
@@ -26,7 +28,7 @@ |
*/ |
function Port() |
{ |
- this._callbacks = Object.create(null); |
+ this._eventEmitter = new EventEmitter(); |
this._onMessage = this._onMessage.bind(this); |
ext.onMessage.addListener(this._onMessage); |
}; |
@@ -35,7 +37,7 @@ |
_onMessage: function(message, sender, sendResponse) |
{ |
let async = false; |
- let callbacks = this._callbacks[message.type]; |
+ let callbacks = this._eventEmitter._callbacks[message.type]; |
if (callbacks) |
{ |
@@ -84,10 +86,7 @@ |
*/ |
on: function(name, callback) |
{ |
- if (name in this._callbacks) |
- this._callbacks[name].push(callback); |
- else |
- this._callbacks[name] = [callback]; |
+ this._eventEmitter.on(name, callback); |
}, |
/** |
@@ -98,13 +97,7 @@ |
*/ |
off: function(name, callback) |
{ |
- let callbacks = this._callbacks[name]; |
- if (callbacks) |
- { |
- let idx = callbacks.indexOf(callback); |
- if (idx != -1) |
- callbacks.splice(idx, 1); |
- } |
+ this._eventEmitter.off(name, callback); |
}, |
/** |