Wrap complex push in trylock

This commit is contained in:
Garrett Mills 2021-04-10 14:35:11 -05:00
parent 1bba0a18ec
commit f072d81b5f
Signed by: garrettmills
GPG Key ID: D2BF5FBA8298F246

View File

@ -20,6 +20,10 @@ export interface FirebaseResourceItem {
export class FirebaseResource<T extends FirebaseResourceItem> extends Iterable<T> {
protected refName!: RTDBRef
get firebase() {
return Application.getApplication().make<FirebaseUnit>(FirebaseUnit)
}
/** Get the Reference for this resource. */
ref(): firebase.database.Reference {
return Application.getApplication().make<FirebaseUnit>(FirebaseUnit).ref(this.refName)
@ -75,6 +79,8 @@ export class FirebaseResource<T extends FirebaseResourceItem> extends Iterable<T
* @param item
*/
async push(item: T): Promise<T> {
await this.firebase.trylock(this.refName)
await this.ref().transaction((collection) => {
if ( !collection ) collection = []
item.seqID = this.findNextId(collection)
@ -97,6 +103,7 @@ export class FirebaseResource<T extends FirebaseResourceItem> extends Iterable<T
})
})
await this.firebase.unlock(this.refName)
return item
}