@storybook/channels
Advanced tools
Comparing version 4.0.0-alpha.24 to 4.0.0-alpha.25
@@ -20,14 +20,21 @@ "use strict"; | ||
function () { | ||
function Channel(_ref) { | ||
function Channel() { | ||
var _this = this; | ||
var transport = _ref.transport; | ||
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, | ||
transport = _ref.transport, | ||
async = _ref.async; | ||
(0, _classCallCheck2.default)(this, Channel); | ||
this._sender = this._randomId(); | ||
this._transport = transport; | ||
this._async = async; | ||
this._transport.setHandler(function (event) { | ||
return _this._handleEvent(event); | ||
}); | ||
if (transport) { | ||
this._transport = transport; | ||
this._transport.setHandler(function (event) { | ||
return _this._handleEvent(event); | ||
}); | ||
} | ||
this._listeners = {}; | ||
@@ -51,2 +58,4 @@ } | ||
value: function emit(type) { | ||
var _this2 = this; | ||
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
@@ -62,5 +71,15 @@ args[_key - 1] = arguments[_key]; | ||
this._transport.send(event); | ||
var handler = function handler() { | ||
if (_this2._transport) { | ||
_this2._transport.send(event); | ||
} | ||
this._handleEvent(event, true); | ||
_this2._handleEvent(event, true); | ||
}; | ||
if (this._async) { | ||
setImmediate(handler); | ||
} else { | ||
handler(); | ||
} | ||
} | ||
@@ -151,6 +170,6 @@ }, { | ||
value: function _onceListener(type, listener) { | ||
var _this2 = this; | ||
var _this3 = this; | ||
var onceListener = function onceListener() { | ||
_this2.removeListener(type, onceListener); | ||
_this3.removeListener(type, onceListener); | ||
@@ -157,0 +176,0 @@ return listener.apply(void 0, arguments); |
@@ -10,2 +10,3 @@ "use strict"; | ||
/* eslint no-underscore-dangle: 0 */ | ||
jest.useFakeTimers(); | ||
describe('Channel', function () { | ||
@@ -24,5 +25,12 @@ var transport = null; | ||
describe('constructor', function () { | ||
it('should set the handler', function () { | ||
it('should set the handler if handler is preset', function () { | ||
channel = new _.default({ | ||
transport: transport | ||
}); | ||
expect(transport.setHandler).toHaveBeenCalled(); | ||
}); | ||
it('should not try to set handler if handler is missing', function () { | ||
channel = new _.default(); | ||
expect(channel._transport).not.toBeDefined(); | ||
}); | ||
}); | ||
@@ -57,2 +65,19 @@ describe('method:addListener', function () { | ||
}); | ||
it('should call handle async option', function () { | ||
var _channel2; | ||
transport.send = jest.fn(); | ||
var type = 'test-type'; | ||
var args = [1, 2, 3]; | ||
channel = new _.default({ | ||
async: true, | ||
transport: transport | ||
}); | ||
(_channel2 = channel).emit.apply(_channel2, [type].concat(args)); | ||
expect(transport.send).not.toHaveBeenCalled(); | ||
jest.runAllImmediates(); | ||
expect(transport.send).toHaveBeenCalled(); | ||
}); | ||
}); | ||
@@ -59,0 +84,0 @@ describe('method:eventNames', function () { |
{ | ||
"name": "@storybook/channels", | ||
"version": "4.0.0-alpha.24", | ||
"version": "4.0.0-alpha.25", | ||
"description": "", | ||
@@ -25,3 +25,3 @@ "keywords": [ | ||
}, | ||
"gitHead": "bdc8120ab9456b34e2f67692cc3439ac75c4ba54" | ||
"gitHead": "155afad695926070cc6426e98d795907846f1207" | ||
} |
/* eslint no-underscore-dangle: 0 */ | ||
export default class Channel { | ||
constructor({ transport }) { | ||
constructor({ transport, async } = {}) { | ||
this._sender = this._randomId(); | ||
this._transport = transport; | ||
this._transport.setHandler(event => this._handleEvent(event)); | ||
this._async = async; | ||
if (transport) { | ||
this._transport = transport; | ||
this._transport.setHandler(event => this._handleEvent(event)); | ||
} | ||
this._listeners = {}; | ||
@@ -23,4 +26,15 @@ } | ||
const event = { type, args, from: this._sender }; | ||
this._transport.send(event); | ||
this._handleEvent(event, true); | ||
const handler = () => { | ||
if (this._transport) { | ||
this._transport.send(event); | ||
} | ||
this._handleEvent(event, true); | ||
}; | ||
if (this._async) { | ||
setImmediate(handler); | ||
} else { | ||
handler(); | ||
} | ||
} | ||
@@ -27,0 +41,0 @@ |
/* eslint no-underscore-dangle: 0 */ | ||
import Channel from '.'; | ||
jest.useFakeTimers(); | ||
describe('Channel', () => { | ||
@@ -15,5 +16,11 @@ let transport = null; | ||
describe('constructor', () => { | ||
it('should set the handler', () => { | ||
it('should set the handler if handler is preset', () => { | ||
channel = new Channel({ transport }); | ||
expect(transport.setHandler).toHaveBeenCalled(); | ||
}); | ||
it('should not try to set handler if handler is missing', () => { | ||
channel = new Channel(); | ||
expect(channel._transport).not.toBeDefined(); | ||
}); | ||
}); | ||
@@ -46,2 +53,16 @@ | ||
}); | ||
it('should call handle async option', () => { | ||
transport.send = jest.fn(); | ||
const type = 'test-type'; | ||
const args = [1, 2, 3]; | ||
channel = new Channel({ async: true, transport }); | ||
channel.emit(type, ...args); | ||
expect(transport.send).not.toHaveBeenCalled(); | ||
jest.runAllImmediates(); | ||
expect(transport.send).toHaveBeenCalled(); | ||
}); | ||
}); | ||
@@ -48,0 +69,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27143
750