fix(app): read views after queued writes, reload on write failure

- Page and journal views read through the write queue after the previous
  view's pending saves, so they never load stale text.
- A failed write reloads the current view and shows a message instead of
  leaving blocks on temporary ids; inserts also trigger reconciliation.
- Completion replaces the query range, not up to the caret, and closes
  when the caret moves away.
- Renaming a page reloads it so rewritten links are not overwritten.
- Dropping below an expanded parent lands as its first child, where the
  indicator is drawn.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dasemuandClaude Opus 5.5 committed 2026-10-21 04:08:36 +02:00
1 parent fce7526fcf
commit 5e25db3e24
9 files changed
+102 -29

No files matched your search

+18 -4
View File
@@ -34,8 +34,10 @@ class PageTarget extends AppRoute {
/// paying for itself; rebuilds are scoped with `ListenableBuilder`.
class AppState extends ChangeNotifier {
AppState(this.backend, {WriteQueue? queue})
: queue = queue ?? WriteQueue(onError: _logError),
refs = BlockRefCache(backend);
: refs = BlockRefCache(backend),
queue = queue ?? WriteQueue() {
this.queue.onError = _writeFailed;
}
final GraphBackend backend;
final WriteQueue queue;
@@ -50,15 +52,27 @@ class AppState extends ChangeNotifier {
/// Last page shown (for synapse reinforcement on navigation).
String? _lastPageId;
/// Bumped when an error makes views reload from the core.
/// Bumped to make the current view reload from the core (failed writes, renames).
int generation = 0;
/// Last write error, shown once by the shell.
String? writeError;
AppRoute get route => _route;
bool get canGoBack => _back.isNotEmpty;
bool get canGoForward => _forward.isNotEmpty;
static void _logError(Object e, StackTrace st) {
/// A failed write leaves the optimistic UI out of sync: reload the view from the core.
void _writeFailed(Object e, StackTrace st) {
debugPrint('graph write failed: $e\n$st');
writeError = '$e';
reload();
}
/// Rebuilds the current view from the core (after queued writes have run).
void reload() {
generation++;
notifyListeners();
}
void go(AppRoute r) {
+7 -2
View File
@@ -298,6 +298,7 @@ class OutlineController extends ChangeNotifier {
if (wasFocused) focusedId = b.id;
if (!_disposed) notifyListeners();
});
_scheduleVerify();
}
void _refocus(OutlineNode n, int? caret) {
@@ -308,11 +309,15 @@ class OutlineController extends ChangeNotifier {
}
void _structural(Future<List<BlockDto>> Function() op) {
_structuralEdits++;
final seen = _structuralEdits;
_enqueue(() async {
await op();
});
_scheduleVerify();
}
void _scheduleVerify() {
_structuralEdits++;
final seen = _structuralEdits;
_reconcile?.cancel();
_reconcile = Timer(const Duration(milliseconds: 600), () {
if (seen == _structuralEdits) _verify();
+4 -3
View File
@@ -9,7 +9,7 @@ class WriteQueue {
WriteQueue({this.onError});
/// Called with any error a task throws; the task's own future also completes with it.
final void Function(Object error, StackTrace stack)? onError;
void Function(Object error, StackTrace stack)? onError;
final _tasks = Queue<Future<void> Function()>();
bool _running = false;
@@ -23,13 +23,14 @@ class WriteQueue {
return (_idle ??= Completer<void>()).future;
}
Future<T> run<T>(Future<T> Function() task) {
/// Queues `task`. Reads pass `report: false` so their errors do not reach [onError].
Future<T> run<T>(Future<T> Function() task, {bool report = true}) {
final done = Completer<T>();
_tasks.add(() async {
try {
done.complete(await task());
} catch (e, st) {
onError?.call(e, st);
if (report) onError?.call(e, st);
done.completeError(e, st);
}
});