Skip to content

Commit 1f05a0f

Browse files
committed
4.9.0
1 parent c872596 commit 1f05a0f

File tree

13 files changed

+155
-36
lines changed

13 files changed

+155
-36
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# CHANGELOG
22

3-
## X.X.X (comming soon)
3+
## 4.9.0 (23 December 2021)
44

55
Bugfixes:
66
- When listening to messages directly, responses that where send directly after `addEventListener()` where missing because of inaccurate JavaScript timing.

dist/es5node/broadcast-channel.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,21 @@ function _startListening(channel) {
260260
if (!channel._iL && _hasMessageListeners(channel)) {
261261
// someone is listening, start subscribing
262262
var listenerFn = function listenerFn(msgObj) {
263-
channel._addEL[msgObj.type].forEach(function (obj) {
264-
if (msgObj.time >= obj.time) {
265-
obj.fn(msgObj.data);
263+
channel._addEL[msgObj.type].forEach(function (listenerObject) {
264+
/**
265+
* Getting the current time in JavaScript has no good precision.
266+
* So instead of only listening to events that happend 'after' the listener
267+
* was added, we also listen to events that happended 100ms before it.
268+
* This ensures that when another process, like a WebWorker, sends events
269+
* we do not miss them out because their timestamp is a bit off compared to the main process.
270+
* Not doing this would make messages missing when we send data directly after subscribing and awaiting a response.
271+
* @link https://johnresig.com/blog/accuracy-of-javascript-time/
272+
*/
273+
var hundredMsInMicro = 100 * 1000;
274+
var minMessageTime = listenerObject.time - hundredMsInMicro;
275+
276+
if (msgObj.time >= minMessageTime) {
277+
listenerObject.fn(msgObj.data);
266278
}
267279
});
268280
};

dist/esbrowser/broadcast-channel.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,21 @@ function _startListening(channel) {
242242
if (!channel._iL && _hasMessageListeners(channel)) {
243243
// someone is listening, start subscribing
244244
var listenerFn = function listenerFn(msgObj) {
245-
channel._addEL[msgObj.type].forEach(function (obj) {
246-
if (msgObj.time >= obj.time) {
247-
obj.fn(msgObj.data);
245+
channel._addEL[msgObj.type].forEach(function (listenerObject) {
246+
/**
247+
* Getting the current time in JavaScript has no good precision.
248+
* So instead of only listening to events that happend 'after' the listener
249+
* was added, we also listen to events that happended 100ms before it.
250+
* This ensures that when another process, like a WebWorker, sends events
251+
* we do not miss them out because their timestamp is a bit off compared to the main process.
252+
* Not doing this would make messages missing when we send data directly after subscribing and awaiting a response.
253+
* @link https://johnresig.com/blog/accuracy-of-javascript-time/
254+
*/
255+
var hundredMsInMicro = 100 * 1000;
256+
var minMessageTime = listenerObject.time - hundredMsInMicro;
257+
258+
if (msgObj.time >= minMessageTime) {
259+
listenerObject.fn(msgObj.data);
248260
}
249261
});
250262
};

dist/esnode/broadcast-channel.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,21 @@ function _startListening(channel) {
242242
if (!channel._iL && _hasMessageListeners(channel)) {
243243
// someone is listening, start subscribing
244244
var listenerFn = function listenerFn(msgObj) {
245-
channel._addEL[msgObj.type].forEach(function (obj) {
246-
if (msgObj.time >= obj.time) {
247-
obj.fn(msgObj.data);
245+
channel._addEL[msgObj.type].forEach(function (listenerObject) {
246+
/**
247+
* Getting the current time in JavaScript has no good precision.
248+
* So instead of only listening to events that happend 'after' the listener
249+
* was added, we also listen to events that happended 100ms before it.
250+
* This ensures that when another process, like a WebWorker, sends events
251+
* we do not miss them out because their timestamp is a bit off compared to the main process.
252+
* Not doing this would make messages missing when we send data directly after subscribing and awaiting a response.
253+
* @link https://johnresig.com/blog/accuracy-of-javascript-time/
254+
*/
255+
var hundredMsInMicro = 100 * 1000;
256+
var minMessageTime = listenerObject.time - hundredMsInMicro;
257+
258+
if (msgObj.time >= minMessageTime) {
259+
listenerObject.fn(msgObj.data);
248260
}
249261
});
250262
};

dist/lib/broadcast-channel.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,21 @@ function _startListening(channel) {
260260
if (!channel._iL && _hasMessageListeners(channel)) {
261261
// someone is listening, start subscribing
262262
var listenerFn = function listenerFn(msgObj) {
263-
channel._addEL[msgObj.type].forEach(function (obj) {
264-
if (msgObj.time >= obj.time) {
265-
obj.fn(msgObj.data);
263+
channel._addEL[msgObj.type].forEach(function (listenerObject) {
264+
/**
265+
* Getting the current time in JavaScript has no good precision.
266+
* So instead of only listening to events that happend 'after' the listener
267+
* was added, we also listen to events that happended 100ms before it.
268+
* This ensures that when another process, like a WebWorker, sends events
269+
* we do not miss them out because their timestamp is a bit off compared to the main process.
270+
* Not doing this would make messages missing when we send data directly after subscribing and awaiting a response.
271+
* @link https://johnresig.com/blog/accuracy-of-javascript-time/
272+
*/
273+
var hundredMsInMicro = 100 * 1000;
274+
var minMessageTime = listenerObject.time - hundredMsInMicro;
275+
276+
if (msgObj.time >= minMessageTime) {
277+
listenerObject.fn(msgObj.data);
266278
}
267279
});
268280
};

dist/lib/browser.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,21 @@ function _startListening(channel) {
261261
if (!channel._iL && _hasMessageListeners(channel)) {
262262
// someone is listening, start subscribing
263263
var listenerFn = function listenerFn(msgObj) {
264-
channel._addEL[msgObj.type].forEach(function (obj) {
265-
if (msgObj.time >= obj.time) {
266-
obj.fn(msgObj.data);
264+
channel._addEL[msgObj.type].forEach(function (listenerObject) {
265+
/**
266+
* Getting the current time in JavaScript has no good precision.
267+
* So instead of only listening to events that happend 'after' the listener
268+
* was added, we also listen to events that happended 100ms before it.
269+
* This ensures that when another process, like a WebWorker, sends events
270+
* we do not miss them out because their timestamp is a bit off compared to the main process.
271+
* Not doing this would make messages missing when we send data directly after subscribing and awaiting a response.
272+
* @link https://johnresig.com/blog/accuracy-of-javascript-time/
273+
*/
274+
var hundredMsInMicro = 100 * 1000;
275+
var minMessageTime = listenerObject.time - hundredMsInMicro;
276+
277+
if (msgObj.time >= minMessageTime) {
278+
listenerObject.fn(msgObj.data);
267279
}
268280
});
269281
};

dist/lib/browser.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/e2e.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,21 @@ function _startListening(channel) {
261261
if (!channel._iL && _hasMessageListeners(channel)) {
262262
// someone is listening, start subscribing
263263
var listenerFn = function listenerFn(msgObj) {
264-
channel._addEL[msgObj.type].forEach(function (obj) {
265-
if (msgObj.time >= obj.time) {
266-
obj.fn(msgObj.data);
264+
channel._addEL[msgObj.type].forEach(function (listenerObject) {
265+
/**
266+
* Getting the current time in JavaScript has no good precision.
267+
* So instead of only listening to events that happend 'after' the listener
268+
* was added, we also listen to events that happended 100ms before it.
269+
* This ensures that when another process, like a WebWorker, sends events
270+
* we do not miss them out because their timestamp is a bit off compared to the main process.
271+
* Not doing this would make messages missing when we send data directly after subscribing and awaiting a response.
272+
* @link https://johnresig.com/blog/accuracy-of-javascript-time/
273+
*/
274+
var hundredMsInMicro = 100 * 1000;
275+
var minMessageTime = listenerObject.time - hundredMsInMicro;
276+
277+
if (msgObj.time >= minMessageTime) {
278+
listenerObject.fn(msgObj.data);
267279
}
268280
});
269281
};
@@ -12374,7 +12386,6 @@ function run() {
1237412386
rand = new Date().getTime();
1237512387

1237612388
channel.onmessage = function (msg) {
12377-
console.log('main: recieved msg' + JSON.stringify(msg));
1237812389
answerPool[msg.from] = msg;
1237912390
var textnode = document.createTextNode(JSON.stringify(msg) + '</br>');
1238012391
msgContainer.appendChild(textnode);
@@ -12670,7 +12681,9 @@ function run() {
1267012681
return true;
1267112682
})]).then(function (errored) {
1267212683
if (errored) {
12673-
throw new Error('timed out for msgId ' + msgId);
12684+
var errorMessage = 'ERROR startWorkerTest() timed out for msgId ' + msgId;
12685+
console.error(errorMessage);
12686+
throw new Error(errorMessage);
1267412687
}
1267512688
});
1267612689

docs/iframe.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,21 @@ function _startListening(channel) {
261261
if (!channel._iL && _hasMessageListeners(channel)) {
262262
// someone is listening, start subscribing
263263
var listenerFn = function listenerFn(msgObj) {
264-
channel._addEL[msgObj.type].forEach(function (obj) {
265-
if (msgObj.time >= obj.time) {
266-
obj.fn(msgObj.data);
264+
channel._addEL[msgObj.type].forEach(function (listenerObject) {
265+
/**
266+
* Getting the current time in JavaScript has no good precision.
267+
* So instead of only listening to events that happend 'after' the listener
268+
* was added, we also listen to events that happended 100ms before it.
269+
* This ensures that when another process, like a WebWorker, sends events
270+
* we do not miss them out because their timestamp is a bit off compared to the main process.
271+
* Not doing this would make messages missing when we send data directly after subscribing and awaiting a response.
272+
* @link https://johnresig.com/blog/accuracy-of-javascript-time/
273+
*/
274+
var hundredMsInMicro = 100 * 1000;
275+
var minMessageTime = listenerObject.time - hundredMsInMicro;
276+
277+
if (msgObj.time >= minMessageTime) {
278+
listenerObject.fn(msgObj.data);
267279
}
268280
});
269281
};

docs/index.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,21 @@ function _startListening(channel) {
261261
if (!channel._iL && _hasMessageListeners(channel)) {
262262
// someone is listening, start subscribing
263263
var listenerFn = function listenerFn(msgObj) {
264-
channel._addEL[msgObj.type].forEach(function (obj) {
265-
if (msgObj.time >= obj.time) {
266-
obj.fn(msgObj.data);
264+
channel._addEL[msgObj.type].forEach(function (listenerObject) {
265+
/**
266+
* Getting the current time in JavaScript has no good precision.
267+
* So instead of only listening to events that happend 'after' the listener
268+
* was added, we also listen to events that happended 100ms before it.
269+
* This ensures that when another process, like a WebWorker, sends events
270+
* we do not miss them out because their timestamp is a bit off compared to the main process.
271+
* Not doing this would make messages missing when we send data directly after subscribing and awaiting a response.
272+
* @link https://johnresig.com/blog/accuracy-of-javascript-time/
273+
*/
274+
var hundredMsInMicro = 100 * 1000;
275+
var minMessageTime = listenerObject.time - hundredMsInMicro;
276+
277+
if (msgObj.time >= minMessageTime) {
278+
listenerObject.fn(msgObj.data);
267279
}
268280
});
269281
};

0 commit comments

Comments
 (0)