broadcast-channel
Advanced tools
Comparing version 1.2.3 to 1.2.4
@@ -468,3 +468,3 @@ import _regeneratorRuntime from 'babel-runtime/regenerator'; | ||
var uuid, _ref12, otherReaderUuids, socketEE, infoFilePath, readQueue, writeQueue, state; | ||
var uuid, _ref12, otherReaderUuids, socketEE, infoFilePath, writeQueue, state; | ||
@@ -493,3 +493,2 @@ return _regeneratorRuntime.wrap(function _callee11$(_context11) { | ||
// ensures we do not read messages in parrallel | ||
readQueue = new IdleQueue(1); | ||
writeQueue = new IdleQueue(1); | ||
@@ -506,3 +505,2 @@ state = { | ||
messagesCallback: null, | ||
readQueue: readQueue, | ||
writeQueue: writeQueue, | ||
@@ -517,6 +515,6 @@ otherReaderUuids: otherReaderUuids, | ||
}; | ||
_context11.next = 15; | ||
_context11.next = 14; | ||
return refreshReaderClients(state); | ||
case 15: | ||
case 14: | ||
@@ -526,6 +524,3 @@ // when new message comes in, we read it and emit it | ||
var obj = JSON.parse(data); | ||
if (obj.a === 'msg') { | ||
handleMessagePing(state, obj.d); | ||
return; | ||
} | ||
handleMessagePing(state, obj); | ||
}); | ||
@@ -535,3 +530,3 @@ | ||
case 17: | ||
case 16: | ||
case 'end': | ||
@@ -554,161 +549,144 @@ return _context11.stop(); | ||
export var handleMessagePing = function () { | ||
var _ref13 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee13(state) { | ||
var _ref13 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee12(state) { | ||
var _this = this; | ||
var msgObj = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; | ||
return _regeneratorRuntime.wrap(function _callee13$(_context14) { | ||
var messages, useMessages, _loop, _iterator, _isArray, _i, _ref14, _ret; | ||
return _regeneratorRuntime.wrap(function _callee12$(_context13) { | ||
while (1) { | ||
switch (_context14.prev = _context14.next) { | ||
switch (_context13.prev = _context13.next) { | ||
case 0: | ||
if (state.messagesCallback) { | ||
_context14.next = 2; | ||
_context13.next = 2; | ||
break; | ||
} | ||
return _context14.abrupt('return'); | ||
return _context13.abrupt('return'); | ||
case 2: | ||
_context14.next = 4; | ||
return state.readQueue.requestIdlePromise(); | ||
messages = void 0; | ||
case 4: | ||
_context14.next = 6; | ||
return state.readQueue.wrapCall(_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee12() { | ||
var messages, useMessages, _loop, _iterator, _isArray, _i, _ref15, _ret; | ||
if (msgObj) { | ||
_context13.next = 9; | ||
break; | ||
} | ||
return _regeneratorRuntime.wrap(function _callee12$(_context13) { | ||
_context13.next = 6; | ||
return getAllMessages(state.channelName); | ||
case 6: | ||
messages = _context13.sent; | ||
_context13.next = 10; | ||
break; | ||
case 9: | ||
// get single message | ||
messages = [getSingleMessage(state.channelName, msgObj)]; | ||
case 10: | ||
useMessages = messages.filter(function (msgObj) { | ||
return msgObj.senderUuid !== state.uuid; | ||
}) // not send by own | ||
.filter(function (msgObj) { | ||
return !state.emittedMessagesIds.has(msgObj.token); | ||
}) // not already emitted | ||
.filter(function (msgObj) { | ||
return msgObj.time >= state.messagesCallbackTime; | ||
}) // not older then onMessageCallback | ||
.sort(function (msgObjA, msgObjB) { | ||
return msgObjA.time - msgObjB.time; | ||
}); // sort by time | ||
if (!state.messagesCallback) { | ||
_context13.next = 20; | ||
break; | ||
} | ||
_loop = /*#__PURE__*/_regeneratorRuntime.mark(function _loop() { | ||
var msgObj, content; | ||
return _regeneratorRuntime.wrap(function _loop$(_context12) { | ||
while (1) { | ||
switch (_context13.prev = _context13.next) { | ||
switch (_context12.prev = _context12.next) { | ||
case 0: | ||
messages = void 0; | ||
if (!_isArray) { | ||
_context12.next = 6; | ||
break; | ||
} | ||
if (msgObj) { | ||
_context13.next = 7; | ||
if (!(_i >= _iterator.length)) { | ||
_context12.next = 3; | ||
break; | ||
} | ||
_context13.next = 4; | ||
return getAllMessages(state.channelName); | ||
return _context12.abrupt('return', 'break'); | ||
case 4: | ||
messages = _context13.sent; | ||
_context13.next = 8; | ||
case 3: | ||
_ref14 = _iterator[_i++]; | ||
_context12.next = 10; | ||
break; | ||
case 7: | ||
// get single message | ||
messages = [getSingleMessage(state.channelName, msgObj)]; | ||
case 6: | ||
_i = _iterator.next(); | ||
case 8: | ||
useMessages = messages.filter(function (msgObj) { | ||
return msgObj.senderUuid !== state.uuid; | ||
}) // not send by own | ||
.filter(function (msgObj) { | ||
return !state.emittedMessagesIds.has(msgObj.token); | ||
}) // not already emitted | ||
.filter(function (msgObj) { | ||
return msgObj.time >= state.messagesCallbackTime; | ||
}) // not older then onMessageCallback | ||
.sort(function (msgObjA, msgObjB) { | ||
return msgObjA.time - msgObjB.time; | ||
}); // sort by time | ||
if (!state.messagesCallback) { | ||
_context13.next = 18; | ||
if (!_i.done) { | ||
_context12.next = 9; | ||
break; | ||
} | ||
_loop = /*#__PURE__*/_regeneratorRuntime.mark(function _loop() { | ||
var msgObj, content; | ||
return _regeneratorRuntime.wrap(function _loop$(_context12) { | ||
while (1) { | ||
switch (_context12.prev = _context12.next) { | ||
case 0: | ||
if (!_isArray) { | ||
_context12.next = 6; | ||
break; | ||
} | ||
return _context12.abrupt('return', 'break'); | ||
if (!(_i >= _iterator.length)) { | ||
_context12.next = 3; | ||
break; | ||
} | ||
case 9: | ||
_ref14 = _i.value; | ||
return _context12.abrupt('return', 'break'); | ||
case 10: | ||
msgObj = _ref14; | ||
_context12.next = 13; | ||
return readMessage(msgObj); | ||
case 3: | ||
_ref15 = _iterator[_i++]; | ||
_context12.next = 10; | ||
break; | ||
case 13: | ||
content = _context12.sent; | ||
case 6: | ||
_i = _iterator.next(); | ||
state.emittedMessagesIds.add(msgObj.token); | ||
setTimeout(function () { | ||
return state.emittedMessagesIds['delete'](msgObj.token); | ||
}, state.options.node.ttl * 2); | ||
if (!_i.done) { | ||
_context12.next = 9; | ||
break; | ||
} | ||
if (state.messagesCallback) { | ||
state.messagesCallback(content.data); | ||
} | ||
return _context12.abrupt('return', 'break'); | ||
case 17: | ||
case 'end': | ||
return _context12.stop(); | ||
} | ||
} | ||
}, _loop, _this); | ||
}); | ||
_iterator = useMessages, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator](); | ||
case 9: | ||
_ref15 = _i.value; | ||
case 14: | ||
return _context13.delegateYield(_loop(), 't0', 15); | ||
case 10: | ||
msgObj = _ref15; | ||
_context12.next = 13; | ||
return readMessage(msgObj); | ||
case 15: | ||
_ret = _context13.t0; | ||
case 13: | ||
content = _context12.sent; | ||
if (!(_ret === 'break')) { | ||
_context13.next = 18; | ||
break; | ||
} | ||
state.emittedMessagesIds.add(msgObj.token); | ||
setTimeout(function () { | ||
return state.emittedMessagesIds['delete'](msgObj.token); | ||
}, state.options.node.ttl * 2); | ||
return _context13.abrupt('break', 20); | ||
if (state.messagesCallback) { | ||
state.messagesCallback(content.data); | ||
} | ||
case 18: | ||
_context13.next = 14; | ||
break; | ||
case 17: | ||
case 'end': | ||
return _context12.stop(); | ||
} | ||
} | ||
}, _loop, _this); | ||
}); | ||
_iterator = useMessages, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator](); | ||
case 12: | ||
return _context13.delegateYield(_loop(), 't0', 13); | ||
case 13: | ||
_ret = _context13.t0; | ||
if (!(_ret === 'break')) { | ||
_context13.next = 16; | ||
break; | ||
} | ||
return _context13.abrupt('break', 18); | ||
case 16: | ||
_context13.next = 12; | ||
break; | ||
case 18: | ||
case 'end': | ||
return _context13.stop(); | ||
} | ||
} | ||
}, _callee12, _this); | ||
}))); | ||
case 6: | ||
case 20: | ||
case 'end': | ||
return _context14.stop(); | ||
return _context13.stop(); | ||
} | ||
} | ||
}, _callee13, this); | ||
}, _callee12, this); | ||
})); | ||
@@ -722,15 +700,15 @@ | ||
export var refreshReaderClients = function () { | ||
var _ref16 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee16(channelState) { | ||
var _ref15 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee15(channelState) { | ||
var _this2 = this; | ||
var otherReaders; | ||
return _regeneratorRuntime.wrap(function _callee16$(_context17) { | ||
return _regeneratorRuntime.wrap(function _callee15$(_context16) { | ||
while (1) { | ||
switch (_context17.prev = _context17.next) { | ||
switch (_context16.prev = _context16.next) { | ||
case 0: | ||
_context17.next = 2; | ||
_context16.next = 2; | ||
return getReadersUuids(channelState.channelName); | ||
case 2: | ||
otherReaders = _context17.sent; | ||
otherReaders = _context16.sent; | ||
@@ -742,18 +720,18 @@ | ||
}).forEach(function () { | ||
var _ref17 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee14(readerUuid) { | ||
return _regeneratorRuntime.wrap(function _callee14$(_context15) { | ||
var _ref16 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee13(readerUuid) { | ||
return _regeneratorRuntime.wrap(function _callee13$(_context14) { | ||
while (1) { | ||
switch (_context15.prev = _context15.next) { | ||
switch (_context14.prev = _context14.next) { | ||
case 0: | ||
_context15.prev = 0; | ||
_context15.next = 3; | ||
_context14.prev = 0; | ||
_context14.next = 3; | ||
return channelState.otherReaderClients[readerUuid].destroy(); | ||
case 3: | ||
_context15.next = 7; | ||
_context14.next = 7; | ||
break; | ||
case 5: | ||
_context15.prev = 5; | ||
_context15.t0 = _context15['catch'](0); | ||
_context14.prev = 5; | ||
_context14.t0 = _context14['catch'](0); | ||
@@ -765,14 +743,14 @@ case 7: | ||
case 'end': | ||
return _context15.stop(); | ||
return _context14.stop(); | ||
} | ||
} | ||
}, _callee14, _this2, [[0, 5]]); | ||
}, _callee13, _this2, [[0, 5]]); | ||
})); | ||
return function (_x25) { | ||
return _ref17.apply(this, arguments); | ||
return _ref16.apply(this, arguments); | ||
}; | ||
}()); | ||
_context17.next = 6; | ||
_context16.next = 6; | ||
return Promise.all(otherReaders.filter(function (readerUuid) { | ||
@@ -785,42 +763,42 @@ return readerUuid !== channelState.uuid; | ||
.map(function () { | ||
var _ref18 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee15(readerUuid) { | ||
var _ref17 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee14(readerUuid) { | ||
var client; | ||
return _regeneratorRuntime.wrap(function _callee15$(_context16) { | ||
return _regeneratorRuntime.wrap(function _callee14$(_context15) { | ||
while (1) { | ||
switch (_context16.prev = _context16.next) { | ||
switch (_context15.prev = _context15.next) { | ||
case 0: | ||
_context16.prev = 0; | ||
_context15.prev = 0; | ||
if (!channelState.closed) { | ||
_context16.next = 3; | ||
_context15.next = 3; | ||
break; | ||
} | ||
return _context16.abrupt('return'); | ||
return _context15.abrupt('return'); | ||
case 3: | ||
_context16.next = 5; | ||
_context15.next = 5; | ||
return openClientConnection(channelState.channelName, readerUuid); | ||
case 5: | ||
client = _context16.sent; | ||
client = _context15.sent; | ||
channelState.otherReaderClients[readerUuid] = client; | ||
_context16.next = 11; | ||
_context15.next = 11; | ||
break; | ||
case 9: | ||
_context16.prev = 9; | ||
_context16.t0 = _context16['catch'](0); | ||
_context15.prev = 9; | ||
_context15.t0 = _context15['catch'](0); | ||
case 11: | ||
case 'end': | ||
return _context16.stop(); | ||
return _context15.stop(); | ||
} | ||
} | ||
}, _callee15, _this2, [[0, 9]]); | ||
}, _callee14, _this2, [[0, 9]]); | ||
})); | ||
return function (_x26) { | ||
return _ref18.apply(this, arguments); | ||
return _ref17.apply(this, arguments); | ||
}; | ||
@@ -834,10 +812,10 @@ }() | ||
case 'end': | ||
return _context17.stop(); | ||
return _context16.stop(); | ||
} | ||
} | ||
}, _callee16, this); | ||
}, _callee15, this); | ||
})); | ||
return function refreshReaderClients(_x24) { | ||
return _ref16.apply(this, arguments); | ||
return _ref15.apply(this, arguments); | ||
}; | ||
@@ -847,41 +825,31 @@ }(); | ||
export var postMessage = function () { | ||
var _ref19 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee18(channelState, messageJson) { | ||
var _ref18 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee17(channelState, messageJson) { | ||
var _this3 = this; | ||
return _regeneratorRuntime.wrap(function _callee18$(_context19) { | ||
return _regeneratorRuntime.wrap(function _callee17$(_context18) { | ||
while (1) { | ||
switch (_context19.prev = _context19.next) { | ||
switch (_context18.prev = _context18.next) { | ||
case 0: | ||
_context19.next = 2; | ||
_context18.next = 2; | ||
return channelState.writeQueue.requestIdlePromise(); | ||
case 2: | ||
_context19.next = 4; | ||
return channelState.writeQueue.wrapCall(_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee17() { | ||
var msgObj, pingObj, messages; | ||
return _regeneratorRuntime.wrap(function _callee17$(_context18) { | ||
_context18.next = 4; | ||
return channelState.writeQueue.wrapCall(_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee16() { | ||
var msgObj, pingStr, messages; | ||
return _regeneratorRuntime.wrap(function _callee16$(_context17) { | ||
while (1) { | ||
switch (_context18.prev = _context18.next) { | ||
switch (_context17.prev = _context17.next) { | ||
case 0: | ||
_context18.next = 2; | ||
_context17.next = 2; | ||
return refreshReaderClients(channelState); | ||
case 2: | ||
_context18.next = 4; | ||
_context17.next = 4; | ||
return writeMessage(channelState.channelName, channelState.uuid, messageJson); | ||
case 4: | ||
msgObj = _context18.sent; | ||
// ping other readers | ||
pingObj = { | ||
a: 'msg', | ||
d: { | ||
t: msgObj.time, | ||
u: msgObj.uuid, | ||
to: msgObj.token | ||
} | ||
}; | ||
_context18.next = 8; | ||
msgObj = _context17.sent; | ||
pingStr = '{"t":' + msgObj.time + ',"u":"' + msgObj.uuid + '","to":"' + msgObj.token + '"}'; | ||
_context17.next = 8; | ||
return Promise.all(Object.values(channelState.otherReaderClients).filter(function (client) { | ||
@@ -891,25 +859,27 @@ return client.writable; | ||
.map(function (client) { | ||
return client.write(JSON.stringify(pingObj)); | ||
return new Promise(function (res) { | ||
client.write(pingStr, res); | ||
}); | ||
})); | ||
case 8: | ||
if (!(randomInt(0, 10) === 0)) { | ||
_context18.next = 14; | ||
if (!(randomInt(0, 50) === 0)) { | ||
_context17.next = 13; | ||
break; | ||
} | ||
_context18.next = 11; | ||
_context17.next = 11; | ||
return getAllMessages(channelState.channelName); | ||
case 11: | ||
messages = _context18.sent; | ||
_context18.next = 14; | ||
return cleanOldMessages(messages, channelState.options.node.ttl); | ||
messages = _context17.sent; | ||
case 14: | ||
/*await*/cleanOldMessages(messages, channelState.options.node.ttl); | ||
case 13: | ||
case 'end': | ||
return _context18.stop(); | ||
return _context17.stop(); | ||
} | ||
} | ||
}, _callee17, _this3); | ||
}, _callee16, _this3); | ||
})) | ||
@@ -923,10 +893,10 @@ | ||
case 'end': | ||
return _context19.stop(); | ||
return _context18.stop(); | ||
} | ||
} | ||
}, _callee18, this); | ||
}, _callee17, this); | ||
})); | ||
return function postMessage(_x27, _x28) { | ||
return _ref19.apply(this, arguments); | ||
return _ref18.apply(this, arguments); | ||
}; | ||
@@ -944,13 +914,13 @@ }(); | ||
export var close = function () { | ||
var _ref21 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee19(channelState) { | ||
return _regeneratorRuntime.wrap(function _callee19$(_context20) { | ||
var _ref20 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee18(channelState) { | ||
return _regeneratorRuntime.wrap(function _callee18$(_context19) { | ||
while (1) { | ||
switch (_context20.prev = _context20.next) { | ||
switch (_context19.prev = _context19.next) { | ||
case 0: | ||
if (!channelState.closed) { | ||
_context20.next = 2; | ||
_context19.next = 2; | ||
break; | ||
} | ||
return _context20.abrupt('return'); | ||
return _context19.abrupt('return'); | ||
@@ -971,6 +941,5 @@ case 2: | ||
channelState.socketEE.emitter.removeAllListeners(); | ||
channelState.readQueue.clear(); | ||
channelState.writeQueue.clear(); | ||
_context20.next = 10; | ||
_context19.next = 9; | ||
return unlink(channelState.infoFilePath)['catch'](function () { | ||
@@ -980,3 +949,3 @@ return null; | ||
case 10: | ||
case 9: | ||
@@ -987,12 +956,12 @@ Object.values(channelState.otherReaderClients).forEach(function (client) { | ||
case 11: | ||
case 10: | ||
case 'end': | ||
return _context20.stop(); | ||
return _context19.stop(); | ||
} | ||
} | ||
}, _callee19, this); | ||
}, _callee18, this); | ||
})); | ||
return function close(_x30) { | ||
return _ref21.apply(this, arguments); | ||
return _ref20.apply(this, arguments); | ||
}; | ||
@@ -999,0 +968,0 @@ }(); |
@@ -407,3 +407,3 @@ 'use strict'; | ||
var uuid, _ref12, _ref13, otherReaderUuids, socketEE, infoFilePath, readQueue, writeQueue, state; | ||
var uuid, _ref12, _ref13, otherReaderUuids, socketEE, infoFilePath, writeQueue, state; | ||
@@ -433,3 +433,2 @@ return _regenerator2['default'].wrap(function _callee11$(_context11) { | ||
// ensures we do not read messages in parrallel | ||
readQueue = new _customIdleQueue2['default'](1); | ||
writeQueue = new _customIdleQueue2['default'](1); | ||
@@ -446,3 +445,2 @@ state = { | ||
messagesCallback: null, | ||
readQueue: readQueue, | ||
writeQueue: writeQueue, | ||
@@ -457,6 +455,6 @@ otherReaderUuids: otherReaderUuids, | ||
}; | ||
_context11.next = 16; | ||
_context11.next = 15; | ||
return refreshReaderClients(state); | ||
case 16: | ||
case 15: | ||
@@ -466,6 +464,3 @@ // when new message comes in, we read it and emit it | ||
var obj = JSON.parse(data); | ||
if (obj.a === 'msg') { | ||
handleMessagePing(state, obj.d); | ||
return; | ||
} | ||
handleMessagePing(state, obj); | ||
}); | ||
@@ -475,3 +470,3 @@ | ||
case 18: | ||
case 17: | ||
case 'end': | ||
@@ -496,164 +491,147 @@ return _context11.stop(); | ||
var handleMessagePing = exports.handleMessagePing = function () { | ||
var _ref14 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee13(state) { | ||
var _ref14 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee12(state) { | ||
var _this = this; | ||
var msgObj = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; | ||
return _regenerator2['default'].wrap(function _callee13$(_context14) { | ||
var messages, useMessages, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _loop, _iterator, _step; | ||
return _regenerator2['default'].wrap(function _callee12$(_context13) { | ||
while (1) { | ||
switch (_context14.prev = _context14.next) { | ||
switch (_context13.prev = _context13.next) { | ||
case 0: | ||
if (state.messagesCallback) { | ||
_context14.next = 2; | ||
_context13.next = 2; | ||
break; | ||
} | ||
return _context14.abrupt('return'); | ||
return _context13.abrupt('return'); | ||
case 2: | ||
_context14.next = 4; | ||
return state.readQueue.requestIdlePromise(); | ||
messages = void 0; | ||
case 4: | ||
_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; | ||
if (msgObj) { | ||
_context13.next = 9; | ||
break; | ||
} | ||
return _regenerator2['default'].wrap(function _callee12$(_context13) { | ||
while (1) { | ||
switch (_context13.prev = _context13.next) { | ||
case 0: | ||
messages = void 0; | ||
_context13.next = 6; | ||
return getAllMessages(state.channelName); | ||
if (msgObj) { | ||
_context13.next = 7; | ||
break; | ||
} | ||
case 6: | ||
messages = _context13.sent; | ||
_context13.next = 10; | ||
break; | ||
_context13.next = 4; | ||
return getAllMessages(state.channelName); | ||
case 9: | ||
// get single message | ||
messages = [getSingleMessage(state.channelName, msgObj)]; | ||
case 4: | ||
messages = _context13.sent; | ||
_context13.next = 8; | ||
break; | ||
case 10: | ||
useMessages = messages.filter(function (msgObj) { | ||
return msgObj.senderUuid !== state.uuid; | ||
}) // not send by own | ||
.filter(function (msgObj) { | ||
return !state.emittedMessagesIds.has(msgObj.token); | ||
}) // not already emitted | ||
.filter(function (msgObj) { | ||
return msgObj.time >= state.messagesCallbackTime; | ||
}) // not older then onMessageCallback | ||
.sort(function (msgObjA, msgObjB) { | ||
return msgObjA.time - msgObjB.time; | ||
}); // sort by time | ||
case 7: | ||
// get single message | ||
messages = [getSingleMessage(state.channelName, msgObj)]; | ||
if (!state.messagesCallback) { | ||
_context13.next = 37; | ||
break; | ||
} | ||
case 8: | ||
useMessages = messages.filter(function (msgObj) { | ||
return msgObj.senderUuid !== state.uuid; | ||
}) // not send by own | ||
.filter(function (msgObj) { | ||
return !state.emittedMessagesIds.has(msgObj.token); | ||
}) // not already emitted | ||
.filter(function (msgObj) { | ||
return msgObj.time >= state.messagesCallbackTime; | ||
}) // not older then onMessageCallback | ||
.sort(function (msgObjA, msgObjB) { | ||
return msgObjA.time - msgObjB.time; | ||
}); // sort by time | ||
_iteratorNormalCompletion = true; | ||
_didIteratorError = false; | ||
_iteratorError = undefined; | ||
_context13.prev = 15; | ||
_loop = /*#__PURE__*/_regenerator2['default'].mark(function _loop() { | ||
var msgObj, content; | ||
return _regenerator2['default'].wrap(function _loop$(_context12) { | ||
while (1) { | ||
switch (_context12.prev = _context12.next) { | ||
case 0: | ||
msgObj = _step.value; | ||
_context12.next = 3; | ||
return readMessage(msgObj); | ||
if (!state.messagesCallback) { | ||
_context13.next = 35; | ||
break; | ||
} | ||
case 3: | ||
content = _context12.sent; | ||
_iteratorNormalCompletion = true; | ||
_didIteratorError = false; | ||
_iteratorError = undefined; | ||
_context13.prev = 13; | ||
_loop = /*#__PURE__*/_regenerator2['default'].mark(function _loop() { | ||
var msgObj, content; | ||
return _regenerator2['default'].wrap(function _loop$(_context12) { | ||
while (1) { | ||
switch (_context12.prev = _context12.next) { | ||
case 0: | ||
msgObj = _step.value; | ||
_context12.next = 3; | ||
return readMessage(msgObj); | ||
state.emittedMessagesIds.add(msgObj.token); | ||
setTimeout(function () { | ||
return state.emittedMessagesIds['delete'](msgObj.token); | ||
}, state.options.node.ttl * 2); | ||
case 3: | ||
content = _context12.sent; | ||
if (state.messagesCallback) { | ||
state.messagesCallback(content.data); | ||
} | ||
state.emittedMessagesIds.add(msgObj.token); | ||
setTimeout(function () { | ||
return state.emittedMessagesIds['delete'](msgObj.token); | ||
}, state.options.node.ttl * 2); | ||
case 7: | ||
case 'end': | ||
return _context12.stop(); | ||
} | ||
} | ||
}, _loop, _this); | ||
}); | ||
_iterator = useMessages[Symbol.iterator](); | ||
if (state.messagesCallback) { | ||
state.messagesCallback(content.data); | ||
} | ||
case 18: | ||
if (_iteratorNormalCompletion = (_step = _iterator.next()).done) { | ||
_context13.next = 23; | ||
break; | ||
} | ||
case 7: | ||
case 'end': | ||
return _context12.stop(); | ||
} | ||
} | ||
}, _loop, _this); | ||
}); | ||
_iterator = useMessages[Symbol.iterator](); | ||
return _context13.delegateYield(_loop(), 't0', 20); | ||
case 16: | ||
if (_iteratorNormalCompletion = (_step = _iterator.next()).done) { | ||
_context13.next = 21; | ||
break; | ||
} | ||
case 20: | ||
_iteratorNormalCompletion = true; | ||
_context13.next = 18; | ||
break; | ||
return _context13.delegateYield(_loop(), 't0', 18); | ||
case 23: | ||
_context13.next = 29; | ||
break; | ||
case 18: | ||
_iteratorNormalCompletion = true; | ||
_context13.next = 16; | ||
break; | ||
case 25: | ||
_context13.prev = 25; | ||
_context13.t1 = _context13['catch'](15); | ||
_didIteratorError = true; | ||
_iteratorError = _context13.t1; | ||
case 21: | ||
_context13.next = 27; | ||
break; | ||
case 29: | ||
_context13.prev = 29; | ||
_context13.prev = 30; | ||
case 23: | ||
_context13.prev = 23; | ||
_context13.t1 = _context13['catch'](13); | ||
_didIteratorError = true; | ||
_iteratorError = _context13.t1; | ||
if (!_iteratorNormalCompletion && _iterator['return']) { | ||
_iterator['return'](); | ||
} | ||
case 27: | ||
_context13.prev = 27; | ||
_context13.prev = 28; | ||
case 32: | ||
_context13.prev = 32; | ||
if (!_iteratorNormalCompletion && _iterator['return']) { | ||
_iterator['return'](); | ||
} | ||
if (!_didIteratorError) { | ||
_context13.next = 35; | ||
break; | ||
} | ||
case 30: | ||
_context13.prev = 30; | ||
throw _iteratorError; | ||
if (!_didIteratorError) { | ||
_context13.next = 33; | ||
break; | ||
} | ||
case 35: | ||
return _context13.finish(32); | ||
throw _iteratorError; | ||
case 36: | ||
return _context13.finish(29); | ||
case 33: | ||
return _context13.finish(30); | ||
case 34: | ||
return _context13.finish(27); | ||
case 35: | ||
case 'end': | ||
return _context13.stop(); | ||
} | ||
} | ||
}, _callee12, _this, [[13, 23, 27, 35], [28,, 30, 34]]); | ||
}))); | ||
case 6: | ||
case 37: | ||
case 'end': | ||
return _context14.stop(); | ||
return _context13.stop(); | ||
} | ||
} | ||
}, _callee13, this); | ||
}, _callee12, this, [[15, 25, 29, 37], [30,, 32, 36]]); | ||
})); | ||
@@ -667,15 +645,15 @@ | ||
var refreshReaderClients = exports.refreshReaderClients = function () { | ||
var _ref16 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee16(channelState) { | ||
var _ref15 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee15(channelState) { | ||
var _this2 = this; | ||
var otherReaders; | ||
return _regenerator2['default'].wrap(function _callee16$(_context17) { | ||
return _regenerator2['default'].wrap(function _callee15$(_context16) { | ||
while (1) { | ||
switch (_context17.prev = _context17.next) { | ||
switch (_context16.prev = _context16.next) { | ||
case 0: | ||
_context17.next = 2; | ||
_context16.next = 2; | ||
return getReadersUuids(channelState.channelName); | ||
case 2: | ||
otherReaders = _context17.sent; | ||
otherReaders = _context16.sent; | ||
@@ -687,18 +665,18 @@ | ||
}).forEach(function () { | ||
var _ref17 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee14(readerUuid) { | ||
return _regenerator2['default'].wrap(function _callee14$(_context15) { | ||
var _ref16 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee13(readerUuid) { | ||
return _regenerator2['default'].wrap(function _callee13$(_context14) { | ||
while (1) { | ||
switch (_context15.prev = _context15.next) { | ||
switch (_context14.prev = _context14.next) { | ||
case 0: | ||
_context15.prev = 0; | ||
_context15.next = 3; | ||
_context14.prev = 0; | ||
_context14.next = 3; | ||
return channelState.otherReaderClients[readerUuid].destroy(); | ||
case 3: | ||
_context15.next = 7; | ||
_context14.next = 7; | ||
break; | ||
case 5: | ||
_context15.prev = 5; | ||
_context15.t0 = _context15['catch'](0); | ||
_context14.prev = 5; | ||
_context14.t0 = _context14['catch'](0); | ||
@@ -710,14 +688,14 @@ case 7: | ||
case 'end': | ||
return _context15.stop(); | ||
return _context14.stop(); | ||
} | ||
} | ||
}, _callee14, _this2, [[0, 5]]); | ||
}, _callee13, _this2, [[0, 5]]); | ||
})); | ||
return function (_x25) { | ||
return _ref17.apply(this, arguments); | ||
return _ref16.apply(this, arguments); | ||
}; | ||
}()); | ||
_context17.next = 6; | ||
_context16.next = 6; | ||
return Promise.all(otherReaders.filter(function (readerUuid) { | ||
@@ -730,42 +708,42 @@ return readerUuid !== channelState.uuid; | ||
.map(function () { | ||
var _ref18 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee15(readerUuid) { | ||
var _ref17 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee14(readerUuid) { | ||
var client; | ||
return _regenerator2['default'].wrap(function _callee15$(_context16) { | ||
return _regenerator2['default'].wrap(function _callee14$(_context15) { | ||
while (1) { | ||
switch (_context16.prev = _context16.next) { | ||
switch (_context15.prev = _context15.next) { | ||
case 0: | ||
_context16.prev = 0; | ||
_context15.prev = 0; | ||
if (!channelState.closed) { | ||
_context16.next = 3; | ||
_context15.next = 3; | ||
break; | ||
} | ||
return _context16.abrupt('return'); | ||
return _context15.abrupt('return'); | ||
case 3: | ||
_context16.next = 5; | ||
_context15.next = 5; | ||
return openClientConnection(channelState.channelName, readerUuid); | ||
case 5: | ||
client = _context16.sent; | ||
client = _context15.sent; | ||
channelState.otherReaderClients[readerUuid] = client; | ||
_context16.next = 11; | ||
_context15.next = 11; | ||
break; | ||
case 9: | ||
_context16.prev = 9; | ||
_context16.t0 = _context16['catch'](0); | ||
_context15.prev = 9; | ||
_context15.t0 = _context15['catch'](0); | ||
case 11: | ||
case 'end': | ||
return _context16.stop(); | ||
return _context15.stop(); | ||
} | ||
} | ||
}, _callee15, _this2, [[0, 9]]); | ||
}, _callee14, _this2, [[0, 9]]); | ||
})); | ||
return function (_x26) { | ||
return _ref18.apply(this, arguments); | ||
return _ref17.apply(this, arguments); | ||
}; | ||
@@ -779,10 +757,10 @@ }() | ||
case 'end': | ||
return _context17.stop(); | ||
return _context16.stop(); | ||
} | ||
} | ||
}, _callee16, this); | ||
}, _callee15, this); | ||
})); | ||
return function refreshReaderClients(_x24) { | ||
return _ref16.apply(this, arguments); | ||
return _ref15.apply(this, arguments); | ||
}; | ||
@@ -792,41 +770,31 @@ }(); | ||
var postMessage = exports.postMessage = function () { | ||
var _ref19 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee18(channelState, messageJson) { | ||
var _ref18 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee17(channelState, messageJson) { | ||
var _this3 = this; | ||
return _regenerator2['default'].wrap(function _callee18$(_context19) { | ||
return _regenerator2['default'].wrap(function _callee17$(_context18) { | ||
while (1) { | ||
switch (_context19.prev = _context19.next) { | ||
switch (_context18.prev = _context18.next) { | ||
case 0: | ||
_context19.next = 2; | ||
_context18.next = 2; | ||
return channelState.writeQueue.requestIdlePromise(); | ||
case 2: | ||
_context19.next = 4; | ||
return channelState.writeQueue.wrapCall((0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee17() { | ||
var msgObj, pingObj, messages; | ||
return _regenerator2['default'].wrap(function _callee17$(_context18) { | ||
_context18.next = 4; | ||
return channelState.writeQueue.wrapCall((0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee16() { | ||
var msgObj, pingStr, messages; | ||
return _regenerator2['default'].wrap(function _callee16$(_context17) { | ||
while (1) { | ||
switch (_context18.prev = _context18.next) { | ||
switch (_context17.prev = _context17.next) { | ||
case 0: | ||
_context18.next = 2; | ||
_context17.next = 2; | ||
return refreshReaderClients(channelState); | ||
case 2: | ||
_context18.next = 4; | ||
_context17.next = 4; | ||
return writeMessage(channelState.channelName, channelState.uuid, messageJson); | ||
case 4: | ||
msgObj = _context18.sent; | ||
// ping other readers | ||
pingObj = { | ||
a: 'msg', | ||
d: { | ||
t: msgObj.time, | ||
u: msgObj.uuid, | ||
to: msgObj.token | ||
} | ||
}; | ||
_context18.next = 8; | ||
msgObj = _context17.sent; | ||
pingStr = '{"t":' + msgObj.time + ',"u":"' + msgObj.uuid + '","to":"' + msgObj.token + '"}'; | ||
_context17.next = 8; | ||
return Promise.all(Object.values(channelState.otherReaderClients).filter(function (client) { | ||
@@ -836,25 +804,27 @@ return client.writable; | ||
.map(function (client) { | ||
return client.write(JSON.stringify(pingObj)); | ||
return new Promise(function (res) { | ||
client.write(pingStr, res); | ||
}); | ||
})); | ||
case 8: | ||
if (!((0, _util2.randomInt)(0, 10) === 0)) { | ||
_context18.next = 14; | ||
if (!((0, _util2.randomInt)(0, 50) === 0)) { | ||
_context17.next = 13; | ||
break; | ||
} | ||
_context18.next = 11; | ||
_context17.next = 11; | ||
return getAllMessages(channelState.channelName); | ||
case 11: | ||
messages = _context18.sent; | ||
_context18.next = 14; | ||
return cleanOldMessages(messages, channelState.options.node.ttl); | ||
messages = _context17.sent; | ||
case 14: | ||
/*await*/cleanOldMessages(messages, channelState.options.node.ttl); | ||
case 13: | ||
case 'end': | ||
return _context18.stop(); | ||
return _context17.stop(); | ||
} | ||
} | ||
}, _callee17, _this3); | ||
}, _callee16, _this3); | ||
})) | ||
@@ -868,10 +838,10 @@ | ||
case 'end': | ||
return _context19.stop(); | ||
return _context18.stop(); | ||
} | ||
} | ||
}, _callee18, this); | ||
}, _callee17, this); | ||
})); | ||
return function postMessage(_x27, _x28) { | ||
return _ref19.apply(this, arguments); | ||
return _ref18.apply(this, arguments); | ||
}; | ||
@@ -881,13 +851,13 @@ }(); | ||
var close = exports.close = function () { | ||
var _ref21 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee19(channelState) { | ||
return _regenerator2['default'].wrap(function _callee19$(_context20) { | ||
var _ref20 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee18(channelState) { | ||
return _regenerator2['default'].wrap(function _callee18$(_context19) { | ||
while (1) { | ||
switch (_context20.prev = _context20.next) { | ||
switch (_context19.prev = _context19.next) { | ||
case 0: | ||
if (!channelState.closed) { | ||
_context20.next = 2; | ||
_context19.next = 2; | ||
break; | ||
} | ||
return _context20.abrupt('return'); | ||
return _context19.abrupt('return'); | ||
@@ -908,6 +878,5 @@ case 2: | ||
channelState.socketEE.emitter.removeAllListeners(); | ||
channelState.readQueue.clear(); | ||
channelState.writeQueue.clear(); | ||
_context20.next = 10; | ||
_context19.next = 9; | ||
return unlink(channelState.infoFilePath)['catch'](function () { | ||
@@ -917,3 +886,3 @@ return null; | ||
case 10: | ||
case 9: | ||
@@ -924,12 +893,12 @@ Object.values(channelState.otherReaderClients).forEach(function (client) { | ||
case 11: | ||
case 10: | ||
case 'end': | ||
return _context20.stop(); | ||
return _context19.stop(); | ||
} | ||
} | ||
}, _callee19, this); | ||
}, _callee18, this); | ||
})); | ||
return function close(_x30) { | ||
return _ref21.apply(this, arguments); | ||
return _ref20.apply(this, arguments); | ||
}; | ||
@@ -936,0 +905,0 @@ }(); |
{ | ||
"name": "broadcast-channel", | ||
"version": "1.2.3", | ||
"version": "1.2.4", | ||
"description": "A BroadcastChannel implementation that works with new browsers, older browsers and Node.js", | ||
@@ -37,2 +37,3 @@ "homepage": "https://github.com/pubkey/broadcast-channel#readme", | ||
"test:typings": "npm run build && mocha ./test/typings.test.js -b --timeout 12000 --exit", | ||
"test:performance": "npm run build && mocha ./test/performance.test.js -b --timeout 24000 --exit", | ||
"size:prewebpack": "npm run build && cross-env NODE_ENV=build webpack --config ./config/webpack.config.js", | ||
@@ -39,0 +40,0 @@ "size:webpack": "npm run size:prewebpack && echo \"Build-Size Webpack (minified+gzip):\" && gzip-size --raw ./test_tmp/webpack.bundle.js", |
25
perf.txt
@@ -68,2 +68,25 @@ BEFORE: | ||
BEFORE: 3795 | ||
AFTER: 3110 | ||
AFTER: 3110 | ||
----------------------------------------- | ||
14.July.2018: test:performance | ||
before: { | ||
"openClose": 1589.1032320000231, | ||
"sendRecieve": { | ||
"parallel": 8576.14631400071, | ||
"series": 8902.407701000571 | ||
} | ||
} | ||
after: { | ||
"openClose": 1606.3578069992363, | ||
"sendRecieve": { | ||
"parallel": 6627.974293999374, | ||
"series": 5202.203781999648 | ||
} | ||
} | ||
----------------------------------------- |
@@ -309,3 +309,2 @@ /** | ||
// ensures we do not read messages in parrallel | ||
const readQueue = new IdleQueue(1); | ||
const writeQueue = new IdleQueue(1); | ||
@@ -323,3 +322,2 @@ | ||
messagesCallback: null, | ||
readQueue, | ||
writeQueue, | ||
@@ -338,6 +336,3 @@ otherReaderUuids, | ||
const obj = JSON.parse(data); | ||
if (obj.a === 'msg') { | ||
handleMessagePing(state, obj.d); | ||
return; | ||
} | ||
handleMessagePing(state, obj); | ||
}); | ||
@@ -359,40 +354,34 @@ | ||
await state.readQueue.requestIdlePromise(); | ||
await state.readQueue.wrapCall( | ||
async () => { | ||
let messages; | ||
if (!msgObj) { | ||
// get all | ||
messages = await getAllMessages(state.channelName); | ||
} else { | ||
// get single message | ||
messages = [ | ||
getSingleMessage(state.channelName, msgObj) | ||
]; | ||
} | ||
let messages; | ||
if (!msgObj) { | ||
// get all | ||
messages = await getAllMessages(state.channelName); | ||
} else { | ||
// get single message | ||
messages = [ | ||
getSingleMessage(state.channelName, msgObj) | ||
]; | ||
} | ||
const useMessages = messages | ||
.filter(msgObj => msgObj.senderUuid !== state.uuid) // not send by own | ||
.filter(msgObj => !state.emittedMessagesIds.has(msgObj.token)) // not already emitted | ||
.filter(msgObj => msgObj.time >= state.messagesCallbackTime) // not older then onMessageCallback | ||
.sort((msgObjA, msgObjB) => msgObjA.time - msgObjB.time); // sort by time | ||
const useMessages = messages | ||
.filter(msgObj => msgObj.senderUuid !== state.uuid) // not send by own | ||
.filter(msgObj => !state.emittedMessagesIds.has(msgObj.token)) // not already emitted | ||
.filter(msgObj => msgObj.time >= state.messagesCallbackTime) // not older then onMessageCallback | ||
.sort((msgObjA, msgObjB) => msgObjA.time - msgObjB.time); // sort by time | ||
if (state.messagesCallback) { | ||
for (const msgObj of useMessages) { | ||
const content = await readMessage(msgObj); | ||
state.emittedMessagesIds.add(msgObj.token); | ||
setTimeout( | ||
() => state.emittedMessagesIds.delete(msgObj.token), | ||
state.options.node.ttl * 2 | ||
); | ||
if (state.messagesCallback) { | ||
for (const msgObj of useMessages) { | ||
const content = await readMessage(msgObj); | ||
state.emittedMessagesIds.add(msgObj.token); | ||
setTimeout( | ||
() => state.emittedMessagesIds.delete(msgObj.token), | ||
state.options.node.ttl * 2 | ||
); | ||
if (state.messagesCallback) { | ||
state.messagesCallback(content.data); | ||
} | ||
} | ||
state.messagesCallback(content.data); | ||
} | ||
} | ||
); | ||
} | ||
} | ||
@@ -444,15 +433,12 @@ | ||
// ping other readers | ||
const pingObj = { | ||
a: 'msg', | ||
d: { | ||
t: msgObj.time, | ||
u: msgObj.uuid, | ||
to: msgObj.token | ||
} | ||
}; | ||
const pingStr = '{"t":' + msgObj.time + ',"u":"' + msgObj.uuid + '","to":"' + msgObj.token + '"}'; | ||
await Promise.all( | ||
Object.values(channelState.otherReaderClients) | ||
.filter(client => client.writable) // client might have closed in between | ||
.map(client => client.write(JSON.stringify(pingObj))) | ||
.map(client => { | ||
return new Promise(res => { | ||
client.write(pingStr, res); | ||
}); | ||
}) | ||
); | ||
@@ -465,5 +451,5 @@ | ||
*/ | ||
if (randomInt(0, 10) === 0) { | ||
if (randomInt(0, 50) === 0) { | ||
const messages = await getAllMessages(channelState.channelName); | ||
await cleanOldMessages(messages, channelState.options.node.ttl); | ||
/*await*/ cleanOldMessages(messages, channelState.options.node.ttl); | ||
} | ||
@@ -498,3 +484,2 @@ | ||
channelState.socketEE.emitter.removeAllListeners(); | ||
channelState.readQueue.clear(); | ||
channelState.writeQueue.clear(); | ||
@@ -501,0 +486,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
199429
4673