halkyon_app: instances, sign-in and sync plumbing for Halkyon's apps

What every Halkyon app's shell needs, taken from Halkyon Learn's and set
on halkyon_base's client: the instances a device keeps an account on,
the first start's tiles and where an instance keeps people's data, the
account switcher; signing in anonymously, with GitHub or an email code,
switching person and the start screen; the gates that build an app's
dependencies per instance and per person; and the sync service with its
status, Settings card and banner. Texts of its own in English and
German take the app's name; what only the app can say, it passes in.
Fakes for apps' tests are in testing.dart.

An instance's basics now name the app it serves (`app`, set by the
host's configuration), and an app accepts only an instance of its own.
SerialQueue and CoalescingRunner move into halkyon_core.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Tizian 「ティツィアーン」 <raisondetredev@pm.me>
This commit is contained in:
Tizian 「ティツィアーン」 2026-10-19 15:18:49 +02:00
commit e23cc77f7c
Signed by: raisondetredev
GPG key ID: 07F6FCF405488B64
2 changed files with 10761 additions and 10 deletions

View file

@ -0,0 +1,50 @@
import 'dart:async';
/// Runs an action one run at a time: asked again while a run is under way,
/// it runs once more after it, however often it was asked, so a burst of
/// changes (a sync brings many) costs one more run, not one each, and an
/// older result never overtakes a newer one.
class CoalescingRunner(
/// What a run does.
final Future<void> Function() _action, {
/// Hears of a run that failed; a run asked for meanwhile follows anyway.
required final void Function(Object error, StackTrace stackTrace) _onError,
}) {
/// The runs under way, one asked for meanwhile included; null while none
/// is: a completed future kept from before would run what waits for it in
/// the zone it was made in, which a widget test's fake clock never
/// reaches.
Future<void>? _running;
/// Whether to run once more after the run under way.
var _again = false;
/// Runs now, or once more after the run under way; completes once no run
/// is under way or asked for.
Future<void> run() {
if (_running case final running?) {
_again = true;
return running;
}
final done = Completer<void>();
// Set before the first run starts, so a call during it waits its turn.
_running = done.future;
unawaited(() async {
do {
_again = false;
try {
await _action();
} on Object catch (error, st) {
_onError(error, st);
}
} while (_again);
_running = null;
done.complete();
}());
return done.future;
}
/// Completes once no run is under way or asked for; at once when none is.
Future<void> get idle => _running ?? Future<void>.value();
}

View file

@ -0,0 +1,28 @@
import 'dart:async';
/// Runs actions one after another, each once the one before is done, failed
/// or not: a repository's reads and writes, say, so that none meets another
/// half done.
class SerialQueue() {
// Null while nothing runs: a completed future kept from before would run
// what waits for it in the zone it was made in, which a widget test's fake
// clock never reaches.
Future<void>? _last;
/// Runs [action] once everything queued before it is done; completes as
/// it does.
Future<T> run<T>(Future<T> Function() action) async {
final previous = _last;
final done = Completer<void>();
_last = done.future;
try {
await previous;
return await action();
} finally {
done.complete();
if (identical(_last, done.future)) {
_last = null;
}
}
}
}