todo-app/lib/frame_stats_web.dart
Krzysztof kuhy Rudnicki 66143bdfe3 Make the app compile and run for web, behind platform seams
First step of moving the desktop app off Flutter's Linux embedder, which tops
out at ~20fps at 4K (see docs/desktop-performance-findings.md). Android is
unaffected and keeps every existing code path.

The blocker was that `dart:io` was imported by note_repository, capture_screen
and settings_screen. On web it compiles to a stub and then throws at runtime --
the first web build rendered a blank white window. So each `dart:io` use now
sits behind a conditional export, with the shared logic hoisted into pure Dart:

- note_repository loses `dart:io`/sqlite entirely. Platform wiring moves to
  repository_factory{,_io,_web}, and the legacy sqlite import moves to its own
  io-only library. The HLC-from-updated_at seeding is preserved verbatim, so
  independently-migrating devices still resolve pre-cutover edits by real edit
  time.
- Notes persist to IndexedDB on web, not localStorage: the log is the primary
  copy of the backlog and localStorage caps at ~5-10MB and is evicted eagerly.
- BACKLOG.md and the explicit Export keep working through a local wrapper
  (added next). A browser cannot write `~/todo/BACKLOG.md`, and the user's
  tooling plus the todo MCP server read that path, so leaving it to rot was not
  an option. Writes are best-effort so the app still works with no wrapper.
- Notes are also mirrored to the wrapper's on-disk copy, so a wiped Chrome
  profile is not a total-loss event for notes that have not reached GitHub yet.

`openRepository` is split so `openRepositoryIn` keeps the migration tests; the
suite stays at 174 green and 100% coverage.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-20 13:14:03 +02:00

82 lines
2.5 KiB
Dart

// coverage:ignore-file
// TEMPORARY diagnostic instrument, web variant. See frame_stats_io.dart.
//
// Armed at build time (`--dart-define=TODO_FRAME_STATS=1`) rather than by an
// environment variable, which a browser does not have. Reports to the devtools
// console.
import 'dart:async';
import 'dart:ui' show FramePhase;
import 'package:flutter/foundation.dart';
import 'package:flutter/scheduler.dart';
/// Whether the frame-stats instrument is armed for this build.
const frameStatsEnabled = bool.fromEnvironment('TODO_FRAME_STATS');
const _slowFrameMs = 8;
const _probePeriod = Duration(milliseconds: 16);
const _blockedMs = 24;
final Stopwatch _clock = Stopwatch();
Timer? _probe;
final List<int> _cadenceUs = <int>[];
String get _at => (_clock.elapsedMilliseconds / 1000).toStringAsFixed(2);
/// Installs the frame-timing and event-loop instruments.
void installFrameStats() {
if (!frameStatsEnabled) return;
_clock.start();
debugPrint('[frame-stats web] armed');
SchedulerBinding.instance.addTimingsCallback((timings) {
for (final t in timings) {
final total = t.buildDuration.inMilliseconds +
t.rasterDuration.inMilliseconds;
if (total > _slowFrameMs) {
debugPrint(
'[SLOW-FRAME $_at s] total=${total}ms '
'(build=${t.buildDuration.inMilliseconds} '
'raster=${t.rasterDuration.inMilliseconds})',
);
}
// Cadence is the headline number for the >=100fps done-condition.
final vsync = t.timestampInMicroseconds(FramePhase.vsyncStart);
_cadenceUs.add(vsync);
}
_reportCadence();
});
var expected = _clock.elapsedMicroseconds + _probePeriod.inMicroseconds;
_probe = Timer.periodic(_probePeriod, (_) {
final now = _clock.elapsedMicroseconds;
final lateMs = (now - expected) / 1000;
if (lateMs > _blockedMs) {
debugPrint('[UI-BLOCKED $_at s] ${lateMs.toStringAsFixed(1)}ms');
}
expected = now + _probePeriod.inMicroseconds;
});
}
void _reportCadence() {
if (_cadenceUs.length < 120) return;
final deltas = <int>[];
for (var i = 1; i < _cadenceUs.length; i++) {
deltas.add(_cadenceUs[i] - _cadenceUs[i - 1]);
}
deltas.sort();
final p50 = deltas[deltas.length ~/ 2] / 1000;
final p95 = deltas[(deltas.length * 95) ~/ 100] / 1000;
debugPrint(
'[frame-stats web] cadence p50=${p50.toStringAsFixed(2)}ms '
'p95=${p95.toStringAsFixed(2)}ms (~${(1000 / p50).toStringAsFixed(1)} fps)',
);
_cadenceUs.clear();
}
/// Stops the probe.
void disposeFrameStats() {
_probe?.cancel();
}