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(); |
ext.onMessage.addListener(this._onMessage.bind(this)); |
}; |
@@ -34,7 +36,7 @@ |
_onMessage: function(message, sender, sendResponse) |
{ |
let async = false; |
- let callbacks = this._callbacks[message.type] || []; |
+ let callbacks = this._eventEmitter._callbacks[message.type] || []; |
for (let callback of callbacks) |
{ |
@@ -80,10 +82,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); |
}, |
/** |
@@ -94,13 +93,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); |
} |
}; |