Skip to content

Commit

Permalink
feat(GeoTransaction): stub GeoTransaction class
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelSolati committed Jan 23, 2019
1 parent ca387c0 commit 0b68729
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/GeoFirestore.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { GeoFirestoreTypes } from './GeoFirestoreTypes';
import { GeoCollectionReference } from './GeoCollectionReference';
import { GeoTransaction } from './GeoTransaction';
import { GeoWriteBatch } from './GeoWriteBatch';

/**
Expand Down Expand Up @@ -33,4 +34,19 @@ export class GeoFirestore {
public collection(collectionPath: string): GeoCollectionReference {
return new GeoCollectionReference(this._firestore.collection(collectionPath));
}

/**
* Executes the given updateFunction and then attempts to commit the changes applied within the transaction. If any document read within
* the transaction has changed, the updateFunction will be retried. If it fails to commit after 5 attempts, the transaction will fail.
*
* @param updateFunction The function to execute within the transaction context.
* @return If the transaction completed successfully or was explicitly aborted (by the updateFunction returning a failed Promise), the
* Promise returned by the updateFunction will be returned here. Else if the transaction failed, a rejected Promise with the
* corresponding failure error will be returned.
*/
public runTransaction(
updateFunction: (transaction: GeoTransaction | GeoFirestoreTypes.cloud.Transaction | GeoFirestoreTypes.web.Transaction) => Promise<any>
): Promise<any> {
return (this._firestore as GeoFirestoreTypes.web.Firestore).runTransaction(updateFunction);
}
}
2 changes: 2 additions & 0 deletions src/GeoFirestoreTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export namespace GeoFirestoreTypes {
export type Query = FirestoreTypes.Query;
export type QuerySnapshot = FirestoreTypes.QuerySnapshot;
export type QueryDocumentSnapshot = FirestoreTypes.QueryDocumentSnapshot;
export type Transaction = FirestoreTypes.Transaction;
export type WriteBatch = FirestoreTypes.WriteBatch;
}
export namespace cloud {
Expand All @@ -60,6 +61,7 @@ export namespace GeoFirestoreTypes {
export type Query = FirebaseFirestore.Query;
export type QuerySnapshot = FirebaseFirestore.QuerySnapshot;
export type QueryDocumentSnapshot = FirebaseFirestore.QueryDocumentSnapshot;
export type Transaction = FirebaseFirestore.Transaction;
export type WriteBatch = FirebaseFirestore.WriteBatch;
}
}
13 changes: 13 additions & 0 deletions src/GeoTransaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { GeoFirestoreTypes } from './GeoFirestoreTypes';

/**
* A reference to a transaction. The `GeoTransaction` object passed to a transaction's updateFunction provides the methods to read and
* write data within the transaction context. See `GeoFirestore.runTransaction()`.
*/
export class GeoTransaction {
constructor(private _transaction: GeoFirestoreTypes.cloud.Transaction | GeoFirestoreTypes.web.Transaction) {
if (Object.prototype.toString.call(_transaction) !== '[object Object]') {
throw new Error('Transaction must be an instance of a Firestore Transaction');
}
}
}

0 comments on commit 0b68729

Please sign in to comment.