Skip to content

Commit bb31bb1

Browse files
committed
fix: allow inbox change handlers to be unsubscribed
1 parent 532b92c commit bb31bb1

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

dist/leanplum.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export interface Inbox {
116116
markAsRead(messageId: string): void;
117117
read(messageId: string): void;
118118
remove(messageId: string): void;
119-
onChanged(handler: Function): void;
119+
onChanged(handler: Function): Function;
120120
count(): number;
121121
unreadCount(): number;
122122
allMessages(): InboxMessage[];

src/LeanplumInbox.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@ export default class LeanplumInbox implements Inbox {
7676
this.createRequest('deleteNewsfeedMessage', args, {})
7777
}
7878

79-
public onChanged(handler: Function): void {
80-
this.changeHandlers.push(handler)
79+
public onChanged(handler: Function): Function {
80+
const idx = this.changeHandlers.push(handler)
81+
return () => this.changeHandlers.splice(idx-1, 1)
8182
}
8283

8384
private triggerChangeHandlers(): void {

test/specs/LeanplumInbox.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,19 @@ describe(LeanplumInbox, () => {
7373
expect(handler).toHaveBeenCalledTimes(0)
7474
expect(inbox.messageIds()).toEqual([])
7575
})
76+
77+
it('allows change handlers to be unsubscribed', () => {
78+
const handler = jest.fn()
79+
80+
const remove = inbox.onChanged(handler)
81+
remove();
82+
83+
mockMessages({
84+
'123##1': {},
85+
})
86+
87+
expect(handler).toHaveBeenCalledTimes(0)
88+
})
7689
})
7790

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

0 commit comments

Comments
 (0)