Function beginContext

Wraps a function so it always runs outside any active method or execution context.

When the wrapped function is called, beginContext checks whether MethodContextService or ExecutionContextService currently have an active scope and, if so, escapes each one with runOutOfContext before invoking run. This prevents accidental context leakage from a caller into a logically independent operation (e.g. an internal runner that must establish its own fresh context).

The returned wrapper preserves the original function's parameter and return types, so it is a transparent drop-in replacement.

const runInContextInternal = beginContext(
async (run: () => Promise<void>, context: IRunContext) => {
return await MethodContextService.runInContext(
() => ExecutionContextService.runInContext(run, context),
context,
);
},
);
  • Type Parameters

    • T extends (...args: any[]) => any

      Type of the wrapped function.

    Parameters

    • run: T

      Function to wrap.

    Returns (...args: Parameters<T>) => ReturnType<T>

    A new function with the same signature as run that escapes any active context before executing.