Skip to content

Commit 9d97119

Browse files
Compat class for Transaction
1 parent 6c6c49a commit 9d97119

File tree

3 files changed

+45
-237
lines changed

3 files changed

+45
-237
lines changed

packages/firestore/exp/src/api/transaction.ts

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,16 @@
1717

1818
import { Transaction as LiteTransaction } from '../../../lite/src/api/transaction';
1919
import { DocumentSnapshot } from './snapshot';
20-
import { TransactionRunner } from '../../../src/core/transaction_runner';
21-
import { AsyncQueue } from '../../../src/util/async_queue';
2220
import { FirebaseFirestore } from './database';
23-
import { Deferred } from '../../../src/util/promise';
2421
import {
2522
ensureFirestoreConfigured,
2623
SnapshotMetadata
2724
} from '../../../src/api/database';
2825
import { Transaction as InternalTransaction } from '../../../src/core/transaction';
2926
import { validateReference } from '../../../lite/src/api/write_batch';
30-
import { getDatastore } from '../../../lite/src/api/components';
3127
import { DocumentReference } from '../../../lite/src/api/reference';
3228
import { ExpUserDataWriter } from './reference';
29+
import { firestoreClientTransaction } from '../../../src/core/firestore_client';
3330

3431
/**
3532
* A reference to a transaction.
@@ -97,18 +94,8 @@ export function runTransaction<T>(
9794
firestore: FirebaseFirestore,
9895
updateFunction: (transaction: Transaction) => Promise<T>
9996
): Promise<T> {
100-
ensureFirestoreConfigured(firestore);
101-
102-
const deferred = new Deferred<T>();
103-
firestore._queue.enqueueAndForget(async () => {
104-
const datastore = await getDatastore(firestore);
105-
new TransactionRunner<T>(
106-
new AsyncQueue(),
107-
datastore,
108-
internalTransaction =>
109-
updateFunction(new Transaction(firestore, internalTransaction)),
110-
deferred
111-
).run();
112-
});
113-
return deferred.promise;
97+
const client = ensureFirestoreConfigured(firestore);
98+
return firestoreClientTransaction(client, internalTransaction =>
99+
updateFunction(new Transaction(firestore, internalTransaction))
100+
);
114101
}

packages/firestore/exp/test/shim.ts

Lines changed: 1 addition & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,11 @@ import {
4040
Bytes as BytesExp
4141
} from '../../exp/index';
4242
import { UntypedFirestoreDataConverter } from '../../src/api/user_data_reader';
43-
import {
44-
isPlainObject,
45-
validateSetOptions
46-
} from '../../src/util/input_validation';
43+
import { isPlainObject } from '../../src/util/input_validation';
4744
import { Compat } from '../../src/compat/compat';
4845
import {
4946
Firestore,
5047
DocumentReference,
51-
DocumentSnapshot,
5248
QuerySnapshot,
5349
wrapObserver,
5450
extractSnapshotOptions
@@ -62,72 +58,6 @@ export { GeoPoint, Timestamp } from '../index';
6258
// of the experimental SDK. This shim is used to run integration tests against
6359
// both SDK versions.
6460

65-
export class Transaction
66-
extends Compat<exp.Transaction>
67-
implements legacy.Transaction {
68-
constructor(
69-
private readonly _firestore: Firestore,
70-
delegate: exp.Transaction
71-
) {
72-
super(delegate);
73-
}
74-
75-
get<T>(documentRef: DocumentReference<T>): Promise<DocumentSnapshot<T>> {
76-
return this._delegate
77-
.get(documentRef._delegate)
78-
.then(result => new DocumentSnapshot(this._firestore, result));
79-
}
80-
81-
set<T>(
82-
documentRef: DocumentReference<T>,
83-
data: T,
84-
options?: legacy.SetOptions
85-
): Transaction {
86-
if (options) {
87-
validateSetOptions('Transaction.set', options);
88-
this._delegate.set(documentRef._delegate, unwrap(data), options);
89-
} else {
90-
this._delegate.set(documentRef._delegate, unwrap(data));
91-
}
92-
return this;
93-
}
94-
95-
update(
96-
documentRef: DocumentReference<any>,
97-
data: legacy.UpdateData
98-
): Transaction;
99-
update(
100-
documentRef: DocumentReference<any>,
101-
field: string | FieldPath,
102-
value: any,
103-
...moreFieldsAndValues: any[]
104-
): Transaction;
105-
update(
106-
documentRef: DocumentReference<any>,
107-
dataOrField: any,
108-
value?: any,
109-
...moreFieldsAndValues: any[]
110-
): Transaction {
111-
if (arguments.length === 2) {
112-
this._delegate.update(documentRef._delegate, unwrap(dataOrField));
113-
} else {
114-
this._delegate.update(
115-
documentRef._delegate,
116-
unwrap(dataOrField),
117-
unwrap(value),
118-
...unwrap(moreFieldsAndValues)
119-
);
120-
}
121-
122-
return this;
123-
}
124-
125-
delete(documentRef: DocumentReference<any>): Transaction {
126-
this._delegate.delete(documentRef._delegate);
127-
return this;
128-
}
129-
}
130-
13161
export class Query<T = legacy.DocumentData>
13262
extends Compat<exp.Query<T>>
13363
implements legacy.Query<T> {

0 commit comments

Comments
 (0)