Wrap complex push in trylock

working-state
Garrett Mills 3 years ago
parent 1bba0a18ec
commit f072d81b5f
Signed by: garrettmills
GPG Key ID: D2BF5FBA8298F246

@ -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
}

Loading…
Cancel
Save