Skip to content

Compat class for Transaction #4070

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 5 additions & 18 deletions packages/firestore/exp/src/api/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,16 @@

import { Transaction as LiteTransaction } from '../../../lite/src/api/transaction';
import { DocumentSnapshot } from './snapshot';
import { TransactionRunner } from '../../../src/core/transaction_runner';
import { AsyncQueue } from '../../../src/util/async_queue';
import { FirebaseFirestore } from './database';
import { Deferred } from '../../../src/util/promise';
import {
ensureFirestoreConfigured,
SnapshotMetadata
} from '../../../src/api/database';
import { Transaction as InternalTransaction } from '../../../src/core/transaction';
import { validateReference } from '../../../lite/src/api/write_batch';
import { getDatastore } from '../../../lite/src/api/components';
import { DocumentReference } from '../../../lite/src/api/reference';
import { ExpUserDataWriter } from './reference';
import { firestoreClientTransaction } from '../../../src/core/firestore_client';

/**
* A reference to a transaction.
Expand Down Expand Up @@ -97,18 +94,8 @@ export function runTransaction<T>(
firestore: FirebaseFirestore,
updateFunction: (transaction: Transaction) => Promise<T>
): Promise<T> {
ensureFirestoreConfigured(firestore);

const deferred = new Deferred<T>();
firestore._queue.enqueueAndForget(async () => {
const datastore = await getDatastore(firestore);
new TransactionRunner<T>(
new AsyncQueue(),
datastore,
internalTransaction =>
updateFunction(new Transaction(firestore, internalTransaction)),
deferred
).run();
});
return deferred.promise;
const client = ensureFirestoreConfigured(firestore);
return firestoreClientTransaction(client, internalTransaction =>
updateFunction(new Transaction(firestore, internalTransaction))
);
}
100 changes: 0 additions & 100 deletions packages/firestore/exp/test/shim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,9 @@
*/

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

import { FieldPath as FieldPathExp, Bytes as BytesExp } from '../../exp/index';
import {
isPlainObject,
validateSetOptions
} from '../../src/util/input_validation';
import { Compat } from '../../src/compat/compat';
import {
Firestore,
DocumentReference,
DocumentSnapshot
} from '../../src/api/database';

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

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

export class Transaction
extends Compat<exp.Transaction>
implements legacy.Transaction {
constructor(
private readonly _firestore: Firestore,
delegate: exp.Transaction
) {
super(delegate);
}

get<T>(documentRef: DocumentReference<T>): Promise<DocumentSnapshot<T>> {
return this._delegate
.get(documentRef._delegate)
.then(result => new DocumentSnapshot(this._firestore, result));
}

set<T>(
documentRef: DocumentReference<T>,
data: T,
options?: legacy.SetOptions
): Transaction {
if (options) {
validateSetOptions('Transaction.set', options);
this._delegate.set(documentRef._delegate, unwrap(data), options);
} else {
this._delegate.set(documentRef._delegate, unwrap(data));
}
return this;
}

update(
documentRef: DocumentReference<any>,
data: legacy.UpdateData
): Transaction;
update(
documentRef: DocumentReference<any>,
field: string | FieldPath,
value: any,
...moreFieldsAndValues: any[]
): Transaction;
update(
documentRef: DocumentReference<any>,
dataOrField: any,
value?: any,
...moreFieldsAndValues: any[]
): Transaction {
if (arguments.length === 2) {
this._delegate.update(documentRef._delegate, unwrap(dataOrField));
} else {
this._delegate.update(
documentRef._delegate,
unwrap(dataOrField),
unwrap(value),
...unwrap(moreFieldsAndValues)
);
}

return this;
}

delete(documentRef: DocumentReference<any>): Transaction {
this._delegate.delete(documentRef._delegate);
return this;
}
}

export class FieldPath
extends Compat<FieldPathExp>
implements legacy.FieldPath {
Expand Down Expand Up @@ -141,27 +65,3 @@ export class Blob extends Compat<BytesExp> implements legacy.Blob {
return this._delegate.isEqual(other._delegate);
}
}

/**
* Takes user data that uses API types from this shim and replaces them
* with the the firestore-exp API types.
*/
function unwrap(value: any): any {
if (Array.isArray(value)) {
return value.map(v => unwrap(v));
} else if (value instanceof Compat) {
return value._delegate;
} else if (value instanceof FieldPath) {
return value._delegate;
} else if (isPlainObject(value)) {
const obj: any = {};
for (const key in value) {
if (value.hasOwnProperty(key)) {
obj[key] = unwrap(value[key]);
}
}
return obj;
} else {
return value;
}
}
Loading