Skip to content

Commit

Permalink
fix time backward
Browse files Browse the repository at this point in the history
  • Loading branch information
wighawag committed Nov 29, 2023
1 parent 2c731f6 commit 0009ff8
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions web/src/lib/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import {writable} from 'svelte/store';
import {connection, devProvider} from './web3';

let timestamp = Math.floor(Date.now() / 1000);
let lastFetchLocalTime = performance.now();

let maxRead = 0;
let synced = false;
let lastFetchLocalTime = performance.now();
let contract: `0x${string}` | undefined;

async function getTime() {
Expand Down Expand Up @@ -86,7 +87,14 @@ export const time = {
subscribe: _time.subscribe,
get now() {
let n = performance.now();
return timestamp + Math.floor((n - lastFetchLocalTime) / 1000);
const v = timestamp + Math.floor((n - lastFetchLocalTime) / 1000);
if (v < maxRead) {
return maxRead;
}
if (synced) {
maxRead = v;
}
return v;
},
setTimeKeeperContract(contractAddress: `0x${string}`) {
contract = contractAddress;
Expand Down

0 comments on commit 0009ff8

Please sign in to comment.