fix(learn): the instance asks LanguageTool for its languages again a minute after it didn't answer

Switching the sidecar on learn.halkyon.eu's server recreated the server
beside LanguageTool, whose start takes a minute: the server's first look
at the languages failed, and it looked again only an hour later, as
after one that worked. Until then instance.info() named no languages,
and an app that asked meanwhile keeps that answer, so its editors check
nothing until it starts anew. A look LanguageTool doesn't answer is now
tried again a minute later, by a timer, until it answers, the languages
known before kept meanwhile; one that worked still counts for an hour.

The server's tests take fake_async (^1.3.3, as Learn's app, already in
the lock) for the timer.

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-26 12:32:06 +01:00
commit 19dd4d9c4e
Signed by: raisondetredev
GPG key ID: 07F6FCF405488B64

View file

@ -42,11 +42,17 @@ class GrammarChecking {
List<String>? _codes;
DateTime? _lookedAt;
Future<void>? _looking;
Timer? _again;
final _asked = HashMap<UuidValue, Queue<DateTime>>();
/// How long a look at the languages counts.
static const _fresh = Duration(hours: 1);
/// How soon a look LanguageTool didn't answer is tried again: it takes
/// a minute to start, and a server started beside it looks at once, and
/// apps keep the languages they were told.
static const _retry = Duration(minutes: 1);
/// The languages checked here (`de`, `ja`); null without a LanguageTool,
/// or while it hasn't answered. Looks again in the background once the
/// last look is an hour old, and never waits.
@ -62,12 +68,14 @@ class GrammarChecking {
}
/// Asks LanguageTool which languages it checks; one look at a time. A
/// failed look forgets none it knew.
/// failed look forgets none it knew, and looks again after [_retry],
/// until LanguageTool answers.
Future<void> look() => _looking ??= () async {
_again?.cancel();
try {
_codes = await languageTool?.languages() ?? _codes;
} on LanguageToolFailed {
// Unknown until the next look.
_again = Timer(_retry, () => unawaited(look()));
} finally {
_lookedAt = clock.now();
_looking = null;