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 = { |
| 36 [internal]: { |
| 37 dispatch(...args) |
| 38 { |
| 39 return [...this[internal].listeners].map(listener => listener(...args)); |
| 40 } |
| 41 }, |
28 addListener(listener) | 42 addListener(listener) |
29 { | 43 { |
30 this._listeners.add(listener); | 44 this[internal].listeners.add(listener); |
31 }, | 45 }, |
32 removeListener(listener) | 46 removeListener(listener) |
33 { | 47 { |
34 this._listeners.delete(listener); | 48 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 } | 49 } |
45 }; | 50 }; |
46 | 51 |
47 | 52 |
48 /* Message passing */ | 53 /* Message passing */ |
49 | 54 |
50 ext.onMessage = new ext._EventTarget(); | 55 ext.onMessage = new ext[internal].EventTarget(); |
51 } | 56 } |
OLD | NEW |