import 'dart:async'; import 'dart:collection'; /// UTF-16 units one emulator write takes at most. const int chatShellFeedSlice = 16 * 1024; /// How long one turn of the event loop writes output before yielding, so a /// large replay or burst never holds a UI frame for long. const Duration chatShellFeedBudget = Duration(milliseconds: 6); /// Output on its way into one emulator, in order: written in slices of at /// most [slice] units, and after [budget] of writing the rest waits for the /// next turn of the event loop. Each piece carries the server's absolute /// byte offset its text ends at; [drawn] is the end of the last piece /// written whole (what a terminal acknowledges as consumed). class ChatShellTerminalFeed { ChatShellTerminalFeed( this._write, { this.budget = chatShellFeedBudget, this.slice = chatShellFeedSlice, }); /// Writes [text] into the emulator; [replay] marks a redraw of old output. final void Function(String text, bool replay) _write; final Duration budget; final int slice; /// Called with a piece's end once it is drawn. void Function(int end)? onDrawn; final Queue<_Piece> _pieces = Queue(); Timer? _timer; bool _pumping = false; int _drawn = 0; int _end = 0; /// The end of the output drawn so far. int get drawn => _drawn; /// The end of the output drawn or waiting to be. int get end => _end; /// Whether nothing waits to be written. bool get idle => _pieces.isEmpty; /// Drops what waits: the emulator is redrawn and continues from [end]. void restart(int end) { _pieces.clear(); _timer?.cancel(); _timer = null; _drawn = end; _end = end; } /// Queues [text] ending at byte [end]; as much as the budget allows is /// written at once. void add(String text, int end, {bool replay = false}) { _pieces.add(_Piece(text, end, replay, null)); _end = end; _schedule(); } /// Runs [action] once everything queued before it is written. void then(void Function() action) { if (_pieces.isEmpty && !_pumping) { action(); return; } _pieces.add(_Piece('', _end, false, action)); } void dispose() { _pieces.clear(); _timer?.cancel(); _timer = null; } void _schedule() { if (_timer == null && !_pumping) _pump(); } void _pump() { _timer = null; _pumping = true; final clock = Stopwatch()..start(); try { while (_pieces.isNotEmpty) { final piece = _pieces.first; if (piece.at < piece.text.length) { var cut = piece.at + slice; if (cut >= piece.text.length) { cut = piece.text.length; } else if (_highSurrogate(piece.text.codeUnitAt(cut - 1))) { // Never split a surrogate pair between two writes. cut++; } _write(piece.text.substring(piece.at, cut), piece.replay); piece.at = cut; } if (piece.at >= piece.text.length) { _pieces.removeFirst(); if (piece.action != null) { piece.action!(); } else { _drawn = piece.end; onDrawn?.call(piece.end); } } if (_pieces.isNotEmpty && clock.elapsed >= budget) { _timer = Timer(Duration.zero, _pump); return; } } } finally { _pumping = false; } } } class _Piece { _Piece(this.text, this.end, this.replay, this.action); final String text; final int end; final bool replay; final void Function()? action; int at = 0; } bool _highSurrogate(int unit) => unit >= 0xD800 && unit <= 0xDBFF;