Skip to content

Commit 12e4e65

Browse files
committed
refactor(event-bus): simplify the for loop in the off() method
1 parent 27e3f99 commit 12e4e65

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

src/event-bus.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,10 @@ class HYEventBus {
6565

6666
const handlers = this.eventBus[eventName]
6767
if (handlers && eventCallback) {
68-
const newHandlers = [...handlers]
69-
for (let i = 0; i < newHandlers.length; i++) {
70-
const handler = newHandlers[i]
68+
for (let i = handlers.length - 1; i >= 0; i--) {
69+
const handler = handlers[i]
7170
if (handler.eventCallback === eventCallback) {
72-
const index = handlers.indexOf(handler)
73-
handlers.splice(index, 1)
71+
handlers.splice(i, 1)
7472
}
7573
}
7674
}

0 commit comments

Comments
 (0)