App: replace a silent socket instead of hanging; blink the terminal cursor on a timer; keep one markdown cache entry per streamed reply
A socket that outlived a sleep or network change looked open, so every queued request waited its own 30 s timeout behind the last until the page was reopened. The client now replaces a socket whose request times out unheard (or after two timeouts in a row), and probes it with a ping after a quiet spell or on returning to the foreground. The terminal's blinking cursor ran a Ticker (a frame every vsync); a 530 ms timer now flips it. Streamed deltas no longer flood the markdown cache and evict finished answers. Adds tool/perf/idle_session_bench.mjs (idle frames, long-session heap, snapshot census) and notes/perf-hangs-idle.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,58 +1,56 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/scheduler.dart';
|
||||
|
||||
/// How long a blinking cursor stays shown, then hidden (xterm's 600 ms is
|
||||
/// close; most terminals use about half a second).
|
||||
const Duration chatShellCursorBlinkPhase = Duration(milliseconds: 530);
|
||||
|
||||
/// The blink of a cursor the program asked to blink (DECSCUSR 1, 3 or 5).
|
||||
/// It runs on a [Ticker] only while [update] says to blink: the terminal
|
||||
/// is focused, the shape blinks and motion is not reduced. Otherwise the
|
||||
/// cursor is steady ([visible] stays true), so an unfocused terminal or a
|
||||
/// test schedules nothing. Typing or output shows it again at once
|
||||
/// ([restart]), as terminals do.
|
||||
/// It runs only while [update] says to blink: the terminal is focused, the
|
||||
/// shape blinks and motion is not reduced. Otherwise the cursor is steady
|
||||
/// ([visible] stays true), so an unfocused terminal or a test schedules
|
||||
/// nothing. Typing or output shows it again at once ([restart]), as
|
||||
/// terminals do.
|
||||
///
|
||||
/// A timer flips the phase, so a blinking cursor costs one frame per phase
|
||||
/// rather than a ticker's frame on every vsync of an otherwise idle page.
|
||||
class ChatShellCursorBlink {
|
||||
ChatShellCursorBlink(TickerProvider vsync) {
|
||||
_ticker = vsync.createTicker(_tick);
|
||||
}
|
||||
|
||||
late final Ticker _ticker;
|
||||
Duration _now = Duration.zero;
|
||||
Duration _since = Duration.zero;
|
||||
Timer? _timer;
|
||||
|
||||
/// Whether the cursor is drawn in the current phase.
|
||||
final ValueNotifier<bool> visible = ValueNotifier(true);
|
||||
|
||||
bool get blinking => _ticker.isActive;
|
||||
bool get blinking => _timer != null;
|
||||
|
||||
void update({required bool blink}) {
|
||||
if (blink == _ticker.isActive) return;
|
||||
if (blink == blinking) return;
|
||||
if (blink) {
|
||||
_now = Duration.zero;
|
||||
_since = Duration.zero;
|
||||
_ticker.start();
|
||||
_start();
|
||||
} else {
|
||||
_ticker.stop();
|
||||
_timer?.cancel();
|
||||
_timer = null;
|
||||
}
|
||||
visible.value = true;
|
||||
}
|
||||
|
||||
/// Shows the cursor and starts the phase again.
|
||||
void restart() {
|
||||
_since = _now;
|
||||
visible.value = true;
|
||||
if (blinking) _start();
|
||||
}
|
||||
|
||||
void _tick(Duration elapsed) {
|
||||
_now = elapsed;
|
||||
final phase =
|
||||
(elapsed - _since).inMicroseconds ~/
|
||||
chatShellCursorBlinkPhase.inMicroseconds;
|
||||
visible.value = phase.isEven;
|
||||
void _start() {
|
||||
_timer?.cancel();
|
||||
_timer = Timer.periodic(
|
||||
chatShellCursorBlinkPhase,
|
||||
(_) => visible.value = !visible.value,
|
||||
);
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
_ticker.dispose();
|
||||
_timer?.cancel();
|
||||
_timer = null;
|
||||
visible.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,7 +78,6 @@ class ChatShellTerminalView extends StatefulWidget {
|
||||
|
||||
class _ChatShellTerminalViewState extends State<ChatShellTerminalView>
|
||||
with
|
||||
SingleTickerProviderStateMixin,
|
||||
ChatShellTerminalFindHost<ChatShellTerminalView>,
|
||||
ChatShellTerminalFeatures<ChatShellTerminalView> {
|
||||
@override
|
||||
|
||||
@@ -18,7 +18,7 @@ import 'chat_shell_terminal_shortcuts.dart';
|
||||
/// [ChatShellTerminalFindHost]). The view supplies its emulator's parts and
|
||||
/// calls [initFeatures] and [disposeFeatures].
|
||||
mixin ChatShellTerminalFeatures<T extends StatefulWidget>
|
||||
on State<T>, TickerProvider, ChatShellTerminalFindHost<T> {
|
||||
on State<T>, ChatShellTerminalFindHost<T> {
|
||||
ScrollController get scroll;
|
||||
GlobalKey<TerminalViewState> get viewKey;
|
||||
|
||||
@@ -44,7 +44,7 @@ mixin ChatShellTerminalFeatures<T extends StatefulWidget>
|
||||
view: viewKey,
|
||||
);
|
||||
hover = ChatShellLinkHover(_terminal, viewKey);
|
||||
blink = ChatShellCursorBlink(this);
|
||||
blink = ChatShellCursorBlink();
|
||||
_guard = ChatShellSelectionGuard(_terminal, controller);
|
||||
scroll.addListener(hover.refresh);
|
||||
_terminal.addListener(_terminalChanged);
|
||||
|
||||
Reference in New Issue
Block a user