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: Implement getPort() Created March 21, 2016, 3:25 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 this._onMessage = this._onMessage.bind(this);
31 ext.onMessage.addListener(this._onMessage);
32 };
33
34 Port.prototype = {
35 _onMessage: function(message, sender, sendResponse)
36 {
37 let async = false;
38 let callbacks = this._callbacks[message.type];
39
40 if (callbacks)
41 {
42 for (let callback of callbacks)
43 {
44 let response = callback(message, sender);
45
46 if (response && typeof response.then == "function")
47 {
48 response.then(
49 sendResponse,
50 reason => {
51 console.error(reason);
52 sendResponse(undefined);
53 }
54 );
55 async = true;
56 }
57 else if (typeof response != "undefined")
58 {
59 sendResponse(response);
60 }
61 }
62 }
63
64 return async;
65 },
66
67 /**
68 * Function to be called when a particular message is received.
69 *
70 * @callback Port~messageCallback
71 * @param {object} message
72 * @param {object} sender
73 * @return The callback can return undefined (no response),
74 * a value (response to be sent to sender immediately)
75 * or a promise (asynchronous response).
76 */
77
78 /**
79 * Adds a callback for the specified message.
80 *
81 * The return value of the callback (if not undefined) is sent as response.
82 * @param {string} name
83 * @param {Port~messageCallback} callback
84 */
85 on: function(name, callback)
86 {
87 if (name in this._callbacks)
88 this._callbacks[name].push(callback);
89 else
90 this._callbacks[name] = [callback];
91 },
92
93 /**
94 * Removes a callback for the specified message.
95 *
96 * @param {string} name
97 * @param {Port~messageCallback} callback
98 */
99 off: function(name, callback)
100 {
101 let callbacks = this._callbacks[name];
102 if (callbacks)
103 {
104 let idx = callbacks.indexOf(callback);
105 if (idx != -1)
106 callbacks.splice(idx, 1);
107 }
108 }
109 };
110
111 /**
112 * The default port to receive messages.
113 *
114 * @type {Port}
115 */
116 exports.port = new Port();
117
118 /**
119 * Creates a new port that is shutdown when the given window is unloaded.
120 *
121 * @param {Window} window
122 * @return {Port}
123 */
124 exports.getPort = function(window)
125 {
126 let port = new Port();
127 window.addEventListener("unload", () =>
128 {
129 ext.onMessage.removeListener(port._onMessage);
130 });
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.
131 return port;
132 };
133
OLDNEW

Powered by Google App Engine
This is Rietveld