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

Delta Between Two Patch Sets: lib/events.js

Issue 29977555: Issue 7202 - Add hasListeners method to EventEmitter (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Add hasListeners method Created Jan. 9, 2019, 3:57 p.m.
Right Patch Set: Remove checks from on() and off() Created Jan. 10, 2019, 4 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | test/filterNotifier.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 } 90 }
91 91
92 /** 92 /**
93 * Returns a copy of the array of listeners for the specified event. 93 * Returns a copy of the array of listeners for the specified event.
94 * 94 *
95 * @param {string} name 95 * @param {string} name
96 * @returns {Array.<function>} 96 * @returns {Array.<function>}
97 */ 97 */
98 listeners(name) 98 listeners(name)
99 { 99 {
100 let listeners = this._listeners.get(name); 100 let listeners = this._listeners.size > 0 ? this._listeners.get(name) : null;
101 return listeners ? listeners.slice() : []; 101 return listeners ? listeners.slice() : [];
102 } 102 }
103 103
104 /** 104 /**
105 * Checks whether there are any listeners for the specified event. 105 * Checks whether there are any listeners for the specified event.
106 * 106 *
107 * @param {string} [name] The name of the event. If omitted, checks whether 107 * @param {string} [name] The name of the event. If omitted, checks whether
108 * there are any listeners for any event. 108 * there are any listeners for any event.
109 * @returns {boolean} 109 * @returns {boolean}
110 */ 110 */
111 hasListeners(name) 111 hasListeners(name)
112 { 112 {
113 return this._listeners.size > 0 && 113 return this._listeners.size > 0 &&
114 (typeof name == "undefined" || this._listeners.has(name)); 114 (typeof name == "undefined" || this._listeners.has(name));
115 } 115 }
116 116
117 /** 117 /**
118 * Calls all previously added listeners for the given event name. 118 * Calls all previously added listeners for the given event name.
119 * 119 *
120 * @param {string} name 120 * @param {string} name
121 * @param {...*} [args] 121 * @param {...*} [args]
122 */ 122 */
123 emit(name, ...args) 123 emit(name, ...args)
124 { 124 {
125 let listeners = this.listeners(name); 125 let listeners = this._listeners.size > 0 ? this._listeners.get(name) : null;
126 for (let listener of listeners) 126 if (listeners)
127 listener(...args); 127 {
128 for (let listener of listeners.slice())
129 listener(...args);
130 }
128 } 131 }
129 } 132 }
130 133
131 exports.EventEmitter = EventEmitter; 134 exports.EventEmitter = EventEmitter;
LEFTRIGHT

Powered by Google App Engine
This is Rietveld