diff --git a/packages/message-core/README.md b/packages/message-core/README.md index eb2576f37..3ec2bad0e 100644 --- a/packages/message-core/README.md +++ b/packages/message-core/README.md @@ -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 diff --git a/packages/message-core/src/index.js b/packages/message-core/src/index.js index 069557c05..6c8983753 100644 --- a/packages/message-core/src/index.js +++ b/packages/message-core/src/index.js @@ -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 => { diff --git a/packages/message-core/src/tests/message.test.js b/packages/message-core/src/tests/message.test.js index 9b324111d..a9698fb04 100644 --- a/packages/message-core/src/tests/message.test.js +++ b/packages/message-core/src/tests/message.test.js @@ -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', () => { @@ -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({}); }); });