onSnapshot method

void onSnapshot(
  1. JSFunction onNext, [
  2. JSFunction onError
])

Executes the query and returns the results as Node Stream.

@return A stream of QueryDocumentSnapshot. Plans and optionally executes this query, and streams the results as Node Stream of {document?: DocumentSnapshot, metrics?: ExplainMetrics} objects.

The stream surfaces documents one at a time as they are received from the server, and at the end, it will surface the metrics associated with executing the query (if any).

@example

let query = firestore.collection('col').where('foo', '==', 'bar');
let count = 0;

query.explainStream({analyze: true}).on('data', (data) => {
  if (data.document) {
    // Use data.document which is a DocumentSnapshot instance.
    console.log(`Found document with name '${data.document.id}'`);
    ++count;
  }
  if (data.metrics) {
    // Use data.metrics which is an ExplainMetrics instance.
  }
}).on('end', () => {
  console.log(`Received ${count} documents.`);
});

@return A stream of {document?: DocumentSnapshot, metrics?: ExplainMetrics} objects. Attaches a listener for QuerySnapshot events.

@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. @return An unsubscribe function that can be called to cancel the snapshot listener.

Implementation

// TODO: Is this possible in dart?
// stream(): NodeJS.ReadableStream;

/// Plans and optionally executes this query, and streams the results as Node Stream
/// of `{document?: DocumentSnapshot, metrics?: ExplainMetrics}` objects.
///
/// The stream surfaces documents one at a time as they are received from the
/// server, and at the end, it will surface the metrics associated with
/// executing the query (if any).
///
/// @example
/// ```
/// let query = firestore.collection('col').where('foo', '==', 'bar');
/// let count = 0;
///
/// query.explainStream({analyze: true}).on('data', (data) => {
///   if (data.document) {
///     // Use data.document which is a DocumentSnapshot instance.
///     console.log(`Found document with name '${data.document.id}'`);
///     ++count;
///   }
///   if (data.metrics) {
///     // Use data.metrics which is an ExplainMetrics instance.
///   }
/// }).on('end', () => {
///   console.log(`Received ${count} documents.`);
/// });
/// ```
///
/// @return A stream of `{document?: DocumentSnapshot, metrics?: ExplainMetrics}`
/// objects.

// TODO: Is this possible in dart?
// explainStream(options?: ExplainOptions): NodeJS.ReadableStream;

/// Attaches a listener for `QuerySnapshot `events.
///
/// @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.
/// @return An unsubscribe function that can be called to cancel
/// the snapshot listener.
external void onSnapshot(
  JSFunction onNext, [
  JSFunction onError,
]);