Comparing version 1.0.0 to 1.1.0
# Changelog | ||
## 1.1.0 (Jul 14, 2016) | ||
* **Disable automatic updates when tab is hidden**. | ||
* Give external access to the estimated round-trip time (`getRtt()`). | ||
* Improve docs. | ||
## 1.0.0 (Jul 13, 2016) | ||
* First public release. |
@@ -44,3 +44,3 @@ 'use strict'; | ||
type: 'CLOCKSY', | ||
data: clocksy.processRequest(msg.data), | ||
data: clocksy.processRequest(data), | ||
}); | ||
@@ -93,6 +93,9 @@ return; | ||
if (type === 'CLOCKSY') { | ||
const tDelta = clocksy.processResponse(msg.data); | ||
const tDelta = clocksy.processResponse(data); | ||
// tDelta is the estimated server time minus the local time. | ||
// Use this dekta for whatever purpose you want | ||
// (you can also get it later on with clocksy.getDelta()) | ||
// Use this delta for whatever purpose you want, e.g. | ||
// correcting the local time for graphs or changing the timestamps | ||
// of data downloaded from the server... | ||
// If you don't need the delta immediately, you can also obtain it later | ||
// calling clocksy.getDelta()) | ||
return; | ||
@@ -107,2 +110,4 @@ } | ||
function ClocksyClient() { | ||
var _this = this; | ||
var _ref = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
@@ -119,2 +124,3 @@ | ||
this.tDelta = null; | ||
this.rtt = null; | ||
this.timer = null; | ||
@@ -124,2 +130,14 @@ this.alpha = alpha; | ||
this.sendRequest = sendRequest || function () {}; | ||
// Keep track of the tab's shown/hidden status. Chrome | ||
// goes bonkers with the timers in hidden windows, so RTT | ||
// calculations are not reliable. Automatic requests are | ||
// not sent while the tab is hidden. | ||
this.fHiddenTab = false; | ||
try { | ||
this.fHiddenTab = document.hidden; | ||
document.addEventListener('visibilitychange', function () { | ||
_this.fHiddenTab = document.hidden; // change tab text for demo | ||
}); | ||
} catch (err) {/* ignore */} | ||
} | ||
@@ -142,2 +160,3 @@ | ||
this.tDelta = this.calcNewDelta(tDelta); | ||
this.rtt = rtt; | ||
return this.tDelta; | ||
@@ -159,6 +178,11 @@ } | ||
}, { | ||
key: 'getRtt', | ||
value: function getRtt() { | ||
return this.rtt; | ||
} | ||
}, { | ||
key: 'start', | ||
value: function start() { | ||
if (this.timer != null) this.stop(); | ||
this.sendAutoRequest(); | ||
this.sendAutoRequest(true); | ||
this.timer = setInterval(this.sendAutoRequest.bind(this), this.updatePeriod); | ||
@@ -175,3 +199,4 @@ } | ||
key: 'sendAutoRequest', | ||
value: function sendAutoRequest() { | ||
value: function sendAutoRequest(fForce) { | ||
if (this.fHiddenTab && !fForce) return; | ||
var req = this.createRequest(); | ||
@@ -178,0 +203,0 @@ this.sendRequest(req); |
@@ -33,3 +33,3 @@ /*! | ||
type: 'CLOCKSY', | ||
data: clocksy.processRequest(msg.data), | ||
data: clocksy.processRequest(data), | ||
}); | ||
@@ -72,6 +72,9 @@ return; | ||
if (type === 'CLOCKSY') { | ||
const tDelta = clocksy.processResponse(msg.data); | ||
const tDelta = clocksy.processResponse(data); | ||
// tDelta is the estimated server time minus the local time. | ||
// Use this dekta for whatever purpose you want | ||
// (you can also get it later on with clocksy.getDelta()) | ||
// Use this delta for whatever purpose you want, e.g. | ||
// correcting the local time for graphs or changing the timestamps | ||
// of data downloaded from the server... | ||
// If you don't need the delta immediately, you can also obtain it later | ||
// calling clocksy.getDelta()) | ||
return; | ||
@@ -89,2 +92,3 @@ } | ||
this.tDelta = null; | ||
this.rtt = null; | ||
this.timer = null; | ||
@@ -94,2 +98,14 @@ this.alpha = alpha; | ||
this.sendRequest = sendRequest || (() => {}); | ||
// Keep track of the tab's shown/hidden status. Chrome | ||
// goes bonkers with the timers in hidden windows, so RTT | ||
// calculations are not reliable. Automatic requests are | ||
// not sent while the tab is hidden. | ||
this.fHiddenTab = false; | ||
try { | ||
this.fHiddenTab = document.hidden; | ||
document.addEventListener('visibilitychange', () => { | ||
this.fHiddenTab = document.hidden; // change tab text for demo | ||
}); | ||
} catch (err) { /* ignore */ } | ||
} | ||
@@ -107,2 +123,3 @@ | ||
this.tDelta = this.calcNewDelta(tDelta); | ||
this.rtt = rtt; | ||
return this.tDelta; | ||
@@ -118,9 +135,8 @@ } | ||
getDelta() { | ||
return this.tDelta; | ||
} | ||
getDelta() { return this.tDelta; } | ||
getRtt() { return this.rtt; } | ||
start() { | ||
if (this.timer != null) this.stop(); | ||
this.sendAutoRequest(); | ||
this.sendAutoRequest(true); | ||
this.timer = setInterval(this.sendAutoRequest.bind(this), | ||
@@ -136,3 +152,4 @@ this.updatePeriod); | ||
sendAutoRequest() { | ||
sendAutoRequest(fForce) { | ||
if (this.fHiddenTab && !fForce) return; | ||
const req = this.createRequest(); | ||
@@ -139,0 +156,0 @@ this.sendRequest(req); |
{ | ||
"name": "clocksy", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Transport-agnostic client-server clock synchronization", | ||
@@ -10,5 +10,9 @@ "main": "lib/index.js", | ||
"clock", | ||
"time", | ||
"synchronization", | ||
"sync", | ||
"rtt" | ||
"rtt", | ||
"client", | ||
"server", | ||
"ntp" | ||
], | ||
@@ -15,0 +19,0 @@ "homepage": "https://github.com/guigrpa/clocksy#readme", |
@@ -8,5 +8,7 @@ # clocksy [![npm version](https://img.shields.io/npm/v/clocksy.svg)](https://www.npmjs.com/package/clocksy) | ||
* Simplicity | ||
* Automatic requests, typically converging to few ms | ||
* Transport-agnostic: use it with HTTP, WebSockets, MQTT, whatever. | ||
* **Simple algorithm**: timestamp at the client, timestamp at the server and send back, measure round-trip time, estimate server time and apply an [IIR filter](https://en.wikipedia.org/wiki/Infinite_impulse_response) to improve accuracy over time. | ||
* **Transport-agnostic**: use it on top of your own transport layer: HTTP, WebSockets, MQTT, whatever. | ||
* **Simple implementation**: since it leaves transport on the user's hands, it is exactly 5 LOCs for the server, ~70 for the client. | ||
* **Automatic requests**, typically converging to < 10 ms in a few iterations. | ||
* **Automatic background tab detection**: Chrome goes bonkers with –sorry, optimizes– timers on background tabs, interfering with clocksy's algorithm. Automatic clock updates are switched off while a tab is hidden. | ||
@@ -38,3 +40,3 @@ | ||
type: 'CLOCKSY', | ||
data: clocksy.processRequest(msg.data), | ||
data: clocksy.processRequest(data), | ||
}); | ||
@@ -67,6 +69,9 @@ return; | ||
if (type === 'CLOCKSY') { | ||
const tDelta = clocksy.processResponse(msg.data); | ||
const tDelta = clocksy.processResponse(data); | ||
// tDelta is the estimated server time minus the local time. | ||
// Use this dekta for whatever purpose you want | ||
// (you can also get it later on with clocksy.getDelta()) | ||
// Use this delta for whatever purpose you want, e.g. | ||
// correcting the local time for graphs or changing the timestamps | ||
// of data downloaded from the server... | ||
// If you don't need the delta immediately, you can also obtain it later | ||
// calling clocksy.getDelta()) | ||
return; | ||
@@ -78,2 +83,7 @@ } | ||
## Related | ||
* [timesync](https://github.com/enmasseio/timesync): provides more functionalities, but is apparently more complex. | ||
## [Changelog](https://github.com/guigrpa/clocksy/blob/master/CHANGELOG.md) | ||
@@ -80,0 +90,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
22351
318
97