OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 Eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 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 | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 13 matching lines...) Expand all Loading... |
24 /** | 24 /** |
25 * Communication port wrapping ext.onMessage to receive messages. | 25 * Communication port wrapping ext.onMessage to receive messages. |
26 * | 26 * |
27 * @constructor | 27 * @constructor |
28 */ | 28 */ |
29 function Port() | 29 function Port() |
30 { | 30 { |
31 this._eventEmitter = new EventEmitter(); | 31 this._eventEmitter = new EventEmitter(); |
32 this._onMessage = this._onMessage.bind(this); | 32 this._onMessage = this._onMessage.bind(this); |
33 ext.onMessage.addListener(this._onMessage); | 33 ext.onMessage.addListener(this._onMessage); |
34 }; | 34 } |
35 | 35 |
36 Port.prototype = { | 36 Port.prototype = { |
37 _onMessage(message, sender, sendResponse) | 37 _onMessage(message, sender, sendResponse) |
38 { | 38 { |
39 let async = false; | 39 let async = false; |
40 let callbacks = this._eventEmitter.listeners(message.type); | 40 let callbacks = this._eventEmitter.listeners(message.type); |
41 | 41 |
42 for (let callback of callbacks) | 42 for (let callback of callbacks) |
43 { | 43 { |
44 let response = callback(message, sender); | 44 let response = callback(message, sender); |
45 | 45 |
46 if (response && typeof response.then == "function") | 46 if (response && typeof response.then == "function") |
47 { | 47 { |
48 response.then( | 48 response.then( |
49 sendResponse, | 49 sendResponse, |
50 reason => { | 50 reason => |
| 51 { |
51 console.error(reason); | 52 console.error(reason); |
52 sendResponse(undefined); | 53 sendResponse(undefined); |
53 } | 54 } |
54 ); | 55 ); |
55 async = true; | 56 async = true; |
56 } | 57 } |
57 else if (typeof response != "undefined") | 58 else if (typeof response != "undefined") |
58 { | |
59 sendResponse(response); | 59 sendResponse(response); |
60 } | |
61 } | 60 } |
62 | 61 |
63 return async; | 62 return async; |
64 }, | 63 }, |
65 | 64 |
66 /** | 65 /** |
67 * Function to be called when a particular message is received. | 66 * Function to be called when a particular message is received. |
68 * | 67 * |
69 * @callback Port~messageCallback | 68 * @callback Port~messageCallback |
70 * @param {object} message | 69 * @param {object} message |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 exports.getPort = function(window) | 121 exports.getPort = function(window) |
123 { | 122 { |
124 let port = new Port(); | 123 let port = new Port(); |
125 window.addEventListener("unload", () => | 124 window.addEventListener("unload", () => |
126 { | 125 { |
127 port.disconnect(); | 126 port.disconnect(); |
128 }); | 127 }); |
129 return port; | 128 return port; |
130 }; | 129 }; |
131 | 130 |
OLD | NEW |