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

Unified Diff: background.js

Issue 29370996: Issue 4783 - Use Port API for the messageResponder (Closed)
Patch Set: Use EventEmitter Created Jan. 16, 2017, 2:25 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 | « no previous file | messageResponder.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: background.js
diff --git a/background.js b/background.js
index fa78123054da5bb29352923c3496ec8a049eecde..59e1a13b93b4bdd5a3f92c58c0f8354c1cc18e3a 100644
--- a/background.js
+++ b/background.js
@@ -337,6 +337,58 @@
reinitialized: params.filterlistsReinitialized
};
+ modules.messaging = {
+ port: {
+ _eventEmitter: new EventEmitter(),
Sebastian Noack 2017/01/16 15:00:03 Couldn't you do it like that: modules.messaging
kzar 2017/01/16 15:16:31 Done.
+ on(name, callback) { this._eventEmitter.on(name, callback); },
+ off(name, callback) { this._eventEmitter.off(name, callback); },
+ _onMessage(event)
+ {
+ if (event.data.type != "message")
+ return;
+ let message = event.data.payload;
+ let messageId = event.data.messageId;
+ let sender = {
+ page: new ext.Page(event.source)
+ };
+
+ function reply(message)
+ {
+ event.source.postMessage({
+ type: "response",
+ messageId: messageId,
+ payload: message
+ }, "*");
+ }
+
+ let port = modules.messaging.port;
+ let listeners = port._eventEmitter._listeners[message.type];
+ if (!listeners)
+ return;
+
+ for (let listener of listeners)
+ {
+ let response = listener(message, sender);
+ if (response && typeof response.then == "function")
+ {
+ response.then(
+ reply,
+ reason => {
+ console.error(reason);
+ reply(undefined);
+ }
+ );
+ }
+ else if (typeof response != "undefined")
+ {
+ reply(response);
+ }
+ }
+ }
+ }
+ };
+ window.addEventListener("message", modules.messaging.port._onMessage);
+
global.Services = {
vc: {
compare: function(v1, v2)
« no previous file with comments | « no previous file | messageResponder.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld