Skip to content

Commit

Permalink
7.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
pubkey committed Nov 27, 2023
1 parent a278986 commit 5b0f7aa
Show file tree
Hide file tree
Showing 29 changed files with 171 additions and 204 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

## X.X.X (comming soon)

## 7.0.0 (27 November 2023)

- CHANGE do not emit messages that have been existed before the channel was created.

## 6.0.0 (30 October 2023)

- ADD support for the Deno runtime
Expand Down
13 changes: 1 addition & 12 deletions dist/es5node/broadcast-channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,18 +237,7 @@ function _startListening(channel) {

var listenerFn = function listenerFn(msgObj) {
channel._addEL[msgObj.type].forEach(function (listenerObject) {
/**
* Getting the current time in JavaScript has no good precision.
* So instead of only listening to events that happened 'after' the listener
* was added, we also listen to events that happened 100ms before it.
* This ensures that when another process, like a WebWorker, sends events
* we do not miss them out because their timestamp is a bit off compared to the main process.
* Not doing this would make messages missing when we send data directly after subscribing and awaiting a response.
* @link https://johnresig.com/blog/accuracy-of-javascript-time/
*/
var hundredMsInMicro = 100 * 1000;
var minMessageTime = listenerObject.time - hundredMsInMicro;
if (msgObj.time >= minMessageTime) {
if (msgObj.time >= listenerObject.time) {
listenerObject.fn(msgObj.data);
}
});
Expand Down
5 changes: 3 additions & 2 deletions dist/es5node/methods/native.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ var microSeconds = exports.microSeconds = _util.microSeconds;
var type = exports.type = 'native';
function create(channelName) {
var state = {
time: (0, _util.microSeconds)(),
messagesCallback: null,
bc: new BroadcastChannel(channelName),
subFns: [] // subscriberFunctions
};

state.bc.onmessage = function (msg) {
state.bc.onmessage = function (msgEvent) {
if (state.messagesCallback) {
state.messagesCallback(msg.data);
state.messagesCallback(msgEvent.data);
}
};
return state;
Expand Down
10 changes: 6 additions & 4 deletions dist/es5node/methods/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,11 +440,13 @@ function _openClientConnection() {
return _openClientConnection.apply(this, arguments);
}
function writeMessage(channelName, readerUuid, messageJson, paths) {
var time = messageJson.time;
if (!time) {
time = microSeconds();
}
paths = paths || getPaths(channelName);
var time = microSeconds();
var writeObject = {
uuid: readerUuid,
time: time,
data: messageJson
};
var token = (0, _util2.randomToken)();
Expand Down Expand Up @@ -687,8 +689,8 @@ function _filterMessage(msgObj, state) {
if (msgObj.senderUuid === state.uuid) return false; // not send by own
if (state.emittedMessagesIds.has(msgObj.token)) return false; // not already emitted
if (!state.messagesCallback) return false; // no listener
if (msgObj.time < state.messagesCallbackTime) return false; // not older then onMessageCallback
if (msgObj.time < state.time) return false; // msgObj is older then channel
if (msgObj.time < state.messagesCallbackTime) return false; // not older than onMessageCallback
if (msgObj.time < state.time) return false; // msgObj is older than channel

state.emittedMessagesIds.add(msgObj.token);
return true;
Expand Down
27 changes: 16 additions & 11 deletions dist/es5node/methods/simulate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.SimulateMethod = void 0;
exports.SimulateMethod = exports.SIMULATE_DELAY_TIME = void 0;
exports.averageResponseTime = averageResponseTime;
exports.canBeUsed = canBeUsed;
exports.close = close;
Expand All @@ -18,6 +18,7 @@ var type = exports.type = 'simulate';
var SIMULATE_CHANNELS = new Set();
function create(channelName) {
var state = {
time: microSeconds(),
name: channelName,
messagesCallback: null
};
Expand All @@ -27,21 +28,25 @@ function create(channelName) {
function close(channelState) {
SIMULATE_CHANNELS["delete"](channelState);
}
var SIMULATE_DELAY_TIME = exports.SIMULATE_DELAY_TIME = 5;
function postMessage(channelState, messageJson) {
return new Promise(function (res) {
return setTimeout(function () {
var channelArray = Array.from(SIMULATE_CHANNELS);
channelArray.filter(function (channel) {
return channel.name === channelState.name;
}).filter(function (channel) {
return channel !== channelState;
}).filter(function (channel) {
return !!channel.messagesCallback;
}).forEach(function (channel) {
return channel.messagesCallback(messageJson);
channelArray.forEach(function (channel) {
if (channel.name === channelState.name &&
// has same name
channel !== channelState &&
// not own channel
!!channel.messagesCallback &&
// has subscribers
channel.time < messageJson.time // channel not created after postMessage() call
) {
channel.messagesCallback(messageJson);
}
});
res();
}, 5);
}, SIMULATE_DELAY_TIME);
});
}
function onMessage(channelState, fn) {
Expand All @@ -51,7 +56,7 @@ function canBeUsed() {
return true;
}
function averageResponseTime() {
return 5;
return SIMULATE_DELAY_TIME;
}
var SimulateMethod = exports.SimulateMethod = {
create: create,
Expand Down
16 changes: 6 additions & 10 deletions dist/es5node/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,21 @@ function randomToken() {
return Math.random().toString(36).substring(2);
}
var lastMs = 0;
var additional = 0;

/**
* returns the current time in micro-seconds,
* Returns the current unix time in micro-seconds,
* WARNING: This is a pseudo-function
* Performance.now is not reliable in webworkers, so we just make sure to never return the same time.
* This is enough in browsers, and this function will not be used in nodejs.
* The main reason for this hack is to ensure that BroadcastChannel behaves equal to production when it is used in fast-running unit tests.
*/
function microSeconds() {
var ms = Date.now();
if (ms === lastMs) {
additional++;
return ms * 1000 + additional;
} else {
lastMs = ms;
additional = 0;
return ms * 1000;
var ret = Date.now() * 1000; // milliseconds to microseconds
if (ret <= lastMs) {
ret = lastMs + 1;
}
lastMs = ret;
return ret;
}

/**
Expand Down
13 changes: 1 addition & 12 deletions dist/esbrowser/broadcast-channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,18 +230,7 @@ function _startListening(channel) {

var listenerFn = function listenerFn(msgObj) {
channel._addEL[msgObj.type].forEach(function (listenerObject) {
/**
* Getting the current time in JavaScript has no good precision.
* So instead of only listening to events that happened 'after' the listener
* was added, we also listen to events that happened 100ms before it.
* This ensures that when another process, like a WebWorker, sends events
* we do not miss them out because their timestamp is a bit off compared to the main process.
* Not doing this would make messages missing when we send data directly after subscribing and awaiting a response.
* @link https://johnresig.com/blog/accuracy-of-javascript-time/
*/
var hundredMsInMicro = 100 * 1000;
var minMessageTime = listenerObject.time - hundredMsInMicro;
if (msgObj.time >= minMessageTime) {
if (msgObj.time >= listenerObject.time) {
listenerObject.fn(msgObj.data);
}
});
Expand Down
5 changes: 3 additions & 2 deletions dist/esbrowser/methods/native.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ export var microSeconds = micro;
export var type = 'native';
export function create(channelName) {
var state = {
time: micro(),
messagesCallback: null,
bc: new BroadcastChannel(channelName),
subFns: [] // subscriberFunctions
};

state.bc.onmessage = function (msg) {
state.bc.onmessage = function (msgEvent) {
if (state.messagesCallback) {
state.messagesCallback(msg.data);
state.messagesCallback(msgEvent.data);
}
};
return state;
Expand Down
10 changes: 6 additions & 4 deletions dist/esbrowser/methods/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,11 +407,13 @@ function _openClientConnection() {
return _openClientConnection.apply(this, arguments);
}
export function writeMessage(channelName, readerUuid, messageJson, paths) {
var time = messageJson.time;
if (!time) {
time = microSeconds();
}
paths = paths || getPaths(channelName);
var time = microSeconds();
var writeObject = {
uuid: readerUuid,
time: time,
data: messageJson
};
var token = randomToken();
Expand Down Expand Up @@ -654,8 +656,8 @@ export function _filterMessage(msgObj, state) {
if (msgObj.senderUuid === state.uuid) return false; // not send by own
if (state.emittedMessagesIds.has(msgObj.token)) return false; // not already emitted
if (!state.messagesCallback) return false; // no listener
if (msgObj.time < state.messagesCallbackTime) return false; // not older then onMessageCallback
if (msgObj.time < state.time) return false; // msgObj is older then channel
if (msgObj.time < state.messagesCallbackTime) return false; // not older than onMessageCallback
if (msgObj.time < state.time) return false; // msgObj is older than channel

state.emittedMessagesIds.add(msgObj.token);
return true;
Expand Down
25 changes: 15 additions & 10 deletions dist/esbrowser/methods/simulate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export var type = 'simulate';
var SIMULATE_CHANNELS = new Set();
export function create(channelName) {
var state = {
time: microSeconds(),
name: channelName,
messagesCallback: null
};
Expand All @@ -13,21 +14,25 @@ export function create(channelName) {
export function close(channelState) {
SIMULATE_CHANNELS["delete"](channelState);
}
export var SIMULATE_DELAY_TIME = 5;
export function postMessage(channelState, messageJson) {
return new Promise(function (res) {
return setTimeout(function () {
var channelArray = Array.from(SIMULATE_CHANNELS);
channelArray.filter(function (channel) {
return channel.name === channelState.name;
}).filter(function (channel) {
return channel !== channelState;
}).filter(function (channel) {
return !!channel.messagesCallback;
}).forEach(function (channel) {
return channel.messagesCallback(messageJson);
channelArray.forEach(function (channel) {
if (channel.name === channelState.name &&
// has same name
channel !== channelState &&
// not own channel
!!channel.messagesCallback &&
// has subscribers
channel.time < messageJson.time // channel not created after postMessage() call
) {
channel.messagesCallback(messageJson);
}
});
res();
}, 5);
}, SIMULATE_DELAY_TIME);
});
}
export function onMessage(channelState, fn) {
Expand All @@ -37,7 +42,7 @@ export function canBeUsed() {
return true;
}
export function averageResponseTime() {
return 5;
return SIMULATE_DELAY_TIME;
}
export var SimulateMethod = {
create: create,
Expand Down
16 changes: 6 additions & 10 deletions dist/esbrowser/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,21 @@ export function randomToken() {
return Math.random().toString(36).substring(2);
}
var lastMs = 0;
var additional = 0;

/**
* returns the current time in micro-seconds,
* Returns the current unix time in micro-seconds,
* WARNING: This is a pseudo-function
* Performance.now is not reliable in webworkers, so we just make sure to never return the same time.
* This is enough in browsers, and this function will not be used in nodejs.
* The main reason for this hack is to ensure that BroadcastChannel behaves equal to production when it is used in fast-running unit tests.
*/
export function microSeconds() {
var ms = Date.now();
if (ms === lastMs) {
additional++;
return ms * 1000 + additional;
} else {
lastMs = ms;
additional = 0;
return ms * 1000;
var ret = Date.now() * 1000; // milliseconds to microseconds
if (ret <= lastMs) {
ret = lastMs + 1;
}
lastMs = ret;
return ret;
}

/**
Expand Down
13 changes: 1 addition & 12 deletions dist/esnode/broadcast-channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,18 +230,7 @@ function _startListening(channel) {

var listenerFn = function listenerFn(msgObj) {
channel._addEL[msgObj.type].forEach(function (listenerObject) {
/**
* Getting the current time in JavaScript has no good precision.
* So instead of only listening to events that happened 'after' the listener
* was added, we also listen to events that happened 100ms before it.
* This ensures that when another process, like a WebWorker, sends events
* we do not miss them out because their timestamp is a bit off compared to the main process.
* Not doing this would make messages missing when we send data directly after subscribing and awaiting a response.
* @link https://johnresig.com/blog/accuracy-of-javascript-time/
*/
var hundredMsInMicro = 100 * 1000;
var minMessageTime = listenerObject.time - hundredMsInMicro;
if (msgObj.time >= minMessageTime) {
if (msgObj.time >= listenerObject.time) {
listenerObject.fn(msgObj.data);
}
});
Expand Down
5 changes: 3 additions & 2 deletions dist/esnode/methods/native.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ export var microSeconds = micro;
export var type = 'native';
export function create(channelName) {
var state = {
time: micro(),
messagesCallback: null,
bc: new BroadcastChannel(channelName),
subFns: [] // subscriberFunctions
};

state.bc.onmessage = function (msg) {
state.bc.onmessage = function (msgEvent) {
if (state.messagesCallback) {
state.messagesCallback(msg.data);
state.messagesCallback(msgEvent.data);
}
};
return state;
Expand Down
10 changes: 6 additions & 4 deletions dist/esnode/methods/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,11 +407,13 @@ function _openClientConnection() {
return _openClientConnection.apply(this, arguments);
}
export function writeMessage(channelName, readerUuid, messageJson, paths) {
var time = messageJson.time;
if (!time) {
time = microSeconds();
}
paths = paths || getPaths(channelName);
var time = microSeconds();
var writeObject = {
uuid: readerUuid,
time: time,
data: messageJson
};
var token = randomToken();
Expand Down Expand Up @@ -654,8 +656,8 @@ export function _filterMessage(msgObj, state) {
if (msgObj.senderUuid === state.uuid) return false; // not send by own
if (state.emittedMessagesIds.has(msgObj.token)) return false; // not already emitted
if (!state.messagesCallback) return false; // no listener
if (msgObj.time < state.messagesCallbackTime) return false; // not older then onMessageCallback
if (msgObj.time < state.time) return false; // msgObj is older then channel
if (msgObj.time < state.messagesCallbackTime) return false; // not older than onMessageCallback
if (msgObj.time < state.time) return false; // msgObj is older than channel

state.emittedMessagesIds.add(msgObj.token);
return true;
Expand Down
Loading

0 comments on commit 5b0f7aa

Please sign in to comment.