Skip to content

Commit

Permalink
feat(message-core): unsusbscribeAll fn
Browse files Browse the repository at this point in the history
  • Loading branch information
Kasey Powers committed Aug 28, 2018
1 parent dd6b5eb commit 3a66ff8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/message-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ it returns a function that can be used to unsubscribe from that event

### unsubscribe

`avMessage.unsubscribe(eventName)` will remove all listeners for this event
`avMessage.unsubscribe(eventName)` will remove all listeners for this event.

### unsubscribeAll

`avMessage.unsubscribeAll()` will remove all listeners for all events.

### enabled

Expand Down
4 changes: 4 additions & 0 deletions packages/message-core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ class AvMessage {
delete this.subscribers[event];
}

unsubscribeAll() {
this.subscribers = {};
}

onMessage(event, data) {
if (this.subscribers[event]) {
this.subscribers[event].forEach(fn => {
Expand Down
17 changes: 17 additions & 0 deletions packages/message-core/src/tests/message.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ describe('AvMessage', () => {
expect(avMessage.subscribers).toEqual({
[testEvent]: [fn2],
});

unsubscribe();
expect(avMessage.subscribers).toEqual({
[testEvent]: [fn2],
});
});

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

avMessage.unsubscribe('event1');
expect(avMessage.subscribers).toEqual({ event2 });

avMessage.unsubscribe();
expect(avMessage.subscribers).toEqual({ event2 });
});

test('unsubscribeAll should remove all subscriptions', () => {
avMessage.subscribers = {
test1: ['a', 'b'],
test2: ['b', 'c'],
};
avMessage.unsubscribeAll();
expect(avMessage.subscribers).toEqual({});
});
});

Expand Down

0 comments on commit 3a66ff8

Please sign in to comment.