LEFT | RIGHT |
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 11 matching lines...) Expand all Loading... |
22 let {EventEmitter} = require("events"); | 22 let {EventEmitter} = require("events"); |
23 | 23 |
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 ext.onMessage.addListener(this._onMessage.bind(this)); | 32 this._onMessage = this._onMessage.bind(this); |
| 33 ext.onMessage.addListener(this._onMessage); |
33 }; | 34 }; |
34 | 35 |
35 Port.prototype = { | 36 Port.prototype = { |
36 _onMessage: function(message, sender, sendResponse) | 37 _onMessage: function(message, sender, sendResponse) |
37 { | 38 { |
38 let async = false; | 39 let async = false; |
39 let callbacks = this._eventEmitter._callbacks[message.type] || []; | 40 let callbacks = this._eventEmitter._listeners[message.type]; |
40 | 41 |
41 for (let callback of callbacks) | 42 if (callbacks) |
42 { | 43 { |
43 let response = callback(message, sender); | 44 for (let callback of callbacks) |
| 45 { |
| 46 let response = callback(message, sender); |
44 | 47 |
45 if (response instanceof Promise) | 48 if (response && typeof response.then == "function") |
46 { | 49 { |
47 response.then( | 50 response.then( |
48 sendResponse, | 51 sendResponse, |
49 reason => { | 52 reason => { |
50 console.error(reason); | 53 console.error(reason); |
51 sendResponse(undefined); | 54 sendResponse(undefined); |
52 } | 55 } |
53 ); | 56 ); |
54 async = true; | 57 async = true; |
55 } | 58 } |
56 else if (typeof response != "undefined") | 59 else if (typeof response != "undefined") |
57 { | 60 { |
58 sendResponse(response); | 61 sendResponse(response); |
| 62 } |
59 } | 63 } |
60 } | 64 } |
61 | 65 |
62 return async; | 66 return async; |
63 }, | 67 }, |
64 | 68 |
65 /** | 69 /** |
66 * Function to be called when a particular message is received. | 70 * Function to be called when a particular message is received. |
67 * | 71 * |
68 * @callback Port~messageCallback | 72 * @callback Port~messageCallback |
(...skipping 18 matching lines...) Expand all Loading... |
87 | 91 |
88 /** | 92 /** |
89 * Removes a callback for the specified message. | 93 * Removes a callback for the specified message. |
90 * | 94 * |
91 * @param {string} name | 95 * @param {string} name |
92 * @param {Port~messageCallback} callback | 96 * @param {Port~messageCallback} callback |
93 */ | 97 */ |
94 off: function(name, callback) | 98 off: function(name, callback) |
95 { | 99 { |
96 this._eventEmitter.off(name, callback); | 100 this._eventEmitter.off(name, callback); |
| 101 }, |
| 102 |
| 103 /** |
| 104 * Disables the port and makes it stop listening to incoming messages. |
| 105 */ |
| 106 disconnect: function() |
| 107 { |
| 108 ext.onMessage.removeListener(this._onMessage); |
97 } | 109 } |
98 }; | 110 }; |
99 | 111 |
100 /** | 112 /** |
101 * The default port to receive messages. | 113 * The default port to receive messages. |
102 * | 114 * |
103 * @type {Port} | 115 * @type {Port} |
104 */ | 116 */ |
105 exports.port = new Port(); | 117 exports.port = new Port(); |
| 118 |
| 119 /** |
| 120 * Creates a new port that is disconnected when the given window is unloaded. |
| 121 * |
| 122 * @param {Window} window |
| 123 * @return {Port} |
| 124 */ |
| 125 exports.getPort = function(window) |
| 126 { |
| 127 let port = new Port(); |
| 128 window.addEventListener("unload", () => |
| 129 { |
| 130 port.disconnect(); |
| 131 }); |
| 132 return port; |
| 133 }; |
| 134 |
LEFT | RIGHT |