Skip to content

Commit

Permalink
4.9.0
Browse files Browse the repository at this point in the history
  • Loading branch information
pubkey committed Dec 23, 2021
1 parent c872596 commit 1f05a0f
Show file tree
Hide file tree
Showing 13 changed files with 155 additions and 36 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# CHANGELOG

## X.X.X (comming soon)
## 4.9.0 (23 December 2021)

Bugfixes:
- When listening to messages directly, responses that where send directly after `addEventListener()` where missing because of inaccurate JavaScript timing.
Expand Down
18 changes: 15 additions & 3 deletions dist/es5node/broadcast-channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,21 @@ function _startListening(channel) {
if (!channel._iL && _hasMessageListeners(channel)) {
// someone is listening, start subscribing
var listenerFn = function listenerFn(msgObj) {
channel._addEL[msgObj.type].forEach(function (obj) {
if (msgObj.time >= obj.time) {
obj.fn(msgObj.data);
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 happend 'after' the listener
* was added, we also listen to events that happended 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) {
listenerObject.fn(msgObj.data);
}
});
};
Expand Down
18 changes: 15 additions & 3 deletions dist/esbrowser/broadcast-channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,21 @@ function _startListening(channel) {
if (!channel._iL && _hasMessageListeners(channel)) {
// someone is listening, start subscribing
var listenerFn = function listenerFn(msgObj) {
channel._addEL[msgObj.type].forEach(function (obj) {
if (msgObj.time >= obj.time) {
obj.fn(msgObj.data);
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 happend 'after' the listener
* was added, we also listen to events that happended 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) {
listenerObject.fn(msgObj.data);
}
});
};
Expand Down
18 changes: 15 additions & 3 deletions dist/esnode/broadcast-channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,21 @@ function _startListening(channel) {
if (!channel._iL && _hasMessageListeners(channel)) {
// someone is listening, start subscribing
var listenerFn = function listenerFn(msgObj) {
channel._addEL[msgObj.type].forEach(function (obj) {
if (msgObj.time >= obj.time) {
obj.fn(msgObj.data);
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 happend 'after' the listener
* was added, we also listen to events that happended 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) {
listenerObject.fn(msgObj.data);
}
});
};
Expand Down
18 changes: 15 additions & 3 deletions dist/lib/broadcast-channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,21 @@ function _startListening(channel) {
if (!channel._iL && _hasMessageListeners(channel)) {
// someone is listening, start subscribing
var listenerFn = function listenerFn(msgObj) {
channel._addEL[msgObj.type].forEach(function (obj) {
if (msgObj.time >= obj.time) {
obj.fn(msgObj.data);
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 happend 'after' the listener
* was added, we also listen to events that happended 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) {
listenerObject.fn(msgObj.data);
}
});
};
Expand Down
18 changes: 15 additions & 3 deletions dist/lib/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,21 @@ function _startListening(channel) {
if (!channel._iL && _hasMessageListeners(channel)) {
// someone is listening, start subscribing
var listenerFn = function listenerFn(msgObj) {
channel._addEL[msgObj.type].forEach(function (obj) {
if (msgObj.time >= obj.time) {
obj.fn(msgObj.data);
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 happend 'after' the listener
* was added, we also listen to events that happended 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) {
listenerObject.fn(msgObj.data);
}
});
};
Expand Down
2 changes: 1 addition & 1 deletion dist/lib/browser.min.js

Large diffs are not rendered by default.

23 changes: 18 additions & 5 deletions docs/e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,21 @@ function _startListening(channel) {
if (!channel._iL && _hasMessageListeners(channel)) {
// someone is listening, start subscribing
var listenerFn = function listenerFn(msgObj) {
channel._addEL[msgObj.type].forEach(function (obj) {
if (msgObj.time >= obj.time) {
obj.fn(msgObj.data);
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 happend 'after' the listener
* was added, we also listen to events that happended 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) {
listenerObject.fn(msgObj.data);
}
});
};
Expand Down Expand Up @@ -12374,7 +12386,6 @@ function run() {
rand = new Date().getTime();

channel.onmessage = function (msg) {
console.log('main: recieved msg' + JSON.stringify(msg));
answerPool[msg.from] = msg;
var textnode = document.createTextNode(JSON.stringify(msg) + '</br>');
msgContainer.appendChild(textnode);
Expand Down Expand Up @@ -12670,7 +12681,9 @@ function run() {
return true;
})]).then(function (errored) {
if (errored) {
throw new Error('timed out for msgId ' + msgId);
var errorMessage = 'ERROR startWorkerTest() timed out for msgId ' + msgId;
console.error(errorMessage);
throw new Error(errorMessage);
}
});

Expand Down
18 changes: 15 additions & 3 deletions docs/iframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,21 @@ function _startListening(channel) {
if (!channel._iL && _hasMessageListeners(channel)) {
// someone is listening, start subscribing
var listenerFn = function listenerFn(msgObj) {
channel._addEL[msgObj.type].forEach(function (obj) {
if (msgObj.time >= obj.time) {
obj.fn(msgObj.data);
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 happend 'after' the listener
* was added, we also listen to events that happended 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) {
listenerObject.fn(msgObj.data);
}
});
};
Expand Down
18 changes: 15 additions & 3 deletions docs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,21 @@ function _startListening(channel) {
if (!channel._iL && _hasMessageListeners(channel)) {
// someone is listening, start subscribing
var listenerFn = function listenerFn(msgObj) {
channel._addEL[msgObj.type].forEach(function (obj) {
if (msgObj.time >= obj.time) {
obj.fn(msgObj.data);
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 happend 'after' the listener
* was added, we also listen to events that happended 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) {
listenerObject.fn(msgObj.data);
}
});
};
Expand Down
18 changes: 15 additions & 3 deletions docs/leader-iframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,21 @@ function _startListening(channel) {
if (!channel._iL && _hasMessageListeners(channel)) {
// someone is listening, start subscribing
var listenerFn = function listenerFn(msgObj) {
channel._addEL[msgObj.type].forEach(function (obj) {
if (msgObj.time >= obj.time) {
obj.fn(msgObj.data);
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 happend 'after' the listener
* was added, we also listen to events that happended 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) {
listenerObject.fn(msgObj.data);
}
});
};
Expand Down
18 changes: 15 additions & 3 deletions docs/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,21 @@ function _startListening(channel) {
if (!channel._iL && _hasMessageListeners(channel)) {
// someone is listening, start subscribing
var listenerFn = function listenerFn(msgObj) {
channel._addEL[msgObj.type].forEach(function (obj) {
if (msgObj.time >= obj.time) {
obj.fn(msgObj.data);
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 happend 'after' the listener
* was added, we also listen to events that happended 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) {
listenerObject.fn(msgObj.data);
}
});
};
Expand Down
2 changes: 0 additions & 2 deletions test/scripts/e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ function run() {
const rand = new Date().getTime();

channel.onmessage = function (msg) {
console.log('main: recieved msg' + JSON.stringify(msg));

answerPool[msg.from] = msg;
var textnode = document.createTextNode(JSON.stringify(msg) + '</br>');
msgContainer.appendChild(textnode);
Expand Down

0 comments on commit 1f05a0f

Please sign in to comment.