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

Unified Diff: lib/messaging.js

Issue 29338491: Issue 3823 - Split up message responder code (Closed)
Patch Set: Implement getPort() Created March 21, 2016, 3: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
Index: lib/messaging.js
===================================================================
new file mode 100644
--- /dev/null
+++ b/lib/messaging.js
@@ -0,0 +1,133 @@
+/*
+ * This file is part of Adblock Plus <https://adblockplus.org/>,
+ * Copyright (C) 2006-2016 Eyeo GmbH
+ *
+ * Adblock Plus is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * Adblock Plus is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/** @module messaging */
+
+"use strict";
+
+/**
+ * Communication port wrapping ext.onMessage to receive messages.
+ *
+ * @constructor
+ */
+function Port()
+{
+ this._callbacks = Object.create(null);
+ this._onMessage = this._onMessage.bind(this);
+ ext.onMessage.addListener(this._onMessage);
+};
+
+Port.prototype = {
+ _onMessage: function(message, sender, sendResponse)
+ {
+ let async = false;
+ let callbacks = this._callbacks[message.type];
+
+ if (callbacks)
+ {
+ for (let callback of callbacks)
+ {
+ let response = callback(message, sender);
+
+ if (response && typeof response.then == "function")
+ {
+ response.then(
+ sendResponse,
+ reason => {
+ console.error(reason);
+ sendResponse(undefined);
+ }
+ );
+ async = true;
+ }
+ else if (typeof response != "undefined")
+ {
+ sendResponse(response);
+ }
+ }
+ }
+
+ return async;
+ },
+
+ /**
+ * Function to be called when a particular message is received.
+ *
+ * @callback Port~messageCallback
+ * @param {object} message
+ * @param {object} sender
+ * @return The callback can return undefined (no response),
+ * a value (response to be sent to sender immediately)
+ * or a promise (asynchronous response).
+ */
+
+ /**
+ * Adds a callback for the specified message.
+ *
+ * The return value of the callback (if not undefined) is sent as response.
+ * @param {string} name
+ * @param {Port~messageCallback} callback
+ */
+ on: function(name, callback)
+ {
+ if (name in this._callbacks)
+ this._callbacks[name].push(callback);
+ else
+ this._callbacks[name] = [callback];
+ },
+
+ /**
+ * Removes a callback for the specified message.
+ *
+ * @param {string} name
+ * @param {Port~messageCallback} callback
+ */
+ off: function(name, callback)
+ {
+ let callbacks = this._callbacks[name];
+ if (callbacks)
+ {
+ let idx = callbacks.indexOf(callback);
+ if (idx != -1)
+ callbacks.splice(idx, 1);
+ }
+ }
+};
+
+/**
+ * The default port to receive messages.
+ *
+ * @type {Port}
+ */
+exports.port = new Port();
+
+/**
+ * Creates a new port that is shutdown when the given window is unloaded.
+ *
+ * @param {Window} window
+ * @return {Port}
+ */
+exports.getPort = function(window)
+{
+ let port = new Port();
+ window.addEventListener("unload", () =>
+ {
+ ext.onMessage.removeListener(port._onMessage);
+ });
Wladimir Palant 2016/03/21 15:50:13 Please consider caching the port per window so tha
Sebastian Noack 2016/03/21 17:15:42 Done.
+ return port;
+};
+

Powered by Google App Engine
This is Rietveld