awaitReady method

Future<ReadinessResult> awaitReady({
  1. required String glintId,
  2. int ceilingMs = 5000,
  3. int staleAfterMs = 1500,
})

Polls until glintId is hittable and not faded to opacity 0, or the ceiling. A screen whose content has not changed for staleAfterMs and still lacks the target fails fast as notFound (with id suggestions) — a static screen will not grow the node by waiting.

Implementation

Future<ReadinessResult> awaitReady({
  required String glintId,
  int ceilingMs = 5000,
  int staleAfterMs = 1500,
}) async {
  final start = DateTime.now();
  var attempts = 0;
  String? lastDetail;
  var sawInTree = false;
  String? lastSignature;
  DateTime? stableSince;
  var suggestions = const <String>[];
  RuntimeUnresponsiveError? busy;

  CallProgress.enter('waiting for the target to be ready and hittable', key: 'ready');
  while (true) {
    CallProgress.checkAlive();
    attempts++;
    busy = null;
    final Scene scene;
    try {
      scene = await reader.readSummary();
    } on Object catch (e) {
      busy = appBusyCause(e);
      if (busy == null) rethrow;
      if (DateTime.now().difference(start).inMilliseconds >= ceilingMs) {
        return ReadinessResult.busy(
            glintId: glintId,
            attempts: attempts,
            elapsedMs: DateTime.now().difference(start).inMilliseconds,
            timeout: busy.timeout);
      }
      await Future<void>.delayed(Duration(milliseconds: pollIntervalMs));
      continue;
    }
    try {
      final node = scene.findByGlintId(glintId);
      if (node == null) {
        final sig = scene.contentSignature();
        if (sig != lastSignature) {
          lastSignature = sig;
          stableSince = DateTime.now();
        }
        suggestions = suggestIds(
          [
            for (final n in scene.root.walk())
              if (n.glintId != null && !n.isOffstage) n.glintId!
          ],
          glintId,
        );
        final stableMs = DateTime.now().difference(stableSince!).inMilliseconds;
        if (!sawInTree && stableMs >= staleAfterMs) {
          return ReadinessResult.notFound(
            glintId: glintId,
            attempts: attempts,
            elapsedMs: DateTime.now().difference(start).inMilliseconds,
            suggestions: suggestions,
            staleScreen: true,
          );
        }
      }
      if (node != null) {
        sawInTree = true;
        try {
          final coord = await resolver.resolve(scene, glintId);
          if (coord.hittable && coord.nearestAncestorOpacity > 0) {
            return ReadinessResult.ready(
              glintId: glintId,
              attempts: attempts,
              elapsedMs: DateTime.now().difference(start).inMilliseconds,
              coord: coord,
            );
          }
          lastDetail = 'painted=${coord.painted}, hittable=${coord.hittable}, '
              'ancestorOpacity=${coord.nearestAncestorOpacity}';
        } on GeometryResolveError catch (e) {
          // Geometry can transiently fail mid-build; keep polling.
          lastDetail = 'geometry resolve error: ${e.message}';
        } on Object catch (e) {
          busy = appBusyCause(e);
          if (busy == null) rethrow;
          lastDetail = 'the app did not answer within ${busy.timeout.inSeconds}s';
        }
      } else {
        lastDetail = 'no node with glintId "$glintId" in scene';
      }
    } finally {
      await scene.dispose();
    }

    final elapsed = DateTime.now().difference(start).inMilliseconds;
    if (elapsed >= ceilingMs) {
      if (busy != null) {
        return ReadinessResult.busy(glintId: glintId, attempts: attempts, elapsedMs: elapsed, timeout: busy.timeout);
      }
      return sawInTree
          ? ReadinessResult.neverReady(
              glintId: glintId,
              attempts: attempts,
              elapsedMs: elapsed,
              detail: lastDetail,
            )
          : ReadinessResult.notFound(
              glintId: glintId,
              attempts: attempts,
              elapsedMs: elapsed,
              suggestions: suggestions,
            );
    }
    await Future<void>.delayed(Duration(milliseconds: pollIntervalMs));
  }
}