Skip to content
This repository has been archived by the owner on Feb 16, 2023. It is now read-only.

Commit

Permalink
fix: updatedAt works as expected
Browse files Browse the repository at this point in the history
  • Loading branch information
villetakanen committed Dec 14, 2021
1 parent 9ec133f commit 1e1c079
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 30 deletions.
24 changes: 11 additions & 13 deletions src/state/store/Storable.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { logDebug } from '@/utils/eventLogger'
import { DocumentData, Timestamp, serverTimestamp } from '@firebase/firestore'

/**
Expand Down Expand Up @@ -51,10 +52,10 @@ export class Storable implements StorableModel {
* @returns {number} The time of the last change to this entity, or any related entity in milliseconds since the epoch.
*/
get flowTime (): number {
if (this._flowTime) return this._flowTime.toMillis()
if (this._updated) return this._updated.toMillis()
if (this._created) return this._created.toMillis()
return 0
if (this._flowTime && this._flowTime !== null) return Math.floor(this._flowTime.toMillis())
if (this._updated) return Math.floor(this._updated.toMillis())
if (this._created) return Math.floor(this._created.toMillis())
return 0 // No flowTime or flowtime===null, no updatedAt, no createdAt, so return 0
}

get createdAt (): Timestamp|undefined {
Expand All @@ -79,26 +80,23 @@ export class Storable implements StorableModel {
*/
get docData (): DocumentData {
const data: DocumentData = {}
data.created = this._created || serverTimestamp()
data.updated = serverTimestamp()
data.createdAt = this._created || serverTimestamp()
data.updatedAt = serverTimestamp()

// We will not inject the flowTime to entities that do not use flowtime.
if (this._flowTime) data.flowTime = this._flowTime
return data
}

set docData (doc: DocumentData) {
if (doc.created) this._created = doc.created
if (doc.updated) this._updated = doc.updated
if (doc.flowTime) this._flowTime = (doc.flowTime as Timestamp)
if (doc.createdAt) this._created = doc.createdAt as Timestamp
if (doc.updatedAt) this._updated = doc.updatedAt as Timestamp
if (doc.flowTime) this._flowTime = doc.flowTime as Timestamp
}

compareFlowTime (other:StorableModel): number {
if (other.id === this.id) return 0 // They are the same entity, return 0
if (other.flowTime > this.flowTime) {
return -1
}
return 1
return this.flowTime > other.flowTime ? -1 : 1
}

compareChangeTime (other:StorableModel): number {
Expand Down
18 changes: 6 additions & 12 deletions src/state/store/Thread.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
import { DocumentData } from '@firebase/firestore'
import { Storable, StorableDoc, StorableModel } from './Storable'

export interface ThreadDoc extends StorableDoc{
name?: string
author?: string
replyCount?: number
lovedCount?: number
seenCount?: number
hidden?: boolean
content?: string
}
import { Storable, StorableModel } from './Storable'

export interface ThreadModel extends StorableModel{
name: string
Expand All @@ -19,6 +9,7 @@ export interface ThreadModel extends StorableModel{
seenCount: number
hidden: boolean
content: string
site?: string
}

export class Thread extends Storable implements StorableModel {
Expand All @@ -29,7 +20,9 @@ export class Thread extends Storable implements StorableModel {
seenCount = 0
hidden = true
content = '<p><br/><p>'
constructor (thread: ThreadDoc|string, doc?: ThreadDoc) {
site:string|undefined // the id of a game or site linked to this thread

constructor (thread: DocumentData|string, doc?: DocumentData) {
super(thread, doc)
const t = typeof thread === 'string' ? doc || { id: thread } : thread
this.docData = t
Expand All @@ -50,5 +43,6 @@ export class Thread extends Storable implements StorableModel {
this.seenCount = doc.seenCount || 0
this.hidden = !!doc.hidden
this.content = doc.content || '<p><br/><p>'
this.site = doc.site || undefined
}
}
9 changes: 6 additions & 3 deletions tests/unit/Storable.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ describe('Storable', () => {
it('Should be comparable to another Storable with a flowTime', () => {
const storable1 = new Storable('id1')
const storable2 = new Storable('id2')
storable1.docData = { updated: new Timestamp(1, 1) }
storable2.docData = { updated: new Timestamp(2, 2) }
expect(storable1.compareFlowTime(storable2)).toBe(-1)
storable1.docData = { updatedAt: new Timestamp(1, 1) }
storable2.docData = { updatedAt: new Timestamp(2, 2) }
expect(storable1.flowTime).toBe(1000)
expect(storable2.flowTime).toBe(2000)
expect(storable1.compareFlowTime(storable2)).toBe(1)
expect(storable2.compareFlowTime(storable1)).toBe(-1)
})
})
6 changes: 4 additions & 2 deletions tests/unit/Thread.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import { Timestamp, DocumentData } from '@firebase/firestore'
const example:DocumentData = {
id: 'example',
author: 'exampleAutor',
created: new Timestamp(0, 0),
updated: new Timestamp(1, 1),
createdAt: new Timestamp(0, 0),
updatedAt: new Timestamp(1, 1),
flowTime: new Timestamp(2, 2),
replyCount: 2,
lovedCount: 3,
seenCount: 4,
hidden: false,
content: '<h1>exampleContent</h1>',
site: 'exampleSite',
}
describe('Thread', () => {
it('should be a valid Thread', () => {
Expand All @@ -26,5 +27,6 @@ describe('Thread', () => {
expect(thread.seenCount).toBe(4)
expect(thread.hidden).toBe(false)
expect(thread.content).toBe('<h1>exampleContent</h1>')
expect(thread.site).toBe('exampleSite')
})
})

0 comments on commit 1e1c079

Please sign in to comment.