Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: lib/messaging.js

Issue 29338534: Issue 3826 - Filter preference change events (Closed)
Patch Set: Rebased and fixed a typo Created March 19, 2016, 7:48 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/events.js ('k') | lib/prefs.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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);
}
};
« no previous file with comments | « lib/events.js ('k') | lib/prefs.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld