progress<T> method

Future<T> progress<T>(
  1. String label,
  2. FutureOr<T> runner(), {
  3. Map<String, Object?>? metadata,
  4. bool isSuccess(
    1. T result
    )?,
})

Runs runner inside a new scope. The scope is automatically opened before the runner and closed after it completes (or fails).

Log calls inside the runner are automatically scoped via the Zone.

Success signal:

  • If runner throws, the scope closes with success: false.
  • Else if isSuccess is provided, its return value is used.
  • Else if T is bool, the returned value is used directly.
  • Otherwise, the scope closes with success: true.

Implementation

Future<T> progress<T>(
  String label,
  FutureOr<T> Function() runner, {
  Map<String, Object?>? metadata,
  bool Function(T result)? isSuccess,
}) async {
  final scope = currentScope.child(
    id: _newScopeId(label),
    label: label,
    metadata: metadata,
  );
  await _writer.openScope(scope);
  final stopwatch = Stopwatch()..start();
  try {
    final result = await runZoned(
      runner,
      zoneValues: {_logScopeKey: scope},
    );
    await _writer.closeScope(
      scope,
      success: isSuccess?.call(result) ?? (result is bool ? result : true),
      duration: stopwatch.elapsed,
    );
    return result;
  } catch (e, st) {
    await _writer.closeScope(
      scope,
      success: false,
      duration: stopwatch.elapsed,
      error: e,
      stackTrace: st,
    );
    rethrow;
  }
}