You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1.2 KiB
TypeScript
42 lines
1.2 KiB
TypeScript
let latestTimestampOffset = Number.MAX_SAFE_INTEGER;
|
|
|
|
/**
|
|
* This function has no use to be called except during tests.
|
|
* @returns the current offset we have with the rest of the network.
|
|
*/
|
|
function getLatestTimestampOffset() {
|
|
if (latestTimestampOffset === Number.MAX_SAFE_INTEGER) {
|
|
window.log.debug('latestTimestampOffset is not set yet');
|
|
return 0;
|
|
}
|
|
// window.log.info('latestTimestampOffset is ', latestTimestampOffset);
|
|
|
|
return latestTimestampOffset;
|
|
}
|
|
|
|
function setLatestTimestampOffset(newOffset: number) {
|
|
latestTimestampOffset = newOffset;
|
|
if (latestTimestampOffset === Number.MAX_SAFE_INTEGER) {
|
|
window?.log?.info(`first timestamp offset received: ${newOffset}ms`);
|
|
}
|
|
latestTimestampOffset = newOffset;
|
|
}
|
|
|
|
function now() {
|
|
// make sure to call exports here, as we stub the exported one for testing.
|
|
return Date.now() - NetworkTime.getLatestTimestampOffset();
|
|
}
|
|
|
|
function getNowWithNetworkOffsetSeconds() {
|
|
// make sure to call exports here, as we stub the exported one for testing.
|
|
|
|
return Math.floor(NetworkTime.now() / 1000);
|
|
}
|
|
|
|
export const NetworkTime = {
|
|
getNowWithNetworkOffsetSeconds,
|
|
getLatestTimestampOffset,
|
|
now,
|
|
setLatestTimestampOffset,
|
|
};
|