Skip to content

Commit 66e9611

Browse files
committedMay 10, 2021
Tweak Observable to use removeItem, for..of, ??
1 parent 4b1ecbe commit 66e9611

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed
 

‎src/core/data-structures/observable.js

+5-8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17+
import {removeItem} from '../types/array';
18+
1719
/**
1820
* This class helps to manage observers. Observers can be added, removed or
1921
* fired through and instance of this class.
@@ -51,10 +53,7 @@ export class Observable {
5153
if (!this.handlers_) {
5254
return;
5355
}
54-
const index = this.handlers_.indexOf(handler);
55-
if (index > -1) {
56-
this.handlers_.splice(index, 1);
57-
}
56+
removeItem(this.handlers, handler);
5857
}
5958

6059
/**
@@ -75,9 +74,7 @@ export class Observable {
7574
if (!this.handlers_) {
7675
return;
7776
}
78-
const handlers = this.handlers_;
79-
for (let i = 0; i < handlers.length; i++) {
80-
const handler = handlers[i];
77+
for (const handler of this.handlers_) {
8178
handler(opt_event);
8279
}
8380
}
@@ -87,6 +84,6 @@ export class Observable {
8784
* @return {number}
8885
*/
8986
getHandlerCount() {
90-
return this.handlers_?.length || 0;
87+
return this.handlers_?.length ?? 0;
9188
}
9289
}

0 commit comments

Comments
 (0)