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: Improve messaging implementation in background.js Created Jan. 16, 2017, 3:15 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..0056a1f4465a8a92834beeeb466bff48e193b0f9 100644
--- a/background.js
+++ b/background.js
@@ -337,6 +337,53 @@
reinitialized: params.filterlistsReinitialized
};
+ modules.messaging = {
+ port: new EventEmitter()
+ };
+
+ window.addEventListener("message", 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)
+ };
+
+ let listeners = modules.messaging.port._listeners[message.type];
+ if (!listeners)
+ return;
+
+ function reply(message)
+ {
+ event.source.postMessage({
+ type: "response",
+ messageId: messageId,
+ payload: message
+ }, "*");
+ }
+
+ 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);
+ }
+ }
+ });
+
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