Skip to content

Commit c413d0b

Browse files
committed
fix: allow unsubscribing multiple inbox change handlers
1 parent bb31bb1 commit c413d0b

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/LeanplumInbox.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ export default class LeanplumInbox implements Inbox {
7777
}
7878

7979
public onChanged(handler: Function): Function {
80-
const idx = this.changeHandlers.push(handler)
81-
return () => this.changeHandlers.splice(idx-1, 1)
80+
this.changeHandlers.push(handler)
81+
return () => this.changeHandlers = this.changeHandlers.filter(x => x !== handler)
8282
}
8383

8484
private triggerChangeHandlers(): void {

test/specs/LeanplumInbox.test.ts

+20
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,26 @@ describe(LeanplumInbox, () => {
8686

8787
expect(handler).toHaveBeenCalledTimes(0)
8888
})
89+
90+
it('unsubscribes the correct handler', () => {
91+
const one = jest.fn()
92+
const two = jest.fn()
93+
const three = jest.fn()
94+
95+
const removeOne = inbox.onChanged(one)
96+
inbox.onChanged(two)
97+
const removeThree = inbox.onChanged(three)
98+
removeOne();
99+
removeThree();
100+
101+
mockMessages({
102+
'123##1': {},
103+
})
104+
105+
expect(one).toHaveBeenCalledTimes(0)
106+
expect(two).toHaveBeenCalledTimes(1)
107+
expect(three).toHaveBeenCalledTimes(0)
108+
})
89109
})
90110

91111
describe('allMessages', () => {

0 commit comments

Comments
 (0)