Skip to content

Commit 02a41a6

Browse files
aidantgr2m
authored andcommitted
fix: removeListener (#95)
1 parent 27bdc45 commit 02a41a6

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

event-handler/remove-listener.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function receiverListener (state, webhookNameOrNames, handler) {
1414

1515
// remove last hook that has been added, that way
1616
// it behaves the same as removeListener
17-
for (let i = state.hooks[webhookNameOrNames].length; i > 0; i--) {
17+
for (let i = state.hooks[webhookNameOrNames].length - 1; i >= 0; i--) {
1818
if (state.hooks[webhookNameOrNames][i] === handler) {
1919
state.hooks[webhookNameOrNames].splice(i, 1)
2020
return
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const test = require('tap').test
2+
3+
const removeListener = require('../../event-handler/remove-listener')
4+
5+
test('remove-listener: single listener', t => {
6+
const push = () => {}
7+
8+
const state = {
9+
hooks: {
10+
push: [push]
11+
}
12+
}
13+
14+
t.doesNotThrow(() => {
15+
removeListener(state, 'push', push)
16+
})
17+
t.deepEqual(state, { hooks: { push: [] } })
18+
t.end()
19+
})
20+
21+
test('remove-listener: multiple listeners', t => {
22+
const push1 = () => {}
23+
const push2 = () => {}
24+
const push3 = () => {}
25+
26+
const ping = () => {}
27+
28+
const state = {
29+
hooks: {
30+
push: [push1, push2, push3],
31+
ping: [ping]
32+
}
33+
}
34+
35+
t.doesNotThrow(() => {
36+
removeListener(state, 'push', push1)
37+
removeListener(state, 'push', push2)
38+
removeListener(state, 'push', push3)
39+
})
40+
t.deepEqual(state, { hooks: { push: [], ping: [ping] } })
41+
t.end()
42+
})

0 commit comments

Comments
 (0)