switchPath method

Stream<Snapshot> switchPath(
  1. String path,
  2. Stream mapper(
    1. Snapshot snapshot
    )
)

Updates the content at path for each snapshot in this stream asynchronously with the values from the stream returned by the mapper callback.

The callback is called with the snapshot value of the original child at path whenever this child changes.

Implementation

Stream<Snapshot> switchPath(
    String path, Stream<dynamic> Function(Snapshot snapshot) mapper) {
  var controller = BehaviorSubject<Snapshot>(sync: true);

  return doOnData((v) => controller.add(v))
      .doOnDone(() => controller.close())
      .map((v) => v.child(path))
      .distinct()
      .switchMap((v) {
    return CombineLatestStream.combine2<Snapshot, dynamic, Snapshot>(
        controller.stream, mapper(v), (a, b) {
      return a.setPath(path, b);
    });
  });
}