replayThenLive function
Stream<JournalEntry>
replayThenLive({
- required int from,
- required Stream<
JournalEntry> historical, - required Stream<
JournalEntry> live, - required int keyOf(),
- bool accept()?,
Replays historical and then continues with live, without gaps or
duplicates.
The live subscription is taken synchronously when the returned stream is
listened to, before historical is read. Live entries that arrive during
the replay are buffered and drained once it finishes; keyOf (a
monotonic journal position) deduplicates entries seen both by the replay
and on live, and nothing at or below from is delivered. Only entries
that pass accept are delivered. live must be a broadcast stream.
Implementation
Stream<JournalEntry> replayThenLive({
required int from,
required Stream<JournalEntry> historical,
required Stream<JournalEntry> live,
required int Function(JournalEntry) keyOf,
bool Function(JournalEntry)? accept,
}) {
late final StreamController<JournalEntry> controller;
StreamSubscription<JournalEntry>? liveSub;
var lastKey = from;
var replaying = true;
var cancelled = false;
final pending = <JournalEntry>[];
void emit(JournalEntry p) {
if (cancelled || controller.isClosed) return;
if (accept != null && !accept(p)) return;
final key = keyOf(p);
if (key > lastKey) {
lastKey = key;
controller.add(p);
}
}
Future<void> replay() async {
try {
await for (final p in historical) {
if (cancelled) return;
emit(p);
}
// No await between here and the end of the drain, so no live entry
// can slip in between the buffered ones and pass-through mode.
replaying = false;
for (final p in pending) {
emit(p);
}
pending.clear();
} catch (e, s) {
if (!controller.isClosed) {
controller.addError(e, s);
await controller.close();
}
}
}
controller = StreamController<JournalEntry>(
onListen: () {
// Subscribe to live entries first so nothing persisted during the
// replay can be missed.
liveSub = live.listen(
(p) {
if (replaying) {
pending.add(p);
} else {
emit(p);
}
},
onError: (Object e, StackTrace s) {
if (!controller.isClosed) controller.addError(e, s);
},
onDone: () {
if (!controller.isClosed) controller.close();
},
);
replay();
},
onCancel: () async {
cancelled = true;
await liveSub?.cancel();
liveSub = null;
},
);
return controller.stream;
}