broadcast-channel
Advanced tools
Comparing version 1.2.1 to 1.2.2
@@ -465,16 +465,14 @@ import _regeneratorRuntime from 'babel-runtime/regenerator'; | ||
export var create = function () { | ||
var _ref11 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee12(channelName) { | ||
var _this = this; | ||
var _ref11 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee11(channelName) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var uuid, _ref12, otherReaderUuids, socketEE, infoFilePath, otherReaderClients, readQueue, writeQueue, state; | ||
var uuid, _ref12, otherReaderUuids, socketEE, infoFilePath, readQueue, writeQueue, state; | ||
return _regeneratorRuntime.wrap(function _callee12$(_context12) { | ||
return _regeneratorRuntime.wrap(function _callee11$(_context11) { | ||
while (1) { | ||
switch (_context12.prev = _context12.next) { | ||
switch (_context11.prev = _context11.next) { | ||
case 0: | ||
options = fillOptionsWithDefaults(options); | ||
_context12.next = 3; | ||
_context11.next = 3; | ||
return ensureFoldersExist(channelName); | ||
@@ -484,45 +482,12 @@ | ||
uuid = randomToken(10); | ||
_context12.next = 6; | ||
_context11.next = 6; | ||
return Promise.all([getReadersUuids(channelName), createSocketEventEmitter(channelName, uuid), createSocketInfoFile(channelName, uuid)]); | ||
case 6: | ||
_ref12 = _context12.sent; | ||
_ref12 = _context11.sent; | ||
otherReaderUuids = _ref12[0]; | ||
socketEE = _ref12[1]; | ||
infoFilePath = _ref12[2]; | ||
otherReaderClients = {}; | ||
_context12.next = 13; | ||
return Promise.all(otherReaderUuids.filter(function (readerUuid) { | ||
return readerUuid !== uuid; | ||
}) // not own | ||
.map(function () { | ||
var _ref13 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee11(readerUuid) { | ||
var client; | ||
return _regeneratorRuntime.wrap(function _callee11$(_context11) { | ||
while (1) { | ||
switch (_context11.prev = _context11.next) { | ||
case 0: | ||
_context11.next = 2; | ||
return openClientConnection(channelName, readerUuid); | ||
case 2: | ||
client = _context11.sent; | ||
otherReaderClients[readerUuid] = client; | ||
case 4: | ||
case 'end': | ||
return _context11.stop(); | ||
} | ||
} | ||
}, _callee11, _this); | ||
})); | ||
return function (_x22) { | ||
return _ref13.apply(this, arguments); | ||
}; | ||
}())); | ||
case 13: | ||
// ensures we do not read messages in parrallel | ||
@@ -544,11 +509,15 @@ readQueue = new IdleQueue(1); | ||
otherReaderUuids: otherReaderUuids, | ||
otherReaderClients: otherReaderClients, | ||
otherReaderClients: {}, | ||
// ensure if process crashes, everything is cleaned up | ||
removeUnload: unload.add(function () { | ||
return close(state); | ||
}) | ||
}), | ||
closed: false | ||
}; | ||
_context11.next = 15; | ||
return refreshReaderClients(state); | ||
case 15: | ||
// when new message comes in, we read it and emit it | ||
socketEE.emitter.on('data', function (data) { | ||
@@ -562,10 +531,10 @@ var obj = JSON.parse(data); | ||
return _context12.abrupt('return', state); | ||
return _context11.abrupt('return', state); | ||
case 18: | ||
case 17: | ||
case 'end': | ||
return _context12.stop(); | ||
return _context11.stop(); | ||
} | ||
} | ||
}, _callee12, this); | ||
}, _callee11, this); | ||
})); | ||
@@ -583,29 +552,29 @@ | ||
export var handleMessagePing = function () { | ||
var _ref14 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee14(state) { | ||
var _this2 = this; | ||
var _ref13 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee13(state) { | ||
var _this = this; | ||
var msgObj = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; | ||
return _regeneratorRuntime.wrap(function _callee14$(_context15) { | ||
return _regeneratorRuntime.wrap(function _callee13$(_context14) { | ||
while (1) { | ||
switch (_context15.prev = _context15.next) { | ||
switch (_context14.prev = _context14.next) { | ||
case 0: | ||
if (state.messagesCallback) { | ||
_context15.next = 2; | ||
_context14.next = 2; | ||
break; | ||
} | ||
return _context15.abrupt('return'); | ||
return _context14.abrupt('return'); | ||
case 2: | ||
_context15.next = 4; | ||
_context14.next = 4; | ||
return state.readQueue.requestIdlePromise(); | ||
case 4: | ||
_context15.next = 6; | ||
return state.readQueue.wrapCall(_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee13() { | ||
var messages, useMessages, _loop, _iterator, _isArray, _i, _ref16, _ret; | ||
_context14.next = 6; | ||
return state.readQueue.wrapCall(_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee12() { | ||
var messages, useMessages, _loop, _iterator, _isArray, _i, _ref15, _ret; | ||
return _regeneratorRuntime.wrap(function _callee13$(_context14) { | ||
return _regeneratorRuntime.wrap(function _callee12$(_context13) { | ||
while (1) { | ||
switch (_context14.prev = _context14.next) { | ||
switch (_context13.prev = _context13.next) { | ||
case 0: | ||
@@ -615,12 +584,12 @@ messages = void 0; | ||
if (msgObj) { | ||
_context14.next = 7; | ||
_context13.next = 7; | ||
break; | ||
} | ||
_context14.next = 4; | ||
_context13.next = 4; | ||
return getAllMessages(state.channelName); | ||
case 4: | ||
messages = _context14.sent; | ||
_context14.next = 8; | ||
messages = _context13.sent; | ||
_context13.next = 8; | ||
break; | ||
@@ -647,3 +616,3 @@ | ||
if (!state.messagesCallback) { | ||
_context14.next = 18; | ||
_context13.next = 18; | ||
break; | ||
@@ -654,8 +623,8 @@ } | ||
var msgObj, content; | ||
return _regeneratorRuntime.wrap(function _loop$(_context13) { | ||
return _regeneratorRuntime.wrap(function _loop$(_context12) { | ||
while (1) { | ||
switch (_context13.prev = _context13.next) { | ||
switch (_context12.prev = _context12.next) { | ||
case 0: | ||
if (!_isArray) { | ||
_context13.next = 6; | ||
_context12.next = 6; | ||
break; | ||
@@ -665,11 +634,11 @@ } | ||
if (!(_i >= _iterator.length)) { | ||
_context13.next = 3; | ||
_context12.next = 3; | ||
break; | ||
} | ||
return _context13.abrupt('return', 'break'); | ||
return _context12.abrupt('return', 'break'); | ||
case 3: | ||
_ref16 = _iterator[_i++]; | ||
_context13.next = 10; | ||
_ref15 = _iterator[_i++]; | ||
_context12.next = 10; | ||
break; | ||
@@ -681,18 +650,18 @@ | ||
if (!_i.done) { | ||
_context13.next = 9; | ||
_context12.next = 9; | ||
break; | ||
} | ||
return _context13.abrupt('return', 'break'); | ||
return _context12.abrupt('return', 'break'); | ||
case 9: | ||
_ref16 = _i.value; | ||
_ref15 = _i.value; | ||
case 10: | ||
msgObj = _ref16; | ||
_context13.next = 13; | ||
msgObj = _ref15; | ||
_context12.next = 13; | ||
return readMessage(msgObj); | ||
case 13: | ||
content = _context13.sent; | ||
content = _context12.sent; | ||
@@ -710,6 +679,6 @@ state.emittedMessagesIds.add(msgObj.token); | ||
case 'end': | ||
return _context13.stop(); | ||
return _context12.stop(); | ||
} | ||
} | ||
}, _loop, _this2); | ||
}, _loop, _this); | ||
}); | ||
@@ -719,16 +688,16 @@ _iterator = useMessages, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator](); | ||
case 12: | ||
return _context14.delegateYield(_loop(), 't0', 13); | ||
return _context13.delegateYield(_loop(), 't0', 13); | ||
case 13: | ||
_ret = _context14.t0; | ||
_ret = _context13.t0; | ||
if (!(_ret === 'break')) { | ||
_context14.next = 16; | ||
_context13.next = 16; | ||
break; | ||
} | ||
return _context14.abrupt('break', 18); | ||
return _context13.abrupt('break', 18); | ||
case 16: | ||
_context14.next = 12; | ||
_context13.next = 12; | ||
break; | ||
@@ -738,6 +707,6 @@ | ||
case 'end': | ||
return _context14.stop(); | ||
return _context13.stop(); | ||
} | ||
} | ||
}, _callee13, _this2); | ||
}, _callee12, _this); | ||
}))); | ||
@@ -747,10 +716,10 @@ | ||
case 'end': | ||
return _context15.stop(); | ||
return _context14.stop(); | ||
} | ||
} | ||
}, _callee14, this); | ||
}, _callee13, this); | ||
})); | ||
return function handleMessagePing(_x23) { | ||
return _ref14.apply(this, arguments); | ||
return function handleMessagePing(_x22) { | ||
return _ref13.apply(this, arguments); | ||
}; | ||
@@ -760,4 +729,4 @@ }(); | ||
export var refreshReaderClients = function () { | ||
var _ref17 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee16(channelState) { | ||
var _this3 = this; | ||
var _ref16 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee16(channelState) { | ||
var _this2 = this; | ||
@@ -779,7 +748,36 @@ var otherReaders; | ||
return !otherReaders.includes(readerUuid); | ||
}).forEach(function (readerUuid) { | ||
channelState.otherReaderClients[readerUuid].destroy(); | ||
delete channelState.otherReaderClients[readerUuid]; | ||
}); | ||
}).forEach(function () { | ||
var _ref17 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee14(readerUuid) { | ||
return _regeneratorRuntime.wrap(function _callee14$(_context15) { | ||
while (1) { | ||
switch (_context15.prev = _context15.next) { | ||
case 0: | ||
_context15.prev = 0; | ||
_context15.next = 3; | ||
return channelState.otherReaderClients[readerUuid].destroy(); | ||
case 3: | ||
_context15.next = 7; | ||
break; | ||
case 5: | ||
_context15.prev = 5; | ||
_context15.t0 = _context15['catch'](0); | ||
case 7: | ||
delete channelState.otherReaderClients[readerUuid]; | ||
case 8: | ||
case 'end': | ||
return _context15.stop(); | ||
} | ||
} | ||
}, _callee14, _this2, [[0, 5]]); | ||
})); | ||
return function (_x25) { | ||
return _ref17.apply(this, arguments); | ||
}; | ||
}()); | ||
_context17.next = 6; | ||
@@ -799,11 +797,27 @@ return Promise.all(otherReaders.filter(function (readerUuid) { | ||
case 0: | ||
_context16.next = 2; | ||
_context16.prev = 0; | ||
if (!channelState.closed) { | ||
_context16.next = 3; | ||
break; | ||
} | ||
return _context16.abrupt('return'); | ||
case 3: | ||
_context16.next = 5; | ||
return openClientConnection(channelState.channelName, readerUuid); | ||
case 2: | ||
case 5: | ||
client = _context16.sent; | ||
channelState.otherReaderClients[readerUuid] = client; | ||
_context16.next = 11; | ||
break; | ||
case 4: | ||
case 9: | ||
_context16.prev = 9; | ||
_context16.t0 = _context16['catch'](0); | ||
case 11: | ||
case 'end': | ||
@@ -813,3 +827,3 @@ return _context16.stop(); | ||
} | ||
}, _callee15, _this3); | ||
}, _callee15, _this2, [[0, 9]]); | ||
})); | ||
@@ -820,3 +834,6 @@ | ||
}; | ||
}())); | ||
}() | ||
// this might throw if the other channel is closed at the same time when this one is running refresh | ||
// so we do not throw an error | ||
)); | ||
@@ -831,4 +848,4 @@ case 6: | ||
return function refreshReaderClients(_x25) { | ||
return _ref17.apply(this, arguments); | ||
return function refreshReaderClients(_x24) { | ||
return _ref16.apply(this, arguments); | ||
}; | ||
@@ -839,3 +856,3 @@ }(); | ||
var _ref19 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee18(channelState, messageJson) { | ||
var _this4 = this; | ||
var _this3 = this; | ||
@@ -901,3 +918,3 @@ return _regeneratorRuntime.wrap(function _callee18$(_context19) { | ||
} | ||
}, _callee17, _this4); | ||
}, _callee17, _this3); | ||
})) | ||
@@ -936,3 +953,6 @@ | ||
case 0: | ||
channelState.removeUnload(); | ||
channelState.closed = true; | ||
if (typeof channelState.removeUnload === 'function') channelState.removeUnload(); | ||
channelState.socketEE.server.close(); | ||
@@ -943,15 +963,15 @@ channelState.socketEE.emitter.removeAllListeners(); | ||
_context20.prev = 5; | ||
_context20.next = 8; | ||
_context20.prev = 6; | ||
_context20.next = 9; | ||
return unlink(channelState.infoFilePath); | ||
case 8: | ||
_context20.next = 12; | ||
case 9: | ||
_context20.next = 13; | ||
break; | ||
case 10: | ||
_context20.prev = 10; | ||
_context20.t0 = _context20['catch'](5); | ||
case 11: | ||
_context20.prev = 11; | ||
_context20.t0 = _context20['catch'](6); | ||
case 12: | ||
case 13: | ||
@@ -962,3 +982,3 @@ Object.values(channelState.otherReaderClients).forEach(function (client) { | ||
case 13: | ||
case 14: | ||
case 'end': | ||
@@ -968,3 +988,3 @@ return _context20.stop(); | ||
} | ||
}, _callee19, this, [[5, 10]]); | ||
}, _callee19, this, [[6, 11]]); | ||
})); | ||
@@ -971,0 +991,0 @@ |
@@ -404,16 +404,14 @@ 'use strict'; | ||
var create = exports.create = function () { | ||
var _ref11 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee12(channelName) { | ||
var _this = this; | ||
var _ref11 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee11(channelName) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var uuid, _ref12, _ref13, otherReaderUuids, socketEE, infoFilePath, otherReaderClients, readQueue, writeQueue, state; | ||
var uuid, _ref12, _ref13, otherReaderUuids, socketEE, infoFilePath, readQueue, writeQueue, state; | ||
return _regenerator2['default'].wrap(function _callee12$(_context12) { | ||
return _regenerator2['default'].wrap(function _callee11$(_context11) { | ||
while (1) { | ||
switch (_context12.prev = _context12.next) { | ||
switch (_context11.prev = _context11.next) { | ||
case 0: | ||
options = (0, _options.fillOptionsWithDefaults)(options); | ||
_context12.next = 3; | ||
_context11.next = 3; | ||
return ensureFoldersExist(channelName); | ||
@@ -423,7 +421,7 @@ | ||
uuid = (0, _util2.randomToken)(10); | ||
_context12.next = 6; | ||
_context11.next = 6; | ||
return Promise.all([getReadersUuids(channelName), createSocketEventEmitter(channelName, uuid), createSocketInfoFile(channelName, uuid)]); | ||
case 6: | ||
_ref12 = _context12.sent; | ||
_ref12 = _context11.sent; | ||
_ref13 = (0, _slicedToArray3['default'])(_ref12, 3); | ||
@@ -433,37 +431,4 @@ otherReaderUuids = _ref13[0]; | ||
infoFilePath = _ref13[2]; | ||
otherReaderClients = {}; | ||
_context12.next = 14; | ||
return Promise.all(otherReaderUuids.filter(function (readerUuid) { | ||
return readerUuid !== uuid; | ||
}) // not own | ||
.map(function () { | ||
var _ref14 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee11(readerUuid) { | ||
var client; | ||
return _regenerator2['default'].wrap(function _callee11$(_context11) { | ||
while (1) { | ||
switch (_context11.prev = _context11.next) { | ||
case 0: | ||
_context11.next = 2; | ||
return openClientConnection(channelName, readerUuid); | ||
case 2: | ||
client = _context11.sent; | ||
otherReaderClients[readerUuid] = client; | ||
case 4: | ||
case 'end': | ||
return _context11.stop(); | ||
} | ||
} | ||
}, _callee11, _this); | ||
})); | ||
return function (_x22) { | ||
return _ref14.apply(this, arguments); | ||
}; | ||
}())); | ||
case 14: | ||
// ensures we do not read messages in parrallel | ||
@@ -485,11 +450,15 @@ readQueue = new _customIdleQueue2['default'](1); | ||
otherReaderUuids: otherReaderUuids, | ||
otherReaderClients: otherReaderClients, | ||
otherReaderClients: {}, | ||
// ensure if process crashes, everything is cleaned up | ||
removeUnload: _unload2['default'].add(function () { | ||
return close(state); | ||
}) | ||
}), | ||
closed: false | ||
}; | ||
_context11.next = 16; | ||
return refreshReaderClients(state); | ||
case 16: | ||
// when new message comes in, we read it and emit it | ||
socketEE.emitter.on('data', function (data) { | ||
@@ -503,10 +472,10 @@ var obj = JSON.parse(data); | ||
return _context12.abrupt('return', state); | ||
return _context11.abrupt('return', state); | ||
case 19: | ||
case 18: | ||
case 'end': | ||
return _context12.stop(); | ||
return _context11.stop(); | ||
} | ||
} | ||
}, _callee12, this); | ||
}, _callee11, this); | ||
})); | ||
@@ -526,29 +495,29 @@ | ||
var handleMessagePing = exports.handleMessagePing = function () { | ||
var _ref15 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee14(state) { | ||
var _this2 = this; | ||
var _ref14 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee13(state) { | ||
var _this = this; | ||
var msgObj = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; | ||
return _regenerator2['default'].wrap(function _callee14$(_context15) { | ||
return _regenerator2['default'].wrap(function _callee13$(_context14) { | ||
while (1) { | ||
switch (_context15.prev = _context15.next) { | ||
switch (_context14.prev = _context14.next) { | ||
case 0: | ||
if (state.messagesCallback) { | ||
_context15.next = 2; | ||
_context14.next = 2; | ||
break; | ||
} | ||
return _context15.abrupt('return'); | ||
return _context14.abrupt('return'); | ||
case 2: | ||
_context15.next = 4; | ||
_context14.next = 4; | ||
return state.readQueue.requestIdlePromise(); | ||
case 4: | ||
_context15.next = 6; | ||
return state.readQueue.wrapCall((0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee13() { | ||
_context14.next = 6; | ||
return state.readQueue.wrapCall((0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee12() { | ||
var messages, useMessages, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _loop, _iterator, _step; | ||
return _regenerator2['default'].wrap(function _callee13$(_context14) { | ||
return _regenerator2['default'].wrap(function _callee12$(_context13) { | ||
while (1) { | ||
switch (_context14.prev = _context14.next) { | ||
switch (_context13.prev = _context13.next) { | ||
case 0: | ||
@@ -558,12 +527,12 @@ messages = void 0; | ||
if (msgObj) { | ||
_context14.next = 7; | ||
_context13.next = 7; | ||
break; | ||
} | ||
_context14.next = 4; | ||
_context13.next = 4; | ||
return getAllMessages(state.channelName); | ||
case 4: | ||
messages = _context14.sent; | ||
_context14.next = 8; | ||
messages = _context13.sent; | ||
_context13.next = 8; | ||
break; | ||
@@ -590,3 +559,3 @@ | ||
if (!state.messagesCallback) { | ||
_context14.next = 35; | ||
_context13.next = 35; | ||
break; | ||
@@ -598,15 +567,15 @@ } | ||
_iteratorError = undefined; | ||
_context14.prev = 13; | ||
_context13.prev = 13; | ||
_loop = /*#__PURE__*/_regenerator2['default'].mark(function _loop() { | ||
var msgObj, content; | ||
return _regenerator2['default'].wrap(function _loop$(_context13) { | ||
return _regenerator2['default'].wrap(function _loop$(_context12) { | ||
while (1) { | ||
switch (_context13.prev = _context13.next) { | ||
switch (_context12.prev = _context12.next) { | ||
case 0: | ||
msgObj = _step.value; | ||
_context13.next = 3; | ||
_context12.next = 3; | ||
return readMessage(msgObj); | ||
case 3: | ||
content = _context13.sent; | ||
content = _context12.sent; | ||
@@ -624,6 +593,6 @@ state.emittedMessagesIds.add(msgObj.token); | ||
case 'end': | ||
return _context13.stop(); | ||
return _context12.stop(); | ||
} | ||
} | ||
}, _loop, _this2); | ||
}, _loop, _this); | ||
}); | ||
@@ -634,26 +603,26 @@ _iterator = useMessages[Symbol.iterator](); | ||
if (_iteratorNormalCompletion = (_step = _iterator.next()).done) { | ||
_context14.next = 21; | ||
_context13.next = 21; | ||
break; | ||
} | ||
return _context14.delegateYield(_loop(), 't0', 18); | ||
return _context13.delegateYield(_loop(), 't0', 18); | ||
case 18: | ||
_iteratorNormalCompletion = true; | ||
_context14.next = 16; | ||
_context13.next = 16; | ||
break; | ||
case 21: | ||
_context14.next = 27; | ||
_context13.next = 27; | ||
break; | ||
case 23: | ||
_context14.prev = 23; | ||
_context14.t1 = _context14['catch'](13); | ||
_context13.prev = 23; | ||
_context13.t1 = _context13['catch'](13); | ||
_didIteratorError = true; | ||
_iteratorError = _context14.t1; | ||
_iteratorError = _context13.t1; | ||
case 27: | ||
_context14.prev = 27; | ||
_context14.prev = 28; | ||
_context13.prev = 27; | ||
_context13.prev = 28; | ||
@@ -665,6 +634,6 @@ if (!_iteratorNormalCompletion && _iterator['return']) { | ||
case 30: | ||
_context14.prev = 30; | ||
_context13.prev = 30; | ||
if (!_didIteratorError) { | ||
_context14.next = 33; | ||
_context13.next = 33; | ||
break; | ||
@@ -676,13 +645,13 @@ } | ||
case 33: | ||
return _context14.finish(30); | ||
return _context13.finish(30); | ||
case 34: | ||
return _context14.finish(27); | ||
return _context13.finish(27); | ||
case 35: | ||
case 'end': | ||
return _context14.stop(); | ||
return _context13.stop(); | ||
} | ||
} | ||
}, _callee13, _this2, [[13, 23, 27, 35], [28,, 30, 34]]); | ||
}, _callee12, _this, [[13, 23, 27, 35], [28,, 30, 34]]); | ||
}))); | ||
@@ -692,10 +661,10 @@ | ||
case 'end': | ||
return _context15.stop(); | ||
return _context14.stop(); | ||
} | ||
} | ||
}, _callee14, this); | ||
}, _callee13, this); | ||
})); | ||
return function handleMessagePing(_x24) { | ||
return _ref15.apply(this, arguments); | ||
return function handleMessagePing(_x23) { | ||
return _ref14.apply(this, arguments); | ||
}; | ||
@@ -705,4 +674,4 @@ }(); | ||
var refreshReaderClients = exports.refreshReaderClients = function () { | ||
var _ref17 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee16(channelState) { | ||
var _this3 = this; | ||
var _ref16 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee16(channelState) { | ||
var _this2 = this; | ||
@@ -724,7 +693,36 @@ var otherReaders; | ||
return !otherReaders.includes(readerUuid); | ||
}).forEach(function (readerUuid) { | ||
channelState.otherReaderClients[readerUuid].destroy(); | ||
delete channelState.otherReaderClients[readerUuid]; | ||
}); | ||
}).forEach(function () { | ||
var _ref17 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee14(readerUuid) { | ||
return _regenerator2['default'].wrap(function _callee14$(_context15) { | ||
while (1) { | ||
switch (_context15.prev = _context15.next) { | ||
case 0: | ||
_context15.prev = 0; | ||
_context15.next = 3; | ||
return channelState.otherReaderClients[readerUuid].destroy(); | ||
case 3: | ||
_context15.next = 7; | ||
break; | ||
case 5: | ||
_context15.prev = 5; | ||
_context15.t0 = _context15['catch'](0); | ||
case 7: | ||
delete channelState.otherReaderClients[readerUuid]; | ||
case 8: | ||
case 'end': | ||
return _context15.stop(); | ||
} | ||
} | ||
}, _callee14, _this2, [[0, 5]]); | ||
})); | ||
return function (_x25) { | ||
return _ref17.apply(this, arguments); | ||
}; | ||
}()); | ||
_context17.next = 6; | ||
@@ -744,11 +742,27 @@ return Promise.all(otherReaders.filter(function (readerUuid) { | ||
case 0: | ||
_context16.next = 2; | ||
_context16.prev = 0; | ||
if (!channelState.closed) { | ||
_context16.next = 3; | ||
break; | ||
} | ||
return _context16.abrupt('return'); | ||
case 3: | ||
_context16.next = 5; | ||
return openClientConnection(channelState.channelName, readerUuid); | ||
case 2: | ||
case 5: | ||
client = _context16.sent; | ||
channelState.otherReaderClients[readerUuid] = client; | ||
_context16.next = 11; | ||
break; | ||
case 4: | ||
case 9: | ||
_context16.prev = 9; | ||
_context16.t0 = _context16['catch'](0); | ||
case 11: | ||
case 'end': | ||
@@ -758,3 +772,3 @@ return _context16.stop(); | ||
} | ||
}, _callee15, _this3); | ||
}, _callee15, _this2, [[0, 9]]); | ||
})); | ||
@@ -765,3 +779,6 @@ | ||
}; | ||
}())); | ||
}() | ||
// this might throw if the other channel is closed at the same time when this one is running refresh | ||
// so we do not throw an error | ||
)); | ||
@@ -776,4 +793,4 @@ case 6: | ||
return function refreshReaderClients(_x25) { | ||
return _ref17.apply(this, arguments); | ||
return function refreshReaderClients(_x24) { | ||
return _ref16.apply(this, arguments); | ||
}; | ||
@@ -784,3 +801,3 @@ }(); | ||
var _ref19 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee18(channelState, messageJson) { | ||
var _this4 = this; | ||
var _this3 = this; | ||
@@ -846,3 +863,3 @@ return _regenerator2['default'].wrap(function _callee18$(_context19) { | ||
} | ||
}, _callee17, _this4); | ||
}, _callee17, _this3); | ||
})) | ||
@@ -873,3 +890,6 @@ | ||
case 0: | ||
channelState.removeUnload(); | ||
channelState.closed = true; | ||
if (typeof channelState.removeUnload === 'function') channelState.removeUnload(); | ||
channelState.socketEE.server.close(); | ||
@@ -880,15 +900,15 @@ channelState.socketEE.emitter.removeAllListeners(); | ||
_context20.prev = 5; | ||
_context20.next = 8; | ||
_context20.prev = 6; | ||
_context20.next = 9; | ||
return unlink(channelState.infoFilePath); | ||
case 8: | ||
_context20.next = 12; | ||
case 9: | ||
_context20.next = 13; | ||
break; | ||
case 10: | ||
_context20.prev = 10; | ||
_context20.t0 = _context20['catch'](5); | ||
case 11: | ||
_context20.prev = 11; | ||
_context20.t0 = _context20['catch'](6); | ||
case 12: | ||
case 13: | ||
@@ -899,3 +919,3 @@ Object.values(channelState.otherReaderClients).forEach(function (client) { | ||
case 13: | ||
case 14: | ||
case 'end': | ||
@@ -905,3 +925,3 @@ return _context20.stop(); | ||
} | ||
}, _callee19, this, [[5, 10]]); | ||
}, _callee19, this, [[6, 11]]); | ||
})); | ||
@@ -908,0 +928,0 @@ |
{ | ||
"name": "broadcast-channel", | ||
"version": "1.2.1", | ||
"version": "1.2.2", | ||
"description": "A BroadcastChannel implementation that works with new browsers, older browsers and Node.js", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/pubkey/broadcast-channel#readme", |
@@ -308,12 +308,2 @@ /** | ||
const otherReaderClients = {}; | ||
await Promise.all( | ||
otherReaderUuids | ||
.filter(readerUuid => readerUuid !== uuid) // not own | ||
.map(async (readerUuid) => { | ||
const client = await openClientConnection(channelName, readerUuid); | ||
otherReaderClients[readerUuid] = client; | ||
}) | ||
); | ||
// ensures we do not read messages in parrallel | ||
@@ -336,7 +326,10 @@ const readQueue = new IdleQueue(1); | ||
otherReaderUuids, | ||
otherReaderClients, | ||
otherReaderClients: {}, | ||
// ensure if process crashes, everything is cleaned up | ||
removeUnload: unload.add(() => close(state)) | ||
removeUnload: unload.add(() => close(state)), | ||
closed: false | ||
}; | ||
await refreshReaderClients(state); | ||
// when new message comes in, we read it and emit it | ||
@@ -412,4 +405,6 @@ socketEE.emitter.on('data', data => { | ||
.filter(readerUuid => !otherReaders.includes(readerUuid)) | ||
.forEach(readerUuid => { | ||
channelState.otherReaderClients[readerUuid].destroy(); | ||
.forEach(async (readerUuid) => { | ||
try { | ||
await channelState.otherReaderClients[readerUuid].destroy(); | ||
} catch (err) { } | ||
delete channelState.otherReaderClients[readerUuid]; | ||
@@ -423,4 +418,10 @@ }); | ||
.map(async (readerUuid) => { | ||
const client = await openClientConnection(channelState.channelName, readerUuid); | ||
channelState.otherReaderClients[readerUuid] = client; | ||
try { | ||
if (channelState.closed) return; | ||
const client = await openClientConnection(channelState.channelName, readerUuid); | ||
channelState.otherReaderClients[readerUuid] = client; | ||
} catch (err) { | ||
// this might throw if the other channel is closed at the same time when this one is running refresh | ||
// so we do not throw an error | ||
} | ||
}) | ||
@@ -481,3 +482,7 @@ ); | ||
export async function close(channelState) { | ||
channelState.removeUnload(); | ||
channelState.closed = true; | ||
if (typeof channelState.removeUnload === 'function') | ||
channelState.removeUnload(); | ||
channelState.socketEE.server.close(); | ||
@@ -484,0 +489,0 @@ channelState.socketEE.emitter.removeAllListeners(); |
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
204931
4722