Skip to content

Add terminate() and snapshotEqual() to firestore-exp #3313

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 5 commits into from
Jun 29, 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
2 changes: 2 additions & 0 deletions .changeset/many-lamps-bake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
9 changes: 7 additions & 2 deletions packages/firestore/exp/index.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@ export { FieldPath, documentId } from '../lite/src/api/field_path';
export {
Firestore,
initializeFirestore,
getFirestore
getFirestore,
terminate
} from './src/api/database';

export { DocumentSnapshot, QueryDocumentSnapshot } from './src/api/snapshot';
export {
DocumentSnapshot,
QueryDocumentSnapshot,
snapshotEqual
} from './src/api/snapshot';

export { SnapshotMetadata } from '../src/api/database';

Expand Down
17 changes: 14 additions & 3 deletions packages/firestore/exp/src/api/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import * as firestore from '../../index';

import { _getProvider } from '@firebase/app-exp';
import { _getProvider, _removeServiceInstance } from '@firebase/app-exp';
import { FirebaseApp, _FirebaseService } from '@firebase/app-types-exp';
import { Provider } from '@firebase/component';

Expand All @@ -30,6 +30,7 @@ import {
} from '../../../src/core/component_provider';

import { Firestore as LiteFirestore } from '../../../lite/src/api/database';
import { cast } from '../../../lite/src/api/util';

/**
* The root reference to the Firestore database and the entry point for the
Expand Down Expand Up @@ -86,8 +87,8 @@ export class Firestore extends LiteFirestore
return this._firestoreClientPromise;
}

async delete(): Promise<void> {
// TODO(firestoreexp): Call terminate() once implemented
delete(): Promise<void> {
return terminate(this);
}
}

Expand All @@ -106,3 +107,13 @@ export function initializeFirestore(
export function getFirestore(app: FirebaseApp): Firestore {
return _getProvider(app, 'firestore-exp').getImmediate() as Firestore;
}

export function terminate(
firestore: firestore.FirebaseFirestore
): Promise<void> {
_removeServiceInstance(firestore.app, 'firestore/lite');
const firestoreImpl = cast(firestore, Firestore);
return firestoreImpl
._getFirestoreClient()
.then(firestoreClient => firestoreClient.terminate());
}
37 changes: 34 additions & 3 deletions packages/firestore/exp/src/api/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ import {
} from '../../../lite/src/api/snapshot';
import { Firestore } from './database';
import { cast } from '../../../lite/src/api/util';
import { DocumentReference, Query } from '../../../lite/src/api/reference';
import {
DocumentReference,
Query,
queryEqual
} from '../../../lite/src/api/reference';
import {
changesFromSnapshot,
SnapshotMetadata
Expand Down Expand Up @@ -121,9 +125,9 @@ export class QuerySnapshot<T = firestore.DocumentData>
private _cachedChangesIncludeMetadataChanges?: boolean;

constructor(
private readonly _firestore: Firestore,
readonly _firestore: Firestore,
readonly query: Query<T>,
private readonly _snapshot: ViewSnapshot,
readonly _snapshot: ViewSnapshot,
readonly metadata: SnapshotMetadata
) {}

Expand Down Expand Up @@ -199,3 +203,30 @@ export class QuerySnapshot<T = firestore.DocumentData>
);
}
}

// TODO(firestoreexp): Add tests for snapshotEqual with different snapshot
// metadata
export function snapshotEqual<T>(
left: firestore.DocumentSnapshot<T> | firestore.QuerySnapshot<T>,
right: firestore.DocumentSnapshot<T> | firestore.QuerySnapshot<T>
): boolean {
if (left instanceof DocumentSnapshot && right instanceof DocumentSnapshot) {
return (
left._firestore === right._firestore &&
left._key.isEqual(right._key) &&
(left._document === null
? right._document === null
: left._document.isEqual(right._document)) &&
left._converter === right._converter
);
} else if (left instanceof QuerySnapshot && right instanceof QuerySnapshot) {
return (
left._firestore === right._firestore &&
queryEqual(left.query, right.query) &&
left.metadata.isEqual(right.metadata) &&
left._snapshot.isEqual(right._snapshot)
);
}

return false;
}