runInScope<R> static method

Future<R> runInScope<R>(
  1. FutureOr<R> callback(), {
  2. String name = 'scoped_run',
  3. LevitScope? parentScope,
})

Runs callback in a fresh child scope and disposes it automatically.

This helper is useful in tests and short-lived workflows where manual createScope/dispose plumbing adds noise.

Uses name for scope diagnostics and middleware metadata. If parentScope is provided, the child scope is created under that scope; otherwise it is created under the current scope.

Returns the value returned by callback.

Throws any exception thrown by callback after disposing the child scope.

Implementation

static Future<R> runInScope<R>(
  FutureOr<R> Function() callback, {
  String name = 'scoped_run',
  LevitScope? parentScope,
}) async {
  final scope = (parentScope ?? Ls.currentScope).createScope(name);
  try {
    return await scope.run(callback);
  } finally {
    await scope.dispose();
  }
}