during<T> static method

Future<T> during<T>(
  1. String phase,
  2. Future<T> body(), {
  3. required String key,
})

Runs body under phase phase, restoring the previous phase after; its time counts under key and still counts in the phase it ran inside.

Implementation

static Future<T> during<T>(String phase, Future<T> Function() body, {required String key}) async {
  final call = current;
  if (call == null) return body();
  checkAlive();
  final before = call._phase;
  final owner = key != call._baseKey && !call._openSince.containsKey(key);
  if (owner) call._openSince[key] = call.clock.elapsedMilliseconds;
  call._set(phase);
  try {
    return await body();
  } finally {
    if (owner) call._closeNested(key);
    if (!call._abandoned) call._set(before);
  }
}