broadcast-channel
Advanced tools
Comparing version 2.1.2 to 2.1.3
@@ -91,7 +91,9 @@ import { sleep, randomToken } from '../util.js'; | ||
awaitLeadership: function awaitLeadership() { | ||
if (!this._awaitLeadershipPromise) { | ||
this._awaitLeadershipPromise = _awaitLeadershipOnce(this); | ||
if ( | ||
/* _awaitLeadershipPromise */ | ||
!this._aLP) { | ||
this._aLP = _awaitLeadershipOnce(this); | ||
} | ||
return this._awaitLeadershipPromise; | ||
return this._aLP; | ||
}, | ||
@@ -98,0 +100,0 @@ die: function die() { |
@@ -150,3 +150,2 @@ /** | ||
options = fillOptionsWithDefaults(options); | ||
var uuid = randomToken(10); | ||
return createDatabase(channelName).then(function (db) { | ||
@@ -158,5 +157,10 @@ var state = { | ||
options: options, | ||
uuid: uuid, | ||
// contains all messages that have been emitted before | ||
emittedMessagesIds: new ObliviousSet(options.idb.ttl * 2), | ||
uuid: randomToken(10), | ||
/** | ||
* emittedMessagesIds | ||
* contains all messages that have been emitted before | ||
* @type {ObliviousSet} | ||
*/ | ||
eMIs: new ObliviousSet(options.idb.ttl * 2), | ||
// ensures we do not read messages in parrallel | ||
@@ -192,3 +196,3 @@ writeBlockPromise: Promise.resolve(), | ||
if (state.emittedMessagesIds.has(msgObj.id)) return false; // already emitted | ||
if (state.eMIs.has(msgObj.id)) return false; // already emitted | ||
@@ -224,3 +228,3 @@ if (msgObj.data.time < state.messagesCallbackTime) return false; // older then onMessageCallback | ||
if (state.messagesCallback) { | ||
state.emittedMessagesIds.add(msgObj.id); | ||
state.eMIs.add(msgObj.id); | ||
state.messagesCallback(msgObj.data); | ||
@@ -227,0 +231,0 @@ } |
@@ -91,12 +91,15 @@ /** | ||
var startTime = new Date().getTime(); | ||
var uuid = randomToken(10); // contains all messages that have been emitted before | ||
var uuid = randomToken(10); | ||
/** | ||
* eMIs | ||
* contains all messages that have been emitted before | ||
* @type {ObliviousSet} | ||
*/ | ||
var emittedMessagesIds = new ObliviousSet(options.localstorage.removeTimeout); | ||
var eMIs = new ObliviousSet(options.localstorage.removeTimeout); | ||
var state = { | ||
startTime: startTime, | ||
channelName: channelName, | ||
options: options, | ||
uuid: uuid, | ||
emittedMessagesIds: emittedMessagesIds | ||
eMIs: eMIs // emittedMessagesIds | ||
}; | ||
@@ -108,7 +111,7 @@ state.listener = addStorageEventListener(channelName, function (msgObj) { | ||
if (!msgObj.token || emittedMessagesIds.has(msgObj.token)) return; // already emitted | ||
if (!msgObj.token || eMIs.has(msgObj.token)) return; // already emitted | ||
if (msgObj.data.time && msgObj.data.time < state.messagesCallbackTime) return; // too old | ||
emittedMessagesIds.add(msgObj.token); | ||
eMIs.add(msgObj.token); | ||
state.messagesCallback(msgObj.data); | ||
@@ -115,0 +118,0 @@ }); |
import isNode from 'detect-node'; | ||
import { randomToken, microSeconds as micro } from '../util'; | ||
import { microSeconds as micro } from '../util'; | ||
export var microSeconds = micro; | ||
export var type = 'native'; | ||
export function create(channelName, options) { | ||
if (!options) options = {}; | ||
export function create(channelName) { | ||
var state = { | ||
uuid: randomToken(10), | ||
channelName: channelName, | ||
options: options, | ||
messagesCallback: null, | ||
bc: new BroadcastChannel(channelName), | ||
subscriberFunctions: [] | ||
subFns: [] // subscriberFunctions | ||
}; | ||
@@ -26,3 +23,3 @@ | ||
channelState.bc.close(); | ||
channelState.subscriberFunctions = []; | ||
channelState.subFns = []; | ||
} | ||
@@ -29,0 +26,0 @@ export function postMessage(channelState, messageJson) { |
/** | ||
* | ||
* | ||
* | ||
* | ||
*/ | ||
@@ -13,21 +13,2 @@ var ObliviousSet = function ObliviousSet(ttl) { | ||
ObliviousSet.prototype = { | ||
_removeTooOldValues: function _removeTooOldValues() { | ||
var olderThen = now() - this.ttl; | ||
var iterator = this.set[Symbol.iterator](); | ||
while (true) { | ||
var value = iterator.next().value; | ||
if (!value) return; // no more elements | ||
var time = this.timeMap.get(value); | ||
if (time < olderThen) { | ||
this.timeMap["delete"](value); | ||
this.set["delete"](value); | ||
} else { | ||
// we reached a value that is not old enough | ||
return; | ||
} | ||
} | ||
}, | ||
add: function add(value) { | ||
@@ -37,3 +18,3 @@ this.timeMap.set(value, now()); | ||
this._removeTooOldValues(); | ||
_removeTooOldValues(this); | ||
}, | ||
@@ -45,3 +26,22 @@ clear: function clear() { | ||
}; | ||
export function _removeTooOldValues(obliviousSet) { | ||
var olderThen = now() - obliviousSet.ttl; | ||
var iterator = obliviousSet.set[Symbol.iterator](); | ||
while (true) { | ||
var value = iterator.next().value; | ||
if (!value) return; // no more elements | ||
var time = obliviousSet.timeMap.get(value); | ||
if (time < olderThen) { | ||
obliviousSet.timeMap["delete"](value); | ||
obliviousSet.set["delete"](value); | ||
} else { | ||
// we reached a value that is not old enough | ||
return; | ||
} | ||
} | ||
} | ||
function now() { | ||
@@ -48,0 +48,0 @@ return new Date().getTime(); |
@@ -102,7 +102,9 @@ "use strict"; | ||
awaitLeadership: function awaitLeadership() { | ||
if (!this._awaitLeadershipPromise) { | ||
this._awaitLeadershipPromise = _awaitLeadershipOnce(this); | ||
if ( | ||
/* _awaitLeadershipPromise */ | ||
!this._aLP) { | ||
this._aLP = _awaitLeadershipOnce(this); | ||
} | ||
return this._awaitLeadershipPromise; | ||
return this._aLP; | ||
}, | ||
@@ -109,0 +111,0 @@ die: function die() { |
@@ -188,3 +188,2 @@ "use strict"; | ||
options = (0, _options.fillOptionsWithDefaults)(options); | ||
var uuid = (0, _util.randomToken)(10); | ||
return createDatabase(channelName).then(function (db) { | ||
@@ -196,5 +195,10 @@ var state = { | ||
options: options, | ||
uuid: uuid, | ||
// contains all messages that have been emitted before | ||
emittedMessagesIds: new _obliviousSet["default"](options.idb.ttl * 2), | ||
uuid: (0, _util.randomToken)(10), | ||
/** | ||
* emittedMessagesIds | ||
* contains all messages that have been emitted before | ||
* @type {ObliviousSet} | ||
*/ | ||
eMIs: new _obliviousSet["default"](options.idb.ttl * 2), | ||
// ensures we do not read messages in parrallel | ||
@@ -230,3 +234,3 @@ writeBlockPromise: Promise.resolve(), | ||
if (state.emittedMessagesIds.has(msgObj.id)) return false; // already emitted | ||
if (state.eMIs.has(msgObj.id)) return false; // already emitted | ||
@@ -262,3 +266,3 @@ if (msgObj.data.time < state.messagesCallbackTime) return false; // older then onMessageCallback | ||
if (state.messagesCallback) { | ||
state.emittedMessagesIds.add(msgObj.id); | ||
state.eMIs.add(msgObj.id); | ||
state.messagesCallback(msgObj.data); | ||
@@ -265,0 +269,0 @@ } |
@@ -122,12 +122,15 @@ "use strict"; | ||
var startTime = new Date().getTime(); | ||
var uuid = (0, _util.randomToken)(10); // contains all messages that have been emitted before | ||
var uuid = (0, _util.randomToken)(10); | ||
/** | ||
* eMIs | ||
* contains all messages that have been emitted before | ||
* @type {ObliviousSet} | ||
*/ | ||
var emittedMessagesIds = new _obliviousSet["default"](options.localstorage.removeTimeout); | ||
var eMIs = new _obliviousSet["default"](options.localstorage.removeTimeout); | ||
var state = { | ||
startTime: startTime, | ||
channelName: channelName, | ||
options: options, | ||
uuid: uuid, | ||
emittedMessagesIds: emittedMessagesIds | ||
eMIs: eMIs // emittedMessagesIds | ||
}; | ||
@@ -139,7 +142,7 @@ state.listener = addStorageEventListener(channelName, function (msgObj) { | ||
if (!msgObj.token || emittedMessagesIds.has(msgObj.token)) return; // already emitted | ||
if (!msgObj.token || eMIs.has(msgObj.token)) return; // already emitted | ||
if (msgObj.data.time && msgObj.data.time < state.messagesCallbackTime) return; // too old | ||
emittedMessagesIds.add(msgObj.token); | ||
eMIs.add(msgObj.token); | ||
state.messagesCallback(msgObj.data); | ||
@@ -146,0 +149,0 @@ }); |
@@ -25,11 +25,8 @@ "use strict"; | ||
function create(channelName, options) { | ||
if (!options) options = {}; | ||
function create(channelName) { | ||
var state = { | ||
uuid: (0, _util.randomToken)(10), | ||
channelName: channelName, | ||
options: options, | ||
messagesCallback: null, | ||
bc: new BroadcastChannel(channelName), | ||
subscriberFunctions: [] | ||
subFns: [] // subscriberFunctions | ||
}; | ||
@@ -48,3 +45,3 @@ | ||
channelState.bc.close(); | ||
channelState.subscriberFunctions = []; | ||
channelState.subFns = []; | ||
} | ||
@@ -51,0 +48,0 @@ |
@@ -6,7 +6,8 @@ "use strict"; | ||
}); | ||
exports._removeTooOldValues = _removeTooOldValues; | ||
exports["default"] = void 0; | ||
/** | ||
* | ||
* | ||
* | ||
* | ||
*/ | ||
@@ -21,21 +22,2 @@ var ObliviousSet = function ObliviousSet(ttl) { | ||
ObliviousSet.prototype = { | ||
_removeTooOldValues: function _removeTooOldValues() { | ||
var olderThen = now() - this.ttl; | ||
var iterator = this.set[Symbol.iterator](); | ||
while (true) { | ||
var value = iterator.next().value; | ||
if (!value) return; // no more elements | ||
var time = this.timeMap.get(value); | ||
if (time < olderThen) { | ||
this.timeMap["delete"](value); | ||
this.set["delete"](value); | ||
} else { | ||
// we reached a value that is not old enough | ||
return; | ||
} | ||
} | ||
}, | ||
add: function add(value) { | ||
@@ -45,3 +27,3 @@ this.timeMap.set(value, now()); | ||
this._removeTooOldValues(); | ||
_removeTooOldValues(this); | ||
}, | ||
@@ -54,2 +36,22 @@ clear: function clear() { | ||
function _removeTooOldValues(obliviousSet) { | ||
var olderThen = now() - obliviousSet.ttl; | ||
var iterator = obliviousSet.set[Symbol.iterator](); | ||
while (true) { | ||
var value = iterator.next().value; | ||
if (!value) return; // no more elements | ||
var time = obliviousSet.timeMap.get(value); | ||
if (time < olderThen) { | ||
obliviousSet.timeMap["delete"](value); | ||
obliviousSet.set["delete"](value); | ||
} else { | ||
// we reached a value that is not old enough | ||
return; | ||
} | ||
} | ||
} | ||
function now() { | ||
@@ -56,0 +58,0 @@ return new Date().getTime(); |
{ | ||
"name": "broadcast-channel", | ||
"version": "2.1.2", | ||
"version": "2.1.3", | ||
"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", |
@@ -14,3 +14,2 @@ import { | ||
const BroadcastChannel = function(name, options) { | ||
@@ -17,0 +16,0 @@ this.name = name; |
@@ -88,6 +88,9 @@ import { | ||
awaitLeadership() { | ||
if (!this._awaitLeadershipPromise) { | ||
this._awaitLeadershipPromise = _awaitLeadershipOnce(this); | ||
if ( | ||
/* _awaitLeadershipPromise */ | ||
!this._aLP | ||
) { | ||
this._aLP = _awaitLeadershipOnce(this); | ||
} | ||
return this._awaitLeadershipPromise; | ||
return this._aLP; | ||
}, | ||
@@ -94,0 +97,0 @@ |
@@ -165,4 +165,2 @@ /** | ||
const uuid = randomToken(10); | ||
return createDatabase(channelName).then(db => { | ||
@@ -174,5 +172,9 @@ const state = { | ||
options, | ||
uuid, | ||
// contains all messages that have been emitted before | ||
emittedMessagesIds: new ObliviousSet(options.idb.ttl * 2), | ||
uuid: randomToken(10), | ||
/** | ||
* emittedMessagesIds | ||
* contains all messages that have been emitted before | ||
* @type {ObliviousSet} | ||
*/ | ||
eMIs: new ObliviousSet(options.idb.ttl * 2), | ||
// ensures we do not read messages in parrallel | ||
@@ -207,3 +209,3 @@ writeBlockPromise: Promise.resolve(), | ||
if (msgObj.uuid === state.uuid) return false; // send by own | ||
if (state.emittedMessagesIds.has(msgObj.id)) return false; // already emitted | ||
if (state.eMIs.has(msgObj.id)) return false; // already emitted | ||
if (msgObj.data.time < state.messagesCallbackTime) return false; // older then onMessageCallback | ||
@@ -237,3 +239,3 @@ return true; | ||
if (state.messagesCallback) { | ||
state.emittedMessagesIds.add(msgObj.id); | ||
state.eMIs.add(msgObj.id); | ||
state.messagesCallback(msgObj.data); | ||
@@ -240,0 +242,0 @@ } |
@@ -103,14 +103,15 @@ /** | ||
const startTime = new Date().getTime(); | ||
const uuid = randomToken(10); | ||
// contains all messages that have been emitted before | ||
const emittedMessagesIds = new ObliviousSet(options.localstorage.removeTimeout); | ||
/** | ||
* eMIs | ||
* contains all messages that have been emitted before | ||
* @type {ObliviousSet} | ||
*/ | ||
const eMIs = new ObliviousSet(options.localstorage.removeTimeout); | ||
const state = { | ||
startTime, | ||
channelName, | ||
options, | ||
uuid, | ||
emittedMessagesIds | ||
eMIs // emittedMessagesIds | ||
}; | ||
@@ -124,6 +125,6 @@ | ||
if (msgObj.uuid === uuid) return; // own message | ||
if (!msgObj.token || emittedMessagesIds.has(msgObj.token)) return; // already emitted | ||
if (!msgObj.token || eMIs.has(msgObj.token)) return; // already emitted | ||
if (msgObj.data.time && msgObj.data.time < state.messagesCallbackTime) return; // too old | ||
emittedMessagesIds.add(msgObj.token); | ||
eMIs.add(msgObj.token); | ||
state.messagesCallback(msgObj.data); | ||
@@ -130,0 +131,0 @@ } |
import isNode from 'detect-node'; | ||
import { | ||
randomToken, | ||
microSeconds as micro | ||
@@ -12,11 +11,7 @@ } from '../util'; | ||
export function create(channelName, options) { | ||
if (!options) options = {}; | ||
export function create(channelName) { | ||
const state = { | ||
uuid: randomToken(10), | ||
channelName, | ||
options, | ||
messagesCallback: null, | ||
bc: new BroadcastChannel(channelName), | ||
subscriberFunctions: [] | ||
subFns: [] // subscriberFunctions | ||
}; | ||
@@ -35,3 +30,3 @@ | ||
channelState.bc.close(); | ||
channelState.subscriberFunctions = []; | ||
channelState.subFns = []; | ||
} | ||
@@ -38,0 +33,0 @@ |
/** | ||
* | ||
* | ||
* | ||
* | ||
*/ | ||
const ObliviousSet = function (ttl) { | ||
const ObliviousSet = function(ttl) { | ||
this.ttl = ttl; | ||
@@ -15,26 +15,8 @@ this.set = new Set(); | ||
ObliviousSet.prototype = { | ||
_removeTooOldValues: function () { | ||
const olderThen = now() - this.ttl; | ||
const iterator = this.set[Symbol.iterator](); | ||
while (true) { | ||
const value = iterator.next().value; | ||
if (!value) return; // no more elements | ||
const time = this.timeMap.get(value); | ||
if (time < olderThen) { | ||
this.timeMap.delete(value); | ||
this.set.delete(value); | ||
} else { | ||
// we reached a value that is not old enough | ||
return; | ||
} | ||
} | ||
}, | ||
add: function (value) { | ||
add: function(value) { | ||
this.timeMap.set(value, now()); | ||
this.set.add(value); | ||
this._removeTooOldValues(); | ||
_removeTooOldValues(this); | ||
}, | ||
clear: function () { | ||
clear: function() { | ||
this.set.clear(); | ||
@@ -45,2 +27,20 @@ this.timeMap.clear(); | ||
export function _removeTooOldValues(obliviousSet) { | ||
const olderThen = now() - obliviousSet.ttl; | ||
const iterator = obliviousSet.set[Symbol.iterator](); | ||
while (true) { | ||
const value = iterator.next().value; | ||
if (!value) return; // no more elements | ||
const time = obliviousSet.timeMap.get(value); | ||
if (time < olderThen) { | ||
obliviousSet.timeMap.delete(value); | ||
obliviousSet.set.delete(value); | ||
} else { | ||
// we reached a value that is not old enough | ||
return; | ||
} | ||
} | ||
} | ||
function now() { | ||
@@ -50,2 +50,2 @@ return new Date().getTime(); | ||
export default ObliviousSet; | ||
export default ObliviousSet; |
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
5662
203960