Skip to content

Commit

Permalink
feat(message-core): add unsubscribe
Browse files Browse the repository at this point in the history
  • Loading branch information
Kasey Powers committed Aug 28, 2018
1 parent 343b09d commit dd6b5eb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/message-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ A package wrapping the postMessage function with helper functions and security c

it returns a function that can be used to unsubscribe from that event

### unsubscribe

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

### enabled

if a value is passed in, sets messaging's enabled flag true/false based on value.
Expand Down
5 changes: 5 additions & 0 deletions packages/message-core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ class AvMessage {
};
}

// remove all subscribers for this event
unsubscribe(event) {
delete this.subscribers[event];
}

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

test('unsusbscribe should remove subscriptions for event', () => {
const event1 = ['a', 'b', 'c'];
const event2 = ['b', 'c', 'd'];
avMessage.subscribers = {
event1,
event2,
};

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

describe('getEventData()', () => {
Expand Down

0 comments on commit dd6b5eb

Please sign in to comment.