Skip to content

Commit 3a66ff8

Browse files
author
Kasey Powers
committed
feat(message-core): unsusbscribeAll fn
1 parent dd6b5eb commit 3a66ff8

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

packages/message-core/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ it returns a function that can be used to unsubscribe from that event
1616

1717
### unsubscribe
1818

19-
`avMessage.unsubscribe(eventName)` will remove all listeners for this event
19+
`avMessage.unsubscribe(eventName)` will remove all listeners for this event.
20+
21+
### unsubscribeAll
22+
23+
`avMessage.unsubscribeAll()` will remove all listeners for all events.
2024

2125
### enabled
2226

packages/message-core/src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ class AvMessage {
6666
delete this.subscribers[event];
6767
}
6868

69+
unsubscribeAll() {
70+
this.subscribers = {};
71+
}
72+
6973
onMessage(event, data) {
7074
if (this.subscribers[event]) {
7175
this.subscribers[event].forEach(fn => {

packages/message-core/src/tests/message.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ describe('AvMessage', () => {
6868
expect(avMessage.subscribers).toEqual({
6969
[testEvent]: [fn2],
7070
});
71+
72+
unsubscribe();
73+
expect(avMessage.subscribers).toEqual({
74+
[testEvent]: [fn2],
75+
});
7176
});
7277

7378
test('unsusbscribe should remove subscriptions for event', () => {
@@ -80,6 +85,18 @@ describe('AvMessage', () => {
8085

8186
avMessage.unsubscribe('event1');
8287
expect(avMessage.subscribers).toEqual({ event2 });
88+
89+
avMessage.unsubscribe();
90+
expect(avMessage.subscribers).toEqual({ event2 });
91+
});
92+
93+
test('unsubscribeAll should remove all subscriptions', () => {
94+
avMessage.subscribers = {
95+
test1: ['a', 'b'],
96+
test2: ['b', 'c'],
97+
};
98+
avMessage.unsubscribeAll();
99+
expect(avMessage.subscribers).toEqual({});
83100
});
84101
});
85102

0 commit comments

Comments
 (0)