/// A seeded stream over the note log's change events. library; import 'dart:async'; /// A stream that emits `compute()` synchronously when first listened to (the /// seed), then again on every event of [changes]. Seeding in `onListen` -- /// rather than lazily in an `async*` body -- avoids a race where a write /// between `listen` and the first generator tick would swallow the initial /// snapshot. Stream watchLog(Stream changes, T Function() compute) { StreamSubscription? sub; late final StreamController controller; controller = StreamController( onListen: () { controller.add(compute()); sub = changes.listen((_) => controller.add(compute())); }, onCancel: () => sub?.cancel(), ); return controller.stream; }