/** * @license * Copyright 2020 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { getModularInstance } from '@firebase/util'; import { loadBundle, namedQuery } from '../api/database'; import { CompleteFn, ErrorFn, isPartialObserver, NextFn, PartialObserver } from '../api/observer'; import { ListenerDataSource } from '../core/event_manager'; import { firestoreClientAddSnapshotsInSyncListener, firestoreClientGetDocumentFromLocalCache, firestoreClientGetDocumentsFromLocalCache, firestoreClientGetDocumentsViaSnapshotListener, firestoreClientGetDocumentViaSnapshotListener, firestoreClientListen, firestoreClientWrite } from '../core/firestore_client'; import { Query as InternalQuery, newQueryForPath } from '../core/query'; import { ViewSnapshot } from '../core/view_snapshot'; import { FieldPath } from '../lite-api/field_path'; import { validateHasExplicitOrderByForLimitToLast } from '../lite-api/query'; import { CollectionReference, doc, DocumentData, DocumentReference, PartialWithFieldValue, Query, SetOptions, UpdateData, WithFieldValue } from '../lite-api/reference'; import { applyFirestoreDataConverter } from '../lite-api/reference_impl'; import { newUserDataReader, ParsedUpdateData, parseSetData, parseUpdateData, parseUpdateVarargs } from '../lite-api/user_data_reader'; import { DocumentKey } from '../model/document_key'; import { DeleteMutation, Mutation, Precondition } from '../model/mutation'; import { debugAssert } from '../util/assert'; import { Code, FirestoreError } from '../util/error'; import { cast } from '../util/input_validation'; import { ensureFirestoreConfigured, Firestore } from './database'; import { DocumentSnapshot, FirestoreDataConverter, QuerySnapshot, SnapshotMetadata } from './snapshot'; import { ExpUserDataWriter } from './user_data_writer'; /** * An options object that can be passed to {@link (onSnapshot:1)} and {@link * QuerySnapshot.docChanges} to control which types of changes to include in the * result set. */ export interface SnapshotListenOptions { /** * Include a change even if only the metadata of the query or of a document * changed. Default is false. */ readonly includeMetadataChanges?: boolean; /** * Set the source the query listens to. Default to "default", which * listens to both cache and server. */ readonly source?: ListenSource; } /** * Describe the source a query listens to. * * Set to `default` to listen to both cache and server changes. Set to `cache` * to listen to changes in cache only. */ export type ListenSource = 'default' | 'cache'; /** * Reads the document referred to by this `DocumentReference`. * * Note: `getDoc()` attempts to provide up-to-date data when possible by waiting * for data from the server, but it may return cached data or fail if you are * offline and the server cannot be reached. To specify this behavior, invoke * {@link getDocFromCache} or {@link getDocFromServer}. * * @param reference - The reference of the document to fetch. * @returns A `Promise` that resolves with a `DocumentSnapshot` containing the * document contents. */ export function getDoc( reference: DocumentReference ): Promise> { reference = cast>( reference, DocumentReference ); const firestore = cast(reference.firestore, Firestore); const client = ensureFirestoreConfigured(firestore); return firestoreClientGetDocumentViaSnapshotListener( client, reference._key ).then(snapshot => convertToDocSnapshot(firestore, reference, snapshot)); } /** * Reads the document referred to by this `DocumentReference` from cache. * Returns an error if the document is not currently cached. * * @returns A `Promise` that resolves with a `DocumentSnapshot` containing the * document contents. */ export function getDocFromCache( reference: DocumentReference ): Promise> { reference = cast>( reference, DocumentReference ); const firestore = cast(reference.firestore, Firestore); const client = ensureFirestoreConfigured(firestore); const userDataWriter = new ExpUserDataWriter(firestore); return firestoreClientGetDocumentFromLocalCache(client, reference._key).then( doc => new DocumentSnapshot( firestore, userDataWriter, reference._key, doc, new SnapshotMetadata( doc !== null && doc.hasLocalMutations, /* fromCache= */ true ), reference.converter ) ); } /** * Reads the document referred to by this `DocumentReference` from the server. * Returns an error if the network is not available. * * @returns A `Promise` that resolves with a `DocumentSnapshot` containing the * document contents. */ export function getDocFromServer< AppModelType, DbModelType extends DocumentData >( reference: DocumentReference ): Promise> { reference = cast>( reference, DocumentReference ); const firestore = cast(reference.firestore, Firestore); const client = ensureFirestoreConfigured(firestore); return firestoreClientGetDocumentViaSnapshotListener(client, reference._key, { source: 'server' }).then(snapshot => convertToDocSnapshot(firestore, reference, snapshot)); } /** * Executes the query and returns the results as a `QuerySnapshot`. * * Note: `getDocs()` attempts to provide up-to-date data when possible by * waiting for data from the server, but it may return cached data or fail if * you are offline and the server cannot be reached. To specify this behavior, * invoke {@link getDocsFromCache} or {@link getDocsFromServer}. * * @returns A `Promise` that resolves with the results of the query. */ export function getDocs( query: Query ): Promise>; export function getDocs( query: Query ): Promise> { query = cast>(query, Query); const firestore = cast(query.firestore, Firestore); const client = ensureFirestoreConfigured(firestore); const userDataWriter = new ExpUserDataWriter(firestore); validateHasExplicitOrderByForLimitToLast(query._query); return firestoreClientGetDocumentsViaSnapshotListener( client, query._query ).then( snapshot => new QuerySnapshot( firestore, userDataWriter, query as Query, snapshot ) ); } /** * Executes the query and returns the results as a `QuerySnapshot` from cache. * Returns an empty result set if no documents matching the query are currently * cached. * * @returns A `Promise` that resolves with the results of the query. */ export function getDocsFromCache< AppModelType, DbModelType extends DocumentData >( query: Query ): Promise> { query = cast>(query, Query); const firestore = cast(query.firestore, Firestore); const client = ensureFirestoreConfigured(firestore); const userDataWriter = new ExpUserDataWriter(firestore); return firestoreClientGetDocumentsFromLocalCache(client, query._query).then( snapshot => new QuerySnapshot( firestore, userDataWriter, query, snapshot ) ); } /** * Executes the query and returns the results as a `QuerySnapshot` from the * server. Returns an error if the network is not available. * * @returns A `Promise` that resolves with the results of the query. */ export function getDocsFromServer< AppModelType, DbModelType extends DocumentData >( query: Query ): Promise> { query = cast>(query, Query); const firestore = cast(query.firestore, Firestore); const client = ensureFirestoreConfigured(firestore); const userDataWriter = new ExpUserDataWriter(firestore); return firestoreClientGetDocumentsViaSnapshotListener(client, query._query, { source: 'server' }).then( snapshot => new QuerySnapshot(firestore, userDataWriter, query, snapshot) ); } /** * Writes to the document referred to by this `DocumentReference`. If the * document does not yet exist, it will be created. * * Note that the returned `Promise` does _not_ resolve until the data is * successfully written to the remote Firestore backend and, similarly, is not * rejected until the remote Firestore backend reports an error saving the given * data. So if the client cannot reach the backend (for example, due to being * offline) then the returned `Promise` will not resolve for a potentially-long * time (for example, until the client has gone back online). That being said, * the given data _will_ be immediately saved to the local cache and will be * incorporated into future "get" operations as if it had been successfully * written to the remote Firestore server, a feature of Firestore called * "latency compensation". The data will _eventually_ be written to the remote * Firestore backend once a connection can be established. Therefore, it is * usually undesirable to `await` the `Promise` returned from this function * because the indefinite amount of time before which the promise resolves or * rejects can block application logic unnecessarily. * * @param reference - A reference to the document to write. * @param data - A map of the fields and values for the document. * @returns A `Promise` that resolves once the data has been successfully * written to the backend or rejects once the backend reports an error writing * the data. */ export function setDoc( reference: DocumentReference, data: WithFieldValue ): Promise; /** * Writes to the document referred to by the specified `DocumentReference`. If * the document does not yet exist, it will be created. If you provide `merge` * or `mergeFields`, the provided data can be merged into an existing document. * * Note that the returned `Promise` does _not_ resolve until the data is * successfully written to the remote Firestore backend and, similarly, is not * rejected until the remote Firestore backend reports an error saving the given * data. So if the client cannot reach the backend (for example, due to being * offline) then the returned `Promise` will not resolve for a potentially-long * time (for example, until the client has gone back online). That being said, * the given data _will_ be immediately saved to the local cache and will be * incorporated into future "get" operations as if it had been successfully * written to the remote Firestore server, a feature of Firestore called * "latency compensation". The data will _eventually_ be written to the remote * Firestore backend once a connection can be established. Therefore, it is * usually undesirable to `await` the `Promise` returned from this function * because the indefinite amount of time before which the promise resolves or * rejects can block application logic unnecessarily. * * @param reference - A reference to the document to write. * @param data - A map of the fields and values for the document. * @param options - An object to configure the set behavior. * @returns A `Promise` that resolves once the data has been successfully * written to the backend or rejects once the backend reports an error writing * the data. */ export function setDoc( reference: DocumentReference, data: PartialWithFieldValue, options: SetOptions ): Promise; export function setDoc( reference: DocumentReference, data: PartialWithFieldValue, options?: SetOptions ): Promise { reference = cast>( reference, DocumentReference ); const firestore = cast(reference.firestore, Firestore); const convertedValue = applyFirestoreDataConverter( reference.converter, data as WithFieldValue, options ); const dataReader = newUserDataReader(firestore); const parsed = parseSetData( dataReader, 'setDoc', reference._key, convertedValue, reference.converter !== null, options ); const mutation = parsed.toMutation(reference._key, Precondition.none()); return executeWrite(firestore, [mutation]); } /** * Updates fields in the document referred to by the specified * `DocumentReference`. The update will fail if applied to a document that does * not exist. * * Note that the returned `Promise` does _not_ resolve until the data is * successfully written to the remote Firestore backend and, similarly, is not * rejected until the remote Firestore backend reports an error saving the given * data. So if the client cannot reach the backend (for example, due to being * offline) then the returned `Promise` will not resolve for a potentially-long * time (for example, until the client has gone back online). That being said, * the given data _will_ be immediately saved to the local cache and will be * incorporated into future "get" operations as if it had been successfully * written to the remote Firestore server, a feature of Firestore called * "latency compensation". The data will _eventually_ be written to the remote * Firestore backend once a connection can be established. Therefore, it is * usually undesirable to `await` the `Promise` returned from this function * because the indefinite amount of time before which the promise resolves or * rejects can block application logic unnecessarily. * * @param reference - A reference to the document to update. * @param data - An object containing the fields and values with which to * update the document. Fields can contain dots to reference nested fields * within the document. * @returns A `Promise` that resolves once the data has been successfully * written to the backend or rejects once the backend reports an error writing * the data. */ export function updateDoc( reference: DocumentReference, data: UpdateData ): Promise; /** * Updates fields in the document referred to by the specified * `DocumentReference` The update will fail if applied to a document that does * not exist. * * Nested fields can be updated by providing dot-separated field path * strings or by providing `FieldPath` objects. * * Note that the returned `Promise` does _not_ resolve until the data is * successfully written to the remote Firestore backend and, similarly, is not * rejected until the remote Firestore backend reports an error saving the given * data. So if the client cannot reach the backend (for example, due to being * offline) then the returned `Promise` will not resolve for a potentially-long * time (for example, until the client has gone back online). That being said, * the given data _will_ be immediately saved to the local cache and will be * incorporated into future "get" operations as if it had been successfully * written to the remote Firestore server, a feature of Firestore called * "latency compensation". The data will _eventually_ be written to the remote * Firestore backend once a connection can be established. Therefore, it is * usually undesirable to `await` the `Promise` returned from this function * because the indefinite amount of time before which the promise resolves or * rejects can block application logic unnecessarily. * * @param reference - A reference to the document to update. * @param field - The first field to update. * @param value - The first value. * @param moreFieldsAndValues - Additional key value pairs. * @returns A `Promise` that resolves once the data has been successfully * written to the backend or rejects once the backend reports an error writing * the data. */ export function updateDoc( reference: DocumentReference, field: string | FieldPath, value: unknown, ...moreFieldsAndValues: unknown[] ): Promise; export function updateDoc( reference: DocumentReference, fieldOrUpdateData: string | FieldPath | UpdateData, value?: unknown, ...moreFieldsAndValues: unknown[] ): Promise { reference = cast>( reference, DocumentReference ); const firestore = cast(reference.firestore, Firestore); const dataReader = newUserDataReader(firestore); // For Compat types, we have to "extract" the underlying types before // performing validation. fieldOrUpdateData = getModularInstance(fieldOrUpdateData); let parsed: ParsedUpdateData; if ( typeof fieldOrUpdateData === 'string' || fieldOrUpdateData instanceof FieldPath ) { parsed = parseUpdateVarargs( dataReader, 'updateDoc', reference._key, fieldOrUpdateData, value, moreFieldsAndValues ); } else { parsed = parseUpdateData( dataReader, 'updateDoc', reference._key, fieldOrUpdateData ); } const mutation = parsed.toMutation(reference._key, Precondition.exists(true)); return executeWrite(firestore, [mutation]); } /** * Deletes the document referred to by the specified `DocumentReference`. * * Note that the returned `Promise` does _not_ resolve until the document is * successfully deleted from the remote Firestore backend and, similarly, is not * rejected until the remote Firestore backend reports an error deleting the given * document. So if the client cannot reach the backend (for example, due to being * offline) then the returned `Promise` will not resolve for a potentially-long * time (for example, until the client has gone back online). That being said, * the given data _will_ be immediately deleted from the local cache and will be * reflected in future "get" operations as if it had been successfully * deleted from the remote Firestore server, a feature of Firestore called * "latency compensation". The document will _eventually_ be deleted from the remote * Firestore backend once a connection can be established. Therefore, it is * usually undesirable to `await` the `Promise` returned from this function * because the indefinite amount of time before which the promise resolves or * rejects can block application logic unnecessarily. * * @param reference - A reference to the document to delete. * @returns A `Promise` that resolves once the document has been successfully * deleted from the backend or rejects once the backend reports an error * deleting the document. */ export function deleteDoc( reference: DocumentReference ): Promise { const firestore = cast(reference.firestore, Firestore); const mutations = [new DeleteMutation(reference._key, Precondition.none())]; return executeWrite(firestore, mutations); } /** * Add a new document to specified `CollectionReference` with the given data, * assigning it a document ID automatically. * * Note that the returned `Promise` does _not_ resolve until the document is * successfully created to the remote Firestore backend and, similarly, is not * rejected until the remote Firestore backend reports an error creating the given * document. So if the client cannot reach the backend (for example, due to being * offline) then the returned `Promise` will not resolve for a potentially-long * time (for example, until the client has gone back online). That being said, * the given document _will_ be immediately created in the local cache and will be * incorporated into future "get" operations as if it had been successfully * created in the remote Firestore server, a feature of Firestore called * "latency compensation". The document will _eventually_ be created in the remote * Firestore backend once a connection can be established. Therefore, it is * usually undesirable to `await` the `Promise` returned from this function * because the indefinite amount of time before which the promise resolves or * rejects can block application logic unnecessarily. * * @param reference - A reference to the collection to add this document to. * @param data - An Object containing the data for the new document. * @returns A `Promise` that resolves once the docoument has been successfully * created in the backend or rejects once the backend reports an error creating * the document. */ export function addDoc( reference: CollectionReference, data: WithFieldValue ): Promise> { const firestore = cast(reference.firestore, Firestore); const docRef = doc(reference); const convertedValue = applyFirestoreDataConverter(reference.converter, data); const dataReader = newUserDataReader(reference.firestore); const parsed = parseSetData( dataReader, 'addDoc', docRef._key, convertedValue, reference.converter !== null, {} ); const mutation = parsed.toMutation(docRef._key, Precondition.exists(false)); return executeWrite(firestore, [mutation]).then(() => docRef); } /** * A function returned by `onSnapshot()` that removes the listener when invoked. */ export interface Unsubscribe { /** Removes the listener when invoked. */ (): void; } // TODO(firestorexp): Make sure these overloads are tested via the Firestore // integration tests /** * Attaches a listener for `DocumentSnapshot` events. You may either pass individual `onNext` and * `onError` callbacks or pass a single observer object with `next` and `error` callbacks. * * NOTE: Although an `onCompletion` callback can be provided, it will never be called because the * snapshot stream is never-ending. * * @param reference - A reference to the document to listen to. * @param observer - A single object containing `next` and `error` callbacks. * @returns An unsubscribe function that can be called to cancel * the snapshot listener. */ export function onSnapshot( reference: DocumentReference, observer: { next?: (snapshot: DocumentSnapshot) => void; error?: (error: FirestoreError) => void; complete?: () => void; } ): Unsubscribe; /** * Attaches a listener for `DocumentSnapshot` events. You may either pass individual `onNext` and * `onError` callbacks or pass a single observer object with `next` and `error` callbacks. * * NOTE: Although an `onCompletion` callback can be provided, it will never be called because the * snapshot stream is never-ending. * * @param reference - A reference to the document to listen to. * @param options - Options controlling the listen behavior. * @param observer - A single object containing `next` and `error` callbacks. * @returns An unsubscribe function that can be called to cancel * the snapshot listener. */ export function onSnapshot( reference: DocumentReference, options: SnapshotListenOptions, observer: { next?: (snapshot: DocumentSnapshot) => void; error?: (error: FirestoreError) => void; complete?: () => void; } ): Unsubscribe; /** * Attaches a listener for `DocumentSnapshot` events. You may either pass individual `onNext` and * `onError` callbacks or pass a single observer object with `next` and `error` callbacks. * * NOTE: Although an `onCompletion` callback can be provided, it will never be called because the * snapshot stream is never-ending. * * @param reference - A reference to the document to listen to. * @param onNext - A callback to be called every time a new `DocumentSnapshot` is available. * @param onError - A callback to be called if the listen fails or is cancelled. No further * callbacks will occur. * @param onCompletion - Can be provided, but will not be called since streams are never ending. * @returns An unsubscribe function that can be called to cancel the snapshot listener. */ export function onSnapshot( reference: DocumentReference, onNext: (snapshot: DocumentSnapshot) => void, onError?: (error: FirestoreError) => void, onCompletion?: () => void ): Unsubscribe; /** * Attaches a listener for `DocumentSnapshot` events. You may either pass individual `onNext` and * `onError` callbacks or pass a single observer object with `next` and `error` callbacks. * * NOTE: Although an `onCompletion` callback can be provided, it will never be called because the * snapshot stream is never-ending. * * @param reference - A reference to the document to listen to. * @param options - Options controlling the listen behavior. * @param onNext - A callback to be called every time a new `DocumentSnapshot` is available. * @param onError - A callback to be called if the listen fails or is cancelled. No further * callbacks will occur. * @param onCompletion - Can be provided, but will not be called since streams are never ending. * @returns An unsubscribe function that can be called to cancel the snapshot listener. */ export function onSnapshot( reference: DocumentReference, options: SnapshotListenOptions, onNext: (snapshot: DocumentSnapshot) => void, onError?: (error: FirestoreError) => void, onCompletion?: () => void ): Unsubscribe; /** * Attaches a listener for `QuerySnapshot` events. You may either pass individual `onNext` and * `onError` callbacks or pass a single observer object with `next` and `error` callbacks. The * listener can be cancelled by calling the function that is returned when `onSnapshot` is called. * * NOTE: Although an `onCompletion` callback can be provided, it will never be called because the * snapshot stream is never-ending. * * @param query - The query to listen to. * @param observer - A single object containing `next` and `error` callbacks. * @returns An unsubscribe function that can be called to cancel the snapshot listener. */ export function onSnapshot( query: Query, observer: { next?: (snapshot: QuerySnapshot) => void; error?: (error: FirestoreError) => void; complete?: () => void; } ): Unsubscribe; /** * Attaches a listener for `QuerySnapshot` events. You may either pass individual `onNext` and * `onError` callbacks or pass a single observer object with `next` and `error` callbacks. The * listener can be cancelled by calling the function that is returned when `onSnapshot` is called. * * NOTE: Although an `onCompletion` callback can be provided, it will never be called because the * snapshot stream is never-ending. * * @param query - The query to listen to. * @param options - Options controlling the listen behavior. * @param observer - A single object containing `next` and `error` callbacks. * @returns An unsubscribe function that can be called to cancel the snapshot listener. */ export function onSnapshot( query: Query, options: SnapshotListenOptions, observer: { next?: (snapshot: QuerySnapshot) => void; error?: (error: FirestoreError) => void; complete?: () => void; } ): Unsubscribe; /** * Attaches a listener for `QuerySnapshot` events. You may either pass individual `onNext` and * `onError` callbacks or pass a single observer object with `next` and `error` callbacks. The * listener can be cancelled by calling the function that is returned when `onSnapshot` is called. * * NOTE: Although an `onCompletion` callback can be provided, it will never be called because the * snapshot stream is never-ending. * * @param query - The query to listen to. * @param onNext - A callback to be called every time a new `QuerySnapshot` is available. * @param onCompletion - Can be provided, but will not be called since streams are never ending. * @param onError - A callback to be called if the listen fails or is cancelled. No further * callbacks will occur. * @returns An unsubscribe function that can be called to cancel the snapshot listener. */ export function onSnapshot( query: Query, onNext: (snapshot: QuerySnapshot) => void, onError?: (error: FirestoreError) => void, onCompletion?: () => void ): Unsubscribe; /** * Attaches a listener for `QuerySnapshot` events. You may either pass individual `onNext` and * `onError` callbacks or pass a single observer object with `next` and `error` callbacks. The * listener can be cancelled by calling the function that is returned when `onSnapshot` is called. * * NOTE: Although an `onCompletion` callback can be provided, it will never be called because the * snapshot stream is never-ending. * * @param query - The query to listen to. * @param options - Options controlling the listen behavior. * @param onNext - A callback to be called every time a new `QuerySnapshot` is available. * @param onCompletion - Can be provided, but will not be called since streams are never ending. * @param onError - A callback to be called if the listen fails or is cancelled. No further * callbacks will occur. * @returns An unsubscribe function that can be called to cancel the snapshot listener. */ export function onSnapshot( query: Query, options: SnapshotListenOptions, onNext: (snapshot: QuerySnapshot) => void, onError?: (error: FirestoreError) => void, onCompletion?: () => void ): Unsubscribe; export function onSnapshot( reference: | Query | DocumentReference, ...args: unknown[] ): Unsubscribe { // onSnapshot for Query or Document. reference = getModularInstance(reference); let options: SnapshotListenOptions = { includeMetadataChanges: false, source: 'default' }; let currArg = 0; if (typeof args[currArg] === 'object' && !isPartialObserver(args[currArg])) { options = args[currArg++] as SnapshotListenOptions; } const internalOptions = { includeMetadataChanges: options.includeMetadataChanges, source: options.source as ListenerDataSource }; if (isPartialObserver(args[currArg])) { const userObserver = args[currArg] as PartialObserver< QuerySnapshot >; args[currArg] = userObserver.next?.bind(userObserver); args[currArg + 1] = userObserver.error?.bind(userObserver); args[currArg + 2] = userObserver.complete?.bind(userObserver); } let observer: PartialObserver; let firestore: Firestore; let internalQuery: InternalQuery; if (reference instanceof DocumentReference) { firestore = cast(reference.firestore, Firestore); internalQuery = newQueryForPath(reference._key.path); observer = { next: snapshot => { if (args[currArg]) { ( args[currArg] as NextFn> )( convertToDocSnapshot( firestore, reference as DocumentReference, snapshot ) ); } }, error: args[currArg + 1] as ErrorFn, complete: args[currArg + 2] as CompleteFn }; } else { const query = cast>(reference, Query); firestore = cast(query.firestore, Firestore); internalQuery = query._query; const userDataWriter = new ExpUserDataWriter(firestore); observer = { next: snapshot => { if (args[currArg]) { (args[currArg] as NextFn>)( new QuerySnapshot(firestore, userDataWriter, query, snapshot) ); } }, error: args[currArg + 1] as ErrorFn, complete: args[currArg + 2] as CompleteFn }; validateHasExplicitOrderByForLimitToLast(reference._query); } const client = ensureFirestoreConfigured(firestore); return firestoreClientListen( client, internalQuery, internalOptions, observer ); } /** * Attaches a listener for `QuerySnapshot` events based on data generated by invoking * {@link QuerySnapshot.toJSON} You may either pass individual `onNext` and `onError` callbacks or * pass a single observer object with `next` and `error` callbacks. The listener can be cancelled by * calling the function that is returned when `onSnapshot` is called. * * NOTE: Although an `onCompletion` callback can be provided, it will never be called because the * snapshot stream is never-ending. * * @param firestore - The {@link Firestore} instance to enable the listener for. * @param snapshotJson - A JSON object generated by invoking {@link QuerySnapshot.toJSON}. * @param onNext - A callback to be called every time a new `QuerySnapshot` is available. * @param onError - A callback to be called if the listen fails or is cancelled. No further * callbacks will occur. * @param onCompletion - Can be provided, but will not be called since streams are never ending. * @param converter - An optional object that converts objects from Firestore before the onNext * listener is invoked. * @returns An unsubscribe function that can be called to cancel the snapshot listener. */ export function onSnapshotResume< AppModelType, DbModelType extends DocumentData >( firestore: Firestore, snapshotJson: object, onNext: (snapshot: QuerySnapshot) => void, onError?: (error: FirestoreError) => void, onCompletion?: () => void, converter?: FirestoreDataConverter ): Unsubscribe; /** * Attaches a listener for `DocumentSnapshot` events based on data generated by invoking * {@link DocumentSnapshot.toJSON}. You may either pass individual `onNext` and `onError` callbacks or * pass a single observer object with `next` and `error` callbacks. The listener can be cancelled by * calling the function that is returned when `onSnapshot` is called. * * NOTE: Although an `onCompletion` callback can be provided, it will never be called because the * snapshot stream is never-ending. * * @param firestore - The {@link Firestore} instance to enable the listener for. * @param snapshotJson - A JSON object generated by invoking {@link DocumentSnapshot.toJSON}. * @param onNext - A callback to be called every time a new `DocumentSnapshot` is available. * @param onError - A callback to be called if the listen fails or is cancelled. No further * callbacks will occur. * @param onCompletion - Can be provided, but will not be called since streams are * never ending. * @param converter - An optional object that converts objects from Firestore before the onNext * listener is invoked. * @returns An unsubscribe function that can be called to cancel the snapshot listener. */ export function onSnapshotResume< AppModelType, DbModelType extends DocumentData >( firestore: Firestore, snapshotJson: object, onNext: (snapshot: DocumentSnapshot) => void, onError?: (error: FirestoreError) => void, onCompletion?: () => void, converter?: FirestoreDataConverter ): Unsubscribe; /** * Attaches a listener for `QuerySnapshot` events based on data generated by invoking * {@link QuerySnapshot.toJSON}. You may either pass individual `onNext` and `onError` callbacks or * pass a single observer object with `next` and `error` callbacks. The listener can be cancelled by * calling the function that is returned when `onSnapshot` is called. * * NOTE: Although an `onCompletion` callback can be provided, it will never be called because the * snapshot stream is never-ending. * * @param firestore - The {@link Firestore} instance to enable the listener for. * @param snapshotJson - A JSON object generated by invoking {@link QuerySnapshot.toJSON}. * @param options - Options controlling the listen behavior. * @param onNext - A callback to be called every time a new `QuerySnapshot` is available. * @param onError - A callback to be called if the listen fails or is cancelled. No further * callbacks will occur. * @param onCompletion - Can be provided, but will not be called since streams are never ending. * @param converter - An optional object that converts objects from Firestore before the onNext * listener is invoked. * @returns An unsubscribe function that can be called to cancel the snapshot listener. */ export function onSnapshotResume< AppModelType, DbModelType extends DocumentData >( firestore: Firestore, snapshotJson: object, options: SnapshotListenOptions, onNext: (snapshot: QuerySnapshot) => void, onError?: (error: FirestoreError) => void, onCompletion?: () => void, converter?: FirestoreDataConverter ): Unsubscribe; /** * Attaches a listener for `DocumentSnapshot` events based on data generated by invoking * {@link DocumentSnapshot.toJSON}. You may either pass individual `onNext` and `onError` callbacks * or pass a single observer object with `next` and `error` callbacks. The listener can be cancelled * by calling the function that is returned when `onSnapshot` is called. * * NOTE: Although an `onCompletion` callback can be provided, it will never be called because the * snapshot stream is never-ending. * * @param firestore - The {@link Firestore} instance to enable the listener for. * @param snapshotJson - A JSON object generated by invoking {@link DocumentSnapshot.toJSON}. * @param options - Options controlling the listen behavior. * @param onNext - A callback to be called every time a new `DocumentSnapshot` is available. * @param onError - A callback to be called if the listen fails or is cancelled. No further * callbacks will occur. * @param onCompletion - Can be provided, but will not be called since streams are never ending. * @param converter - An optional object that converts objects from Firestore before the onNext * listener is invoked. * @returns An unsubscribe function that can be called to cancel * the snapshot listener. */ export function onSnapshotResume< AppModelType, DbModelType extends DocumentData >( firestore: Firestore, snapshotJson: object, options: SnapshotListenOptions, onNext: (snapshot: DocumentSnapshot) => void, onError?: (error: FirestoreError) => void, onCompletion?: () => void, converter?: FirestoreDataConverter ): Unsubscribe; /** * Attaches a listener for `QuerySnapshot` events based on QuerySnapshot data generated by invoking * {@link QuerySnapshot.toJSON}. You may either pass individual `onNext` and `onError` callbacks or * pass a single observer object with `next` and `error` callbacks. The listener can be cancelled by * calling the function that is returned when `onSnapshot` is called. * * NOTE: Although an `onCompletion` callback can be provided, it will never be called because the * snapshot stream is never-ending. * * @param firestore - The {@link Firestore} instance to enable the listener for. * @param snapshotJson - A JSON object generated by invoking {@link QuerySnapshot.toJSON}. * @param observer - A single object containing `next` and `error` callbacks. * @param converter - An optional object that converts objects from Firestore before the onNext * listener is invoked. * @returns An unsubscribe function that can be called to cancel * the snapshot listener. */ export function onSnapshotResume< AppModelType, DbModelType extends DocumentData >( firestore: Firestore, snapshotJson: object, observer: { next: (snapshot: QuerySnapshot) => void; error?: (error: FirestoreError) => void; complete?: () => void; }, converter?: FirestoreDataConverter ): Unsubscribe; /** * Attaches a listener for `DocumentSnapshot` events based on data generated by invoking * {@link DocumentSnapshot.toJSON} You may either pass individual `onNext` and `onError` callbacks * or pass a single observer object with `next` and `error` callbacks. The listener can be cancelled * by calling the function that is returned when `onSnapshot` is called. * * NOTE: Although an `onCompletion` callback can be provided, it will never be called because the * snapshot stream is never-ending. * * @param firestore - The {@link Firestore} instance to enable the listener for. * @param snapshotJson - A JSON object generated by invoking {@link DocumentSnapshot.toJSON}. * @param observer - A single object containing `next` and `error` callbacks. * @param converter - An optional object that converts objects from Firestore before the onNext * listener is invoked. * @returns An unsubscribe function that can be called to cancel * the snapshot listener. */ export function onSnapshotResume< AppModelType, DbModelType extends DocumentData >( firestore: Firestore, snapshotJson: object, observer: { next: (snapshot: DocumentSnapshot) => void; error?: (error: FirestoreError) => void; complete?: () => void; }, converter?: FirestoreDataConverter ): Unsubscribe; /** * Attaches a listener for `QuerySnapshot` events based on QuerySnapshot data generated by invoking * {@link QuerySnapshot.toJSON} You may either pass individual `onNext` and `onError` callbacks or * pass a single observer object with `next` and `error` callbacks. The listener can be cancelled by * calling the function that is returned when `onSnapshot` is called. * * NOTE: Although an `onCompletion` callback can be provided, it will never be called because the * snapshot stream is never-ending. * * @param firestore - The {@link Firestore} instance to enable the listener for. * @param snapshotJson - A JSON object generated by invoking {@link QuerySnapshot.toJSON}. * @param options - Options controlling the listen behavior. * @param observer - A single object containing `next` and `error` callbacks. * @param converter - An optional object that converts objects from Firestore before the onNext * listener is invoked. * @returns An unsubscribe function that can be called to cancel * the snapshot listener. */ export function onSnapshotResume< AppModelType, DbModelType extends DocumentData >( firestore: Firestore, snapshotJson: object, options: SnapshotListenOptions, observer: { next: (snapshot: QuerySnapshot) => void; error?: (error: FirestoreError) => void; complete?: () => void; }, converter?: FirestoreDataConverter ): Unsubscribe; /** * Attaches a listener for `DocumentSnapshot` events based on QuerySnapshot data generated by * invoking {@link DocumentSnapshot.toJSON} You may either pass individual `onNext` and `onError` * callbacks or pass a single observer object with `next` and `error` callbacks. The listener can be * cancelled by calling the function that is returned when `onSnapshot` is called. * * NOTE: Although an `onCompletion` callback can be provided, it will never be called because the * snapshot stream is never-ending. * * @param firestore - The {@link Firestore} instance to enable the listener for. * @param snapshotJson - A JSON object generated by invoking {@link DocumentSnapshot.toJSON}. * @param options - Options controlling the listen behavior. * @param observer - A single object containing `next` and `error` callbacks. * @param converter - An optional object that converts objects from Firestore before the onNext * listener is invoked. * @returns An unsubscribe function that can be called to cancel the snapshot listener. */ export function onSnapshotResume< AppModelType, DbModelType extends DocumentData >( firestore: Firestore, snapshotJson: object, options: SnapshotListenOptions, observer: { next: (snapshot: DocumentSnapshot) => void; error?: (error: FirestoreError) => void; complete?: () => void; }, converter?: FirestoreDataConverter ): Unsubscribe; export function onSnapshotResume< AppModelType, DbModelType extends DocumentData >(reference: Firestore, snapshotJson: object, ...args: unknown[]): Unsubscribe { const db = getModularInstance(reference); const json = normalizeSnapshotJsonFields(snapshotJson); if (json.error) { throw new FirestoreError(Code.INVALID_ARGUMENT, json.error); } let curArg = 0; let options: SnapshotListenOptions | undefined = undefined; if (typeof args[curArg] === 'object' && !isPartialObserver(args[curArg])) { options = args[curArg++] as SnapshotListenOptions; } if (json.bundleSource === 'QuerySnapshot') { let observer: { next: (snapshot: QuerySnapshot) => void; error?: (error: FirestoreError) => void; complete?: () => void; } | null = null; if (typeof args[curArg] === 'object' && isPartialObserver(args[curArg])) { const userObserver = args[curArg++] as PartialObserver< QuerySnapshot >; observer = { next: userObserver.next!, error: userObserver.error, complete: userObserver.complete }; } else { observer = { next: args[curArg++] as ( snapshot: QuerySnapshot ) => void, error: args[curArg++] as (error: FirestoreError) => void, complete: args[curArg++] as () => void }; } return onSnapshotQuerySnapshotBundle( db, json, options, observer!, args[curArg] as FirestoreDataConverter ); } else if (json.bundleSource === 'DocumentSnapshot') { let observer: { next: (snapshot: DocumentSnapshot) => void; error?: (error: FirestoreError) => void; complete?: () => void; } | null = null; if (typeof args[curArg] === 'object' && isPartialObserver(args[curArg])) { const userObserver = args[curArg++] as PartialObserver< DocumentSnapshot >; observer = { next: userObserver.next!, error: userObserver.error, complete: userObserver.complete }; } else { observer = { next: args[curArg++] as ( snapshot: DocumentSnapshot ) => void, error: args[curArg++] as (error: FirestoreError) => void, complete: args[curArg++] as () => void }; } return onSnapshotDocumentSnapshotBundle( db, json, options, observer!, args[curArg] as FirestoreDataConverter ); } else { throw new FirestoreError( Code.INVALID_ARGUMENT, `unsupported bundle source: ${json.bundleSource}` ); } } export interface PipelineListenOptions { /** * Include a change even if only the metadata of the query or of a document * changed. Default is false. */ readonly includeMetadataChanges?: boolean; /** * Set the source the query listens to. Default to "default", which * listens to both cache and server. */ readonly source?: ListenSource; readonly serverTimestampBehavior?: 'estimate' | 'previous' | 'none'; } // TODO(firestorexp): Make sure these overloads are tested via the Firestore // integration tests /** * Attaches a listener for a snapshots-in-sync event. The snapshots-in-sync * event indicates that all listeners affected by a given change have fired, * even if a single server-generated change affects multiple listeners. * * NOTE: The snapshots-in-sync event only indicates that listeners are in sync * with each other, but does not relate to whether those snapshots are in sync * with the server. Use SnapshotMetadata in the individual listeners to * determine if a snapshot is from the cache or the server. * * @param firestore - The instance of Firestore for synchronizing snapshots. * @param observer - A single object containing `next` and `error` callbacks. * @returns An unsubscribe function that can be called to cancel the snapshot * listener. */ export function onSnapshotsInSync( firestore: Firestore, observer: { next?: (value: void) => void; error?: (error: FirestoreError) => void; complete?: () => void; } ): Unsubscribe; /** * Attaches a listener for a snapshots-in-sync event. The snapshots-in-sync * event indicates that all listeners affected by a given change have fired, * even if a single server-generated change affects multiple listeners. * * NOTE: The snapshots-in-sync event only indicates that listeners are in sync * with each other, but does not relate to whether those snapshots are in sync * with the server. Use `SnapshotMetadata` in the individual listeners to * determine if a snapshot is from the cache or the server. * * @param firestore - The `Firestore` instance for synchronizing snapshots. * @param onSync - A callback to be called every time all snapshot listeners are * in sync with each other. * @returns An unsubscribe function that can be called to cancel the snapshot * listener. */ export function onSnapshotsInSync( firestore: Firestore, onSync: () => void ): Unsubscribe; export function onSnapshotsInSync( firestore: Firestore, arg: unknown ): Unsubscribe { firestore = cast(firestore, Firestore); const client = ensureFirestoreConfigured(firestore); const observer = isPartialObserver(arg) ? (arg as PartialObserver) : { next: arg as () => void }; return firestoreClientAddSnapshotsInSyncListener(client, observer); } /** * Locally writes `mutations` on the async queue. * @internal */ export function executeWrite( firestore: Firestore, mutations: Mutation[] ): Promise { const client = ensureFirestoreConfigured(firestore); return firestoreClientWrite(client, mutations); } /** * Converts a {@link ViewSnapshot} that contains the single document specified by `ref` * to a {@link DocumentSnapshot}. */ function convertToDocSnapshot( firestore: Firestore, ref: DocumentReference, snapshot: ViewSnapshot ): DocumentSnapshot { debugAssert( snapshot.docs.size <= 1, 'Expected zero or a single result on a document-only query' ); const doc = snapshot.docs.get(ref._key); const userDataWriter = new ExpUserDataWriter(firestore); return new DocumentSnapshot( firestore, userDataWriter, ref._key, doc, new SnapshotMetadata(snapshot.hasPendingWrites, snapshot.fromCache), ref.converter ); } /** * Ensures the data required to construct an {@link onSnapshot} listener exist in a `snapshotJson` * object that originates from {@link DocumentSnapshot.toJSON} or {@link Querysnapshot.toJSON}. The * data is normalized into a typed object. * * @param snapshotJson - The JSON object that the app provided to {@link onSnapshot}. * @returns A normalized object that contains all of the required bundle JSON fields. If * {@link snapshotJson} doesn't contain the required fields, or if the fields exist as empty * strings, then the {@link snapshotJson.error} field will be a non empty string. * * @internal */ function normalizeSnapshotJsonFields(snapshotJson: object): { bundle: string; bundleName: string; bundleSource: string; error?: string; } { const result: { bundle: string; bundleName: string; bundleSource: string; error?: string; } = { bundle: '', bundleName: '', bundleSource: '' }; const requiredKeys = ['bundle', 'bundleName', 'bundleSource']; for (const key of requiredKeys) { if (!(key in snapshotJson)) { result.error = `snapshotJson missing required field: ${key}`; break; } // eslint-disable-next-line @typescript-eslint/no-explicit-any const value = (snapshotJson as any)[key]; if (typeof value !== 'string') { result.error = `snapshotJson field '${key}' must be a string.`; break; } if (value.length === 0) { result.error = `snapshotJson field '${key}' cannot be an empty string.`; break; } if (key === 'bundle') { result.bundle = value; } else if (key === 'bundleName') { result.bundleName = value; } else if (key === 'bundleSource') { result.bundleSource = value; } } return result; } /** * Loads the bundle in a separate task and then invokes {@link onSnapshot} with a * {@link DocumentReference} for the document in the bundle. * * @param firestore - The {@link Firestore} instance for the {@link onSnapshot} operation request. * @param json - The JSON bundle to load, produced by {@link DocumentSnapshot.toJSON}. * @param options - Options controlling the listen behavior. * @param observer - A single object containing `next` and `error` callbacks. * @param converter - An optional object that converts objects from Firestore before the onNext * listener is invoked. * @returns An unsubscribe function that can be called to cancel the snapshot * listener. * * @internal */ function onSnapshotDocumentSnapshotBundle< AppModelType, DbModelType extends DocumentData >( db: Firestore, json: { bundle: string; bundleName: string; bundleSource: string }, options: SnapshotListenOptions | undefined, observer: { next: (snapshot: DocumentSnapshot) => void; error?: (error: FirestoreError) => void; complete?: () => void; }, converter?: FirestoreDataConverter ): Unsubscribe { let unsubscribed: boolean = false; let internalUnsubscribe: Unsubscribe | undefined; const loadTask = loadBundle(db, json.bundle); loadTask .then(() => { if (!unsubscribed) { const docReference = new DocumentReference( db, converter ? converter : null, DocumentKey.fromPath(json.bundleName) ); internalUnsubscribe = onSnapshot( docReference as DocumentReference, options ? options : {}, observer ); } }) .catch(e => { if (observer.error) { observer.error(e); } return () => {}; }); return () => { if (unsubscribed) { return; } unsubscribed = true; if (internalUnsubscribe) { internalUnsubscribe(); } }; } /** * Loads the bundle in a separate task and then invokes {@link onSnapshot} with a * {@link Query} that represents the Query in the bundle. * * @param firestore - The {@link Firestore} instance for the {@link onSnapshot} operation request. * @param json - The JSON bundle to load, produced by {@link QuerySnapshot.toJSON}. * @param options - Options controlling the listen behavior. * @param observer - A single object containing `next` and `error` callbacks. * @param converter - An optional object that converts objects from Firestore before the onNext * listener is invoked. * @returns An unsubscribe function that can be called to cancel the snapshot * listener. * * @internal */ function onSnapshotQuerySnapshotBundle< AppModelType, DbModelType extends DocumentData >( db: Firestore, json: { bundle: string; bundleName: string; bundleSource: string }, options: SnapshotListenOptions | undefined, observer: { next: (snapshot: QuerySnapshot) => void; error?: (error: FirestoreError) => void; complete?: () => void; }, converter?: FirestoreDataConverter ): Unsubscribe { let unsubscribed: boolean = false; let internalUnsubscribe: Unsubscribe | undefined; const loadTask = loadBundle(db, json.bundle); loadTask .then(() => namedQuery(db, json.bundleName)) .then(query => { if (query && !unsubscribed) { const realQuery: Query = (query as Query)!; if (converter) { realQuery.withConverter(converter); } internalUnsubscribe = onSnapshot( query as Query, options ? options : {}, observer ); } }) .catch(e => { if (observer.error) { observer.error(e); } return () => {}; }); return () => { if (unsubscribed) { return; } unsubscribed = true; if (internalUnsubscribe) { internalUnsubscribe(); } }; }