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