Skip to content

Commit 6a154eb

Browse files
Compat class for Transaction (#4070)
* Compat class for Transaction * Cleanup * Lint
1 parent 6cd65fc commit 6a154eb

File tree

3 files changed

+45
-271
lines changed

3 files changed

+45
-271
lines changed

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

+5-18
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

-100
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,9 @@
1616
*/
1717

1818
import * as legacy from '@firebase/firestore-types';
19-
import * as exp from '../index';
2019

2120
import { FieldPath as FieldPathExp, Bytes as BytesExp } from '../../exp/index';
22-
import {
23-
isPlainObject,
24-
validateSetOptions
25-
} from '../../src/util/input_validation';
2621
import { Compat } from '../../src/compat/compat';
27-
import {
28-
Firestore,
29-
DocumentReference,
30-
DocumentSnapshot
31-
} from '../../src/api/database';
3222

3323
export { GeoPoint, Timestamp } from '../index';
3424

@@ -38,72 +28,6 @@ export { GeoPoint, Timestamp } from '../index';
3828
// of the experimental SDK. This shim is used to run integration tests against
3929
// both SDK versions.
4030

41-
export class Transaction
42-
extends Compat<exp.Transaction>
43-
implements legacy.Transaction {
44-
constructor(
45-
private readonly _firestore: Firestore,
46-
delegate: exp.Transaction
47-
) {
48-
super(delegate);
49-
}
50-
51-
get<T>(documentRef: DocumentReference<T>): Promise<DocumentSnapshot<T>> {
52-
return this._delegate
53-
.get(documentRef._delegate)
54-
.then(result => new DocumentSnapshot(this._firestore, result));
55-
}
56-
57-
set<T>(
58-
documentRef: DocumentReference<T>,
59-
data: T,
60-
options?: legacy.SetOptions
61-
): Transaction {
62-
if (options) {
63-
validateSetOptions('Transaction.set', options);
64-
this._delegate.set(documentRef._delegate, unwrap(data), options);
65-
} else {
66-
this._delegate.set(documentRef._delegate, unwrap(data));
67-
}
68-
return this;
69-
}
70-
71-
update(
72-
documentRef: DocumentReference<any>,
73-
data: legacy.UpdateData
74-
): Transaction;
75-
update(
76-
documentRef: DocumentReference<any>,
77-
field: string | FieldPath,
78-
value: any,
79-
...moreFieldsAndValues: any[]
80-
): Transaction;
81-
update(
82-
documentRef: DocumentReference<any>,
83-
dataOrField: any,
84-
value?: any,
85-
...moreFieldsAndValues: any[]
86-
): Transaction {
87-
if (arguments.length === 2) {
88-
this._delegate.update(documentRef._delegate, unwrap(dataOrField));
89-
} else {
90-
this._delegate.update(
91-
documentRef._delegate,
92-
unwrap(dataOrField),
93-
unwrap(value),
94-
...unwrap(moreFieldsAndValues)
95-
);
96-
}
97-
98-
return this;
99-
}
100-
101-
delete(documentRef: DocumentReference<any>): Transaction {
102-
this._delegate.delete(documentRef._delegate);
103-
return this;
104-
}
105-
}
106-
10731
export class FieldPath
10832
extends Compat<FieldPathExp>
10933
implements legacy.FieldPath {
@@ -141,27 +65,3 @@ export class Blob extends Compat<BytesExp> implements legacy.Blob {
14165
return this._delegate.isEqual(other._delegate);
14266
}
14367
}
144-
145-
/**
146-
* Takes user data that uses API types from this shim and replaces them
147-
* with the the firestore-exp API types.
148-
*/
149-
function unwrap(value: any): any {
150-
if (Array.isArray(value)) {
151-
return value.map(v => unwrap(v));
152-
} else if (value instanceof Compat) {
153-
return value._delegate;
154-
} else if (value instanceof FieldPath) {
155-
return value._delegate;
156-
} else if (isPlainObject(value)) {
157-
const obj: any = {};
158-
for (const key in value) {
159-
if (value.hasOwnProperty(key)) {
160-
obj[key] = unwrap(value[key]);
161-
}
162-
}
163-
return obj;
164-
} else {
165-
return value;
166-
}
167-
}

0 commit comments

Comments
 (0)