Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: ext/common.js

Issue 29410614: Noissue - Make _listeners a Set object (Closed)
Patch Set: Created April 12, 2017, 12:17 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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-2017 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 *
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 "use strict"; 18 "use strict";
19 19
20 (function() 20 (function()
21 { 21 {
22 window.ext = {}; 22 window.ext = {};
23 23
24 let EventTarget = ext._EventTarget = function() 24 let EventTarget = ext._EventTarget = function()
25 { 25 {
26 this._listeners = []; 26 this._listeners = new Set();
27 }; 27 };
28 EventTarget.prototype = { 28 EventTarget.prototype = {
29 addListener(listener) 29 addListener(listener)
30 { 30 {
31 if (this._listeners.indexOf(listener) == -1) 31 this._listeners.add(listener);
32 this._listeners.push(listener);
33 }, 32 },
34 removeListener(listener) 33 removeListener(listener)
35 { 34 {
36 let idx = this._listeners.indexOf(listener); 35 this._listeners.delete(listener);
37 if (idx != -1)
38 this._listeners.splice(idx, 1);
39 }, 36 },
40 _dispatch(...args) 37 _dispatch(...args)
41 { 38 {
42 let results = []; 39 let results = [];
43 let listeners = this._listeners.slice(); 40 let listeners = [...this._listeners];
Sebastian Noack 2017/04/12 12:32:14 Is turning the set into an array even necessary, c
Manish Jethani 2017/04/12 12:37:50 Yeah, I'm trying to be compatible with the current
Sebastian Noack 2017/04/12 12:42:36 I see. This might have been the reason why the lis
Wladimir Palant 2017/04/12 14:43:02 It is. However, is that an efficient way of copyin
Sebastian Noack 2017/04/12 14:49:30 We don't need a set, an array is sufficient. So if
44 41
45 for (let listener of listeners) 42 for (let listener of listeners)
46 results.push(listener(...args)); 43 results.push(listener(...args));
47 44
48 return results; 45 return results;
49 } 46 }
50 }; 47 };
51 48
52 // Workaround since HTMLCollection and NodeList didn't have iterator support 49 // Workaround since HTMLCollection and NodeList didn't have iterator support
53 // before Chrome 51. 50 // before Chrome 51.
(...skipping 18 matching lines...) Expand all
72 return chrome.extension.getBackgroundPage(); 69 return chrome.extension.getBackgroundPage();
73 } 70 }
74 }; 71 };
75 72
76 73
77 /* Utils */ 74 /* Utils */
78 75
79 ext.getURL = chrome.extension.getURL; 76 ext.getURL = chrome.extension.getURL;
80 ext.i18n = chrome.i18n; 77 ext.i18n = chrome.i18n;
81 }()); 78 }());
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld