Timestamp.now()

This commit is contained in:
Daniel Holmgren 2022-02-18 13:33:22 -06:00
parent 6aed1a5758
commit 2625a18f87
4 changed files with 15 additions and 10 deletions

@ -3,9 +3,14 @@ export class Timestamp {
time: number
clockid: number
constructor(time?: number, clockid?: number) {
this.time = time || Date.now()
this.clockid = clockid || Math.floor(Math.random() * 1024)
constructor(time: number, clockid: number) {
this.time = time
this.clockid = clockid
}
static now(): Timestamp {
const clockid = Math.floor(Math.random() * 1024)
return new Timestamp(Date.now(), clockid)
}
static parse(str: string): Timestamp {

@ -113,7 +113,7 @@ export class UserStore implements UserStoreI {
async addPost (text: string): Promise<Timestamp> {
const user = await this.getUser()
const timestamp = new Timestamp
const timestamp = Timestamp.now()
const post = {
id: timestamp.toString(),

@ -24,12 +24,12 @@ test.beforeEach(async t => {
test("splitting tables", async t => {
const { branch, cid } = t.context as Context
for(let i=0; i < 100; i++) {
await branch.addEntry(new Timestamp(), cid)
await branch.addEntry(Timestamp.now(), cid)
await wait(1)
}
t.is(branch.keys().length, 1, "Does not split at 100 entries")
await branch.addEntry(new Timestamp(), cid)
await branch.addEntry(Timestamp.now(), cid)
t.is(branch.keys().length, 2, "Does split at 101 entries")
})
@ -37,11 +37,11 @@ test("compressing tables", async t => {
const { branch, cid } = t.context as Context
for (let i=0; i < 400; i++) {
await branch.addEntry(new Timestamp(), cid)
await branch.addEntry(Timestamp.now(), cid)
await wait(1)
}
t.is(branch.keys().length, 4, "Does not compress at 4 tables")
await branch.addEntry(new Timestamp(), cid)
await branch.addEntry(Timestamp.now(), cid)
t.is(branch.keys().length, 2, "Compresses oldest 4 tables once there are 5 tables")
})

@ -2,14 +2,14 @@ import test from 'ava'
import Timestamp from '../src/timestamp.js'
test('Creates new timestamp', t => {
const timestamp = new Timestamp()
const timestamp = Timestamp.now()
const str = timestamp.toString()
t.is(typeof str, 'string', 'Is a string')
t.is(str.toString().length, 11, 'Is the proper length')
})
test('Can parse a timestamp', t => {
const timestamp = new Timestamp()
const timestamp = Timestamp.now()
const str = timestamp.toString()
const parsed = Timestamp.parse(str)
t.is(parsed.time, timestamp.time, 'Preserves time')