When implementing Knockout in TypeScript, the following code is used:
class timeHelpers {
private static createTimeBasedObservable(updatePeriodInMillis: number) {
const nowTime: KnockoutObservable<moment.Moment> = ko.observable<moment.Moment>();
let interval: number;
const computed = ko.pureComputed(() => nowTime());
computed.subscribe(() => {
nowTime(moment.utc());
interval = setInterval(() => nowTime(moment.utc()), updatePeriodInMillis);
}, this, "awake");
computed.subscribe(() => {
clearInterval(interval);
interval = undefined;
}, this, "asleep");
return computed;
}
static readonly utcNowWithMinutePrecision = timeHelpers.createTimeBasedObservable(60 * 1000);
static readonly utcNowWithSecondPrecision = timeHelpers.createTimeBasedObservable(1000);
export = timeHelpers;
}
An issue arises with consecutive calls to production server values that are not always sequential.
...
const now = timeHelpers.utcNowWithSecondPrecision();
now.add(moment.duration(this.serverTimeDifference()));
// this.serverTimeDifference() is some numerical value
...
This inconsistency can be observed in the console printout link provided below: