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: Created March 17, 2016, 2:04 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,112 @@
+/*
+ * 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);
+ ext.onMessage.addListener(this._onMessage.bind(this));
kzar 2016/03/18 15:22:10 I guess this could be a memory leak, if the port i
Sebastian Noack 2016/03/18 15:26:22 Who cares? This class is used as a singleton.
kzar 2016/03/18 15:52:40 Acknowledged.
+};
+
+Port.prototype = {
+ _onMessage: function(message, sender, sendResponse)
+ {
+ let async = false;
+ let callbacks = this._callbacks[message.type] || [];
+
+ for (let callback of callbacks)
+ {
+ let response = callback(message, sender);
+
+ if (response instanceof Promise)
+ {
+ 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();
« lib/filterComposer.js ('K') | « lib/filterComposer.js ('k') | lib/whitelisting.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld