progress<T> method
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
runnerthrows, the scope closes withsuccess: false. - Else if
isSuccessis provided, its return value is used. - Else if
Tisbool, 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;
}
}