Skip to content
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

Compat class for DocumentSnapshot #4051

Merged
merged 6 commits into from
Nov 11, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
31 changes: 17 additions & 14 deletions packages/firestore/exp/src/api/reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import {
CollectionReference,
doc,
DocumentReference,
newExpUserDataWriter,
newUserDataReader,
Query,
SetOptions,
Expand All @@ -58,13 +57,13 @@ import {
Unsubscribe
} from '../../../src/api/observer';
import {
getEventManager,
executeQueryFromCache,
executeQueryViaSnapshotListener,
readDocumentFromCache,
readDocumentViaSnapshotListener,
firestoreClientWrite,
getEventManager,
getLocalStore,
firestoreClientWrite
readDocumentFromCache,
readDocumentViaSnapshotListener
} from '../../../src/core/firestore_client';
import {
newQueryForPath,
Expand All @@ -81,6 +80,13 @@ import {
} from '../../../src/core/event_manager';
import { FirestoreError } from '../../../src/util/error';
import { Compat } from '../../../src/compat/compat';
import { ExpUserDataWriter } from '../../../src/api/user_data_writer';

export {
DocumentReference,
CollectionReference,
Query
} from '../../../lite/src/api/reference';

/**
* An options object that can be passed to {@link onSnapshot()} and {@link
Expand Down Expand Up @@ -141,7 +147,7 @@ export function getDocFromCache<T>(
): Promise<DocumentSnapshot<T>> {
const firestore = cast(reference.firestore, FirebaseFirestore);
const client = ensureFirestoreConfigured(firestore);
const userDataWriter = newExpUserDataWriter(firestore, reference._converter);
const userDataWriter = new ExpUserDataWriter(firestore);

const deferred = new Deferred<Document | null>();
firestore._queue.enqueueAndForget(async () => {
Expand Down Expand Up @@ -206,7 +212,7 @@ export function getDocFromServer<T>(
export function getDocs<T>(query: Query<T>): Promise<QuerySnapshot<T>> {
const firestore = cast(query.firestore, FirebaseFirestore);
const client = ensureFirestoreConfigured(firestore);
const userDataWriter = newExpUserDataWriter(firestore, query._converter);
const userDataWriter = new ExpUserDataWriter(firestore);

validateHasExplicitOrderByForLimitToLast(query._query);

Expand Down Expand Up @@ -237,7 +243,7 @@ export function getDocsFromCache<T>(
): Promise<QuerySnapshot<T>> {
const firestore = cast(query.firestore, FirebaseFirestore);
const client = ensureFirestoreConfigured(firestore);
const userDataWriter = newExpUserDataWriter(firestore, query._converter);
const userDataWriter = new ExpUserDataWriter(firestore);

const deferred = new Deferred<ViewSnapshot>();
firestore._queue.enqueueAndForget(async () => {
Expand All @@ -260,7 +266,7 @@ export function getDocsFromServer<T>(
): Promise<QuerySnapshot<T>> {
const firestore = cast(query.firestore, FirebaseFirestore);
const client = ensureFirestoreConfigured(firestore);
const userDataWriter = newExpUserDataWriter(firestore, query._converter);
const userDataWriter = new ExpUserDataWriter(firestore);

const deferred = new Deferred<ViewSnapshot>();
firestore._queue.enqueueAndForget(async () => {
Expand Down Expand Up @@ -707,10 +713,7 @@ export function onSnapshot<T>(
} else {
firestore = cast(reference.firestore, FirebaseFirestore);
internalQuery = reference._query;
const userDataWriter = newExpUserDataWriter(
firestore,
reference._converter
);
const userDataWriter = new ExpUserDataWriter(firestore);

observer = {
next: snapshot => {
Expand Down Expand Up @@ -846,7 +849,7 @@ function convertToDocSnapshot<T>(
);
const doc = snapshot.docs.get(ref._key);

const userDataWriter = newExpUserDataWriter(firestore, ref._converter);
const userDataWriter = new ExpUserDataWriter(firestore);
return new DocumentSnapshot(
firestore,
userDataWriter,
Expand Down
6 changes: 3 additions & 3 deletions packages/firestore/exp/src/api/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import { DocumentKey } from '../../../src/model/document_key';
import { Document } from '../../../src/model/document';
import { UserDataWriter } from '../../../src/api/user_data_writer';
import { AbstractUserDataWriter } from '../../../src/api/user_data_writer';
import {
DocumentSnapshot as LiteDocumentSnapshot,
fieldPathFromArgument
Expand Down Expand Up @@ -181,7 +181,7 @@ export class DocumentSnapshot<T = DocumentData> extends LiteDocumentSnapshot<

constructor(
readonly _firestore: FirebaseFirestore,
userDataWriter: UserDataWriter,
userDataWriter: AbstractUserDataWriter,
key: DocumentKey,
document: Document | null,
metadata: SnapshotMetadata,
Expand Down Expand Up @@ -328,7 +328,7 @@ export class QuerySnapshot<T = DocumentData> {

constructor(
readonly _firestore: FirebaseFirestore,
readonly _userDataWriter: UserDataWriter,
readonly _userDataWriter: AbstractUserDataWriter,
query: Query<T>,
readonly _snapshot: ViewSnapshot
) {
Expand Down
9 changes: 2 additions & 7 deletions packages/firestore/exp/src/api/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ import { Transaction as InternalTransaction } from '../../../src/core/transactio
import { validateReference } from '../../../lite/src/api/write_batch';
import { getDatastore } from '../../../lite/src/api/components';
import { DocumentReference } from '../../../lite/src/api/reference';
import { UserDataWriter } from '../../../src/api/user_data_writer';
import { Bytes } from '../../../lite/src/api/bytes';
import { ExpUserDataWriter } from '../../../src/api/user_data_writer';

/**
* A reference to a transaction.
Expand Down Expand Up @@ -58,11 +57,7 @@ export class Transaction extends LiteTransaction {
*/
get<T>(documentRef: DocumentReference<T>): Promise<DocumentSnapshot<T>> {
const ref = validateReference<T>(documentRef, this._firestore);
const userDataWriter = new UserDataWriter(
this._firestore._databaseId,
key => new DocumentReference(this._firestore, ref._converter, key),
bytes => new Bytes(bytes)
);
const userDataWriter = new ExpUserDataWriter(this._firestore);
return super
.get(documentRef)
.then(
Expand Down
25 changes: 3 additions & 22 deletions packages/firestore/lite/src/api/reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
parseSetData,
parseUpdateData,
parseUpdateVarargs,
UntypedFirestoreDataConverter,
UserDataReader
} from '../../../src/api/user_data_reader';
import {
Expand Down Expand Up @@ -79,8 +78,7 @@ import {
import { newSerializer } from '../../../src/platform/serializer';
import { Code, FirestoreError } from '../../../src/util/error';
import { getDatastore } from './components';
import { UserDataWriter } from '../../../src/api/user_data_writer';
import { Bytes } from './bytes';
import { LiteUserDataWriter } from '../../../src/api/user_data_writer';

/**
* Document data (for use with {@link setDoc()}) consists of fields mapped to
Expand Down Expand Up @@ -916,10 +914,7 @@ export function getDoc<T>(
reference: DocumentReference<T>
): Promise<DocumentSnapshot<T>> {
const datastore = getDatastore(reference.firestore);
const userDataWriter = newExpUserDataWriter(
reference.firestore,
reference._converter
);
const userDataWriter = new LiteUserDataWriter(reference.firestore);

return invokeBatchGetDocumentsRpc(datastore, [reference._key]).then(
result => {
Expand Down Expand Up @@ -952,10 +947,7 @@ export function getDocs<T>(query: Query<T>): Promise<QuerySnapshot<T>> {
validateHasExplicitOrderByForLimitToLast(query._query);

const datastore = getDatastore(query.firestore);
const userDataWriter = newExpUserDataWriter(
query.firestore,
query._converter
);
const userDataWriter = new LiteUserDataWriter(query.firestore);
return invokeRunQueryRpc(datastore, query._query).then(result => {
const docs = result.map(
doc =>
Expand Down Expand Up @@ -1247,14 +1239,3 @@ export function newUserDataReader(
serializer
);
}

export function newExpUserDataWriter<T>(
firestore: FirebaseFirestore,
converter: UntypedFirestoreDataConverter<T> | null
): UserDataWriter {
return new UserDataWriter(
firestore._databaseId,
key => new DocumentReference(firestore, converter, key),
bytes => new Bytes(bytes)
);
}
4 changes: 2 additions & 2 deletions packages/firestore/lite/src/api/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
import { FieldPath } from './field_path';
import { DocumentKey } from '../../../src/model/document_key';
import { Document } from '../../../src/model/document';
import { UserDataWriter } from '../../../src/api/user_data_writer';
import { AbstractUserDataWriter } from '../../../src/api/user_data_writer';
import { FieldPath as InternalFieldPath } from '../../../src/model/path';
import {
fieldPathFromDotSeparatedString,
Expand Down Expand Up @@ -118,7 +118,7 @@ export class DocumentSnapshot<T = DocumentData> {

constructor(
public _firestore: FirebaseFirestore,
public _userDataWriter: UserDataWriter,
public _userDataWriter: AbstractUserDataWriter,
public _key: DocumentKey,
public _document: Document | null,
public _converter: UntypedFirestoreDataConverter<T> | null
Expand Down
7 changes: 2 additions & 5 deletions packages/firestore/lite/src/api/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ import { Deferred } from '../../../src/util/promise';
import { validateReference } from './write_batch';
import {
DocumentReference,
newExpUserDataWriter,
newUserDataReader,
SetOptions,
UpdateData
} from './reference';
import { FieldPath } from './field_path';
import { getDatastore } from './components';
import { LiteUserDataWriter } from '../../../src/api/user_data_writer';

// TODO(mrschmidt) Consider using `BaseTransaction` as the base class in the
// legacy SDK.
Expand Down Expand Up @@ -78,10 +78,7 @@ export class Transaction {
*/
get<T>(documentRef: DocumentReference<T>): Promise<DocumentSnapshot<T>> {
const ref = validateReference(documentRef, this._firestore);
const userDataWriter = newExpUserDataWriter(
this._firestore,
documentRef._converter
);
const userDataWriter = new LiteUserDataWriter(this._firestore);
return this._transaction
.lookup([ref._key])
.then((docs: MaybeDocument[]) => {
Expand Down
21 changes: 3 additions & 18 deletions packages/firestore/src/api/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { Value as ProtoValue } from '../protos/firestore_proto_api';

import { FirebaseApp } from '@firebase/app-types';
import { _FirebaseApp, FirebaseService } from '@firebase/app-types/private';
import { Blob } from './blob';
import { DatabaseId } from '../core/database_info';
import { ListenOptions } from '../core/event_manager';
import {
Expand Down Expand Up @@ -462,7 +461,7 @@ export class Transaction implements PublicTransaction {
documentRef,
this._firestore
);
const userDataWriter = newUserDataWriter(this._firestore, ref._converter);
const userDataWriter = new UserDataWriter(this._firestore);
return this._transaction
.lookup([ref._key])
.then((docs: MaybeDocument[]) => {
Expand Down Expand Up @@ -758,10 +757,7 @@ export class DocumentReference<T = PublicDocumentData>
delegate: ExpDocumentReference<T>
) {
super(delegate);
this._userDataWriter = newUserDataWriter(
firestore,
this._delegate._converter
);
this._userDataWriter = new UserDataWriter(firestore);
}

static forPath<U>(
Expand Down Expand Up @@ -1819,7 +1815,7 @@ export class QuerySnapshot<T = PublicDocumentData>
private readonly _snapshot: ViewSnapshot,
private readonly _converter: PublicFirestoreDataConverter<T> | null
) {
this._userDataWriter = newUserDataWriter(this._firestore, this._converter);
this._userDataWriter = new UserDataWriter(this._firestore);
this.metadata = new SnapshotMetadata(
_snapshot.hasPendingWrites,
_snapshot.fromCache
Expand Down Expand Up @@ -2147,14 +2143,3 @@ export function applyFirestoreDataConverter<T>(
}
return convertedValue;
}

function newUserDataWriter<T>(
firestore: Firestore,
converter: UntypedFirestoreDataConverter<T> | null
): UserDataWriter {
return new UserDataWriter(
firestore._databaseId,
key => DocumentReference.forKey(key, firestore, converter),
bytes => new Blob(bytes)
);
}
Loading