track<T> static method

Future<T> track<T>(
  1. Future<T> work, {
  2. String? label,
})

Announces work and hands it straight back.

label is what a diagnosis calls it — the deadline message names every tracked future still pending when a scenario runs out of time, and scene model reads better there than Future<void>.

Implementation

static Future<T> track<T>(Future<T> work, {String? label}) {
  var entry = _observed
      ? TrackedRealWork(label, StackTrace.current, since: _clock.elapsed)
      : TrackedRealWork(label, null);
  _pending.add(entry);
  void done(Object? _) => _pending.remove(entry);
  // `then` with an error handler rather than `whenComplete`: the future
  // this produces is nobody's, and an error propagated onto it would be an
  // unhandled one in whatever zone announced the work. The caller keeps the
  // original future, errors and all.
  work.then<void>(
    done,
    onError: (Object error, StackTrace stack) => done(null),
  );
  return work;
}