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

Side by Side Diff: lib/messaging.js

Issue 29338491: Issue 3823 - Split up message responder code (Closed)
Patch Set: Created March 17, 2016, 2:04 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 Eyeo GmbH
4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 /** @module messaging */
19
20 "use strict";
21
22 /**
23 * Communication port wrapping ext.onMessage to receive messages.
24 *
25 * @constructor
26 */
27 function Port()
28 {
29 this._callbacks = Object.create(null);
30 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.
31 };
32
33 Port.prototype = {
34 _onMessage: function(message, sender, sendResponse)
35 {
36 let async = false;
37 let callbacks = this._callbacks[message.type] || [];
38
39 for (let callback of callbacks)
40 {
41 let response = callback(message, sender);
42
43 if (response instanceof Promise)
44 {
45 response.then(
46 sendResponse,
47 reason => {
48 console.error(reason);
49 sendResponse(undefined);
50 }
51 );
52 async = true;
53 }
54 else if (typeof response != "undefined")
55 {
56 sendResponse(response);
57 }
58 }
59
60 return async;
61 },
62
63 /**
64 * Function to be called when a particular message is received.
65 *
66 * @callback Port~messageCallback
67 * @param {object} message
68 * @param {object} sender
69 * @return The callback can return undefined (no response),
70 * a value (response to be sent to sender immediately)
71 * or a promise (asynchronous response).
72 */
73
74 /**
75 * Adds a callback for the specified message.
76 *
77 * The return value of the callback (if not undefined) is sent as response.
78 * @param {string} name
79 * @param {Port~messageCallback} callback
80 */
81 on: function(name, callback)
82 {
83 if (name in this._callbacks)
84 this._callbacks[name].push(callback);
85 else
86 this._callbacks[name] = [callback];
87 },
88
89 /**
90 * Removes a callback for the specified message.
91 *
92 * @param {string} name
93 * @param {Port~messageCallback} callback
94 */
95 off: function(name, callback)
96 {
97 let callbacks = this._callbacks[name];
98 if (callbacks)
99 {
100 let idx = callbacks.indexOf(callback);
101 if (idx != -1)
102 callbacks.splice(idx, 1);
103 }
104 }
105 };
106
107 /**
108 * The default port to receive messages.
109 *
110 * @type {Port}
111 */
112 exports.port = new Port();
OLDNEW
« 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