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-present eyeo GmbH | 3 * Copyright (C) 2006-present 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 * |
14 * You should have received a copy of the GNU General Public License | 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/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
| 18 /* global defineNamespace */ |
| 19 |
18 "use strict"; | 20 "use strict"; |
19 | 21 |
| 22 const internal = Symbol(); |
| 23 |
20 { | 24 { |
21 window.ext = {}; | 25 window.ext = {}; |
22 | 26 |
23 let EventTarget = ext._EventTarget = function() | 27 defineNamespace(ext, internal); |
| 28 |
| 29 let EventTarget = ext[internal].EventTarget = function() |
24 { | 30 { |
25 this._listeners = new Set(); | 31 defineNamespace(this, internal); |
| 32 |
| 33 this[internal].listeners = new Set(); |
26 }; | 34 }; |
27 EventTarget.prototype = { | 35 EventTarget.prototype = { |
28 addListener(listener) | 36 addListener(listener) |
29 { | 37 { |
30 this._listeners.add(listener); | 38 this[internal].listeners.add(listener); |
31 }, | 39 }, |
32 removeListener(listener) | 40 removeListener(listener) |
33 { | 41 { |
34 this._listeners.delete(listener); | 42 this[internal].listeners.delete(listener); |
35 }, | |
36 _dispatch(...args) | |
37 { | |
38 let results = []; | |
39 | |
40 for (let listener of this._listeners) | |
41 results.push(listener(...args)); | |
42 | |
43 return results; | |
44 } | 43 } |
45 }; | 44 }; |
46 | 45 |
| 46 ext[internal].dispatchEvent = (eventTarget, ...args) => |
| 47 [...eventTarget[internal].listeners].map( |
| 48 listener => listener(...args) |
| 49 ); |
| 50 |
47 | 51 |
48 /* Message passing */ | 52 /* Message passing */ |
49 | 53 |
50 ext.onMessage = new ext._EventTarget(); | 54 ext.onMessage = new ext[internal].EventTarget(); |
51 } | 55 } |
OLD | NEW |