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-2017 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 |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
(...skipping 10 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 { |
59 sendResponse(response); | 60 sendResponse(response); |
60 } | 61 } |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 exports.getPort = function(window) | 123 exports.getPort = function(window) |
123 { | 124 { |
124 let port = new Port(); | 125 let port = new Port(); |
125 window.addEventListener("unload", () => | 126 window.addEventListener("unload", () => |
126 { | 127 { |
127 port.disconnect(); | 128 port.disconnect(); |
128 }); | 129 }); |
129 return port; | 130 return port; |
130 }; | 131 }; |
131 | 132 |
OLD | NEW |