This repository has been archived by the owner on Feb 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: storable class, and site class cleared
- Loading branch information
1 parent
d74611e
commit 8fb7322
Showing
3 changed files
with
96 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,61 @@ | ||
import { DocumentData, Timestamp, serverTimestamp } from '@firebase/firestore' | ||
|
||
export interface StorableDoc { | ||
id: string | ||
created?: Timestamp | ||
updated?: Timestamp | ||
} | ||
|
||
/** | ||
* A helper superclass for entities storable to Firestone | ||
*/ | ||
export class Storable { | ||
export class Storable implements StorableDoc { | ||
private _id: string | ||
|
||
constructor (id: string) { | ||
this._id = id | ||
private _created: Timestamp|undefined | ||
private _updated: Timestamp|undefined | ||
|
||
constructor (storable: string|StorableDoc, doc?: DocumentData) { | ||
if (typeof storable === 'string') { | ||
this._id = storable | ||
if (doc) this.docData = doc | ||
return | ||
} | ||
this._id = storable?.id || '' | ||
this.docData = storable | ||
} | ||
|
||
get id (): string { | ||
return this._id | ||
} | ||
|
||
get docData(): {} { | ||
console.error('docData getter must be overridden') | ||
return {} | ||
get created (): Timestamp|undefined { | ||
return this._created | ||
} | ||
|
||
get updated (): Timestamp|undefined { | ||
return this._updated | ||
} | ||
|
||
get docData (): DocumentData { | ||
const data: DocumentData = { | ||
created: serverTimestamp(), | ||
updated: serverTimestamp() | ||
} | ||
if (this._created) data.created = this._created | ||
|
||
return data | ||
} | ||
|
||
set docData (doc: DocumentData) { | ||
console.error('docData setter must be overridden') | ||
if (doc.updated) this._updated = doc.updated | ||
} | ||
|
||
compareChangeTime (other:Storable): number { | ||
if (!other.updated) return 1 // other site has no lastUpdate, this is more recent | ||
if (!this.updated) return -1 // this site has no lastUpdate, other is more recent | ||
if (other.updated.toMillis() > this.updated.toMillis()) { | ||
return -1 | ||
} | ||
return 1 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters