@storybook/channels
Advanced tools
Comparing version 3.3.0-alpha.3 to 3.3.0-alpha.4
@@ -44,2 +44,13 @@ "use strict"; | ||
}, { | ||
key: "addPeerListener", | ||
value: function addPeerListener(type, listener) { | ||
var _this = this; | ||
var peerListener = listener; | ||
peerListener.isPeer = function (from) { | ||
return from === _this._sender; | ||
}; | ||
this.on(type, peerListener); | ||
} | ||
}, { | ||
key: "emit", | ||
@@ -125,3 +136,3 @@ value: function emit(type) { | ||
listeners.forEach(function (fn) { | ||
return fn.apply(undefined, (0, _toConsumableArray3.default)(event.args)); | ||
return !(fn.isPeer && fn.isPeer(event.from)) && fn.apply(undefined, (0, _toConsumableArray3.default)(event.args)); | ||
}); | ||
@@ -133,6 +144,6 @@ } | ||
value: function _onceListener(type, listener) { | ||
var _this = this; | ||
var _this2 = this; | ||
var onceListener = function onceListener() { | ||
_this.removeListener(type, onceListener); | ||
_this2.removeListener(type, onceListener); | ||
return listener.apply(undefined, arguments); | ||
@@ -139,0 +150,0 @@ }; |
{ | ||
"name": "@storybook/channels", | ||
"version": "3.3.0-alpha.3", | ||
"version": "3.3.0-alpha.4", | ||
"description": "", | ||
@@ -9,6 +9,3 @@ "license": "MIT", | ||
"prepare": "node ../../scripts/prepare.js" | ||
}, | ||
"devDependencies": { | ||
"shelljs": "^0.7.8" | ||
} | ||
} |
@@ -18,2 +18,3 @@ # Storybook Channel | ||
addListener(type, listener) {} | ||
addPeerListener(type, listener) {} // ignore events from itself | ||
emit(type, ...args) {} | ||
@@ -20,0 +21,0 @@ eventNames() {} |
@@ -15,2 +15,8 @@ /* eslint no-underscore-dangle: 0 */ | ||
addPeerListener(type, listener) { | ||
const peerListener = listener; | ||
peerListener.isPeer = from => from === this._sender; | ||
this.on(type, peerListener); | ||
} | ||
emit(type, ...args) { | ||
@@ -79,3 +85,3 @@ const event = { type, args, from: this._sender }; | ||
if (listeners) { | ||
listeners.forEach(fn => fn(...event.args)); | ||
listeners.forEach(fn => !(fn.isPeer && fn.isPeer(event.from)) && fn(...event.args)); | ||
} | ||
@@ -82,0 +88,0 @@ } |
@@ -103,4 +103,4 @@ /* eslint no-underscore-dangle: 0 */ | ||
channel.once('type-2', 22); | ||
expect(channel._listeners['type-1'].length).toEqual(1); | ||
expect(channel._listeners['type-2'].length).toEqual(2); | ||
expect(channel._listeners['type-1']).toHaveLength(1); | ||
expect(channel._listeners['type-2']).toHaveLength(2); | ||
}); | ||
@@ -117,2 +117,20 @@ | ||
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']).toHaveLength(1); | ||
expect(channel._listeners['type-2']).toHaveLength(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', () => { | ||
@@ -136,4 +154,4 @@ it('should add event listeners', () => { | ||
channel.prependOnceListener('type-2', 22); | ||
expect(channel._listeners['type-1'].length).toEqual(1); | ||
expect(channel._listeners['type-2'].length).toEqual(2); | ||
expect(channel._listeners['type-1']).toHaveLength(1); | ||
expect(channel._listeners['type-2']).toHaveLength(2); | ||
}); | ||
@@ -190,3 +208,11 @@ | ||
}); | ||
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]); | ||
}); | ||
}); | ||
}); |
17158
0
402
52