1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-06-18 23:07:45 +02:00

Improved ntp time

This commit is contained in:
Laurent Cozic
2020-09-09 00:34:27 +01:00
parent 1f70a76c7e
commit f41ba67e15
3 changed files with 16 additions and 6 deletions

View File

@ -1,7 +1,7 @@
const ntpClient = require('lib/vendor/ntp-client');
const Mutex = require('async-mutex').Mutex;
let lastSyncTime = 0;
let nextSyncTime = 0;
let timeOffset = 0;
const fetchingTimeMutex = new Mutex();
@ -24,7 +24,7 @@ async function networkTime():Promise<Date> {
}
function shouldSyncTime() {
return !lastSyncTime || Date.now() - lastSyncTime >= 5 * 1000;
return !nextSyncTime || Date.now() > nextSyncTime;
}
export default async function():Promise<Date> {
@ -34,9 +34,14 @@ export default async function():Promise<Date> {
try {
if (shouldSyncTime()) {
const date = await networkTime();
lastSyncTime = Date.now();
nextSyncTime = Date.now() + 60 * 1000;
timeOffset = date.getTime() - Date.now();
}
} catch (error) {
// Fallback to application time since
// most of the time it's actually correct
nextSyncTime = Date.now() + 20 * 1000;
timeOffset = 0;
} finally {
release();
}