watch method

Stream<Snapshot> watch (
  1. {Schema schema,
  2. Duration interval,
  3. Reach reach}
)

Returns am infinite stream of snapshots.

Some databases such as Firebase or Firestore support this operation natively. In other databases, the operation may be implemented with polling.

Implementation

Stream<Snapshot> watch({
  Schema schema,
  Duration interval,
  Reach reach,
}) async* {
  // As long as the stream is not closed.
  while (true) {
    // Construct a stream.
    final stream = DocumentReadWatchRequest(
      document: this,
      outputSchema: schema,
      pollingInterval: interval,
      reach: reach,
    ).delegateTo(parentDatabase.adapter);

    // Yield the stream.
    yield* (stream);

    // Wait a bit before watching again.
    await Future.delayed(interval ?? const Duration(seconds: 1));
  }
}