Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to channel that ignores event from self #1879

Merged
merged 4 commits into from
Nov 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]);
});
});
});