Skip to content

Commit

Permalink
Merge pull request #1879 from storybooks/channel-onPeer
Browse files Browse the repository at this point in the history
Add method to channel that ignores event from self
  • Loading branch information
Hypnosphi committed Nov 15, 2017
2 parents 5c34ccf + 6e73d4a commit 5efd97e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/channels/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Channels are used with Storybook implementations to send/receive events between
```js
class Channel {
addListener(type, listener) {}
addPeerListener(type, listener) {} // ignore events from itself
emit(type, ...args) {}
eventNames() {}
listenerCount(type) {}
Expand Down
8 changes: 7 additions & 1 deletion lib/channels/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ export default class Channel {
this.on(type, listener);
}

addPeerListener(type, listener) {
const peerListener = listener;
peerListener.isPeer = from => from === this._sender;
this.on(type, peerListener);
}

emit(type, ...args) {
const event = { type, args, from: this._sender };
this._transport.send(event);
Expand Down Expand Up @@ -75,7 +81,7 @@ export default class Channel {
_handleEvent(event) {
const listeners = this._listeners[event.type];
if (listeners) {
listeners.forEach(fn => fn(...event.args));
listeners.forEach(fn => !(fn.isPeer && fn.isPeer(event.from)) && fn(...event.args));
}
}

Expand Down
27 changes: 27 additions & 0 deletions lib/channels/src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,25 @@ describe('Channel', () => {
});
});


describe('method:addPeerListener', () => {
it('should add event listeners', () => {
channel.addPeerListener('type-1', () => {});
channel.addPeerListener('type-2', () => {});
channel.addPeerListener('type-2', () => {});
expect(channel._listeners['type-1'].length).toEqual(1);
expect(channel._listeners['type-2'].length).toEqual(2);
});

it('should call event listeners on event', () => {
const received = [];
channel.addPeerListener('type-1', n => received.push(n));
channel._handleEvent({ type: 'type-1', args: [11] });
channel._handleEvent({ type: 'type-1', args: [12] });
expect(received).toEqual([11, 12]);
});
});

describe('method:prependListener', () => {
it('should add event listeners', () => {
channel.prependListener('type-1', 11);
Expand Down Expand Up @@ -184,5 +203,13 @@ describe('Channel', () => {
channel._handleEvent({ type: 'type-1', args: [12], from: channel._sender });
expect(received).toEqual([11, 12]);
});

it('should ignore if event handled by addPeerListener', () => {
const received = [];
channel.addPeerListener('type-1', n => received.push(n));
channel._handleEvent({ type: 'type-1', args: [11], from: channel._sender });
channel._handleEvent({ type: 'type-1', args: [12], from: '_'});
expect(received).toEqual([12]);
});
});
});

0 comments on commit 5efd97e

Please sign in to comment.