Implementation:Microsoft Playwright ProgressController
Overview
ProgressController manages the lifecycle of asynchronous operations in Playwright, providing timeout management, abort capabilities, and structured logging for API calls.
Description
The ProgressController class wraps async tasks with timeout handling, abort signals, and state tracking. It transitions through states: before, running, error, and finished. Tasks receive a Progress object for logging and timeout access. The controller supports:
- Timeout-based cancellation using
AbortController - Forced abort via the
abort()method - Call metadata tracking for tracing and debugging
- Structured logging through the
onCallLogcallback - Static factory methods for internal tasks
Usage
Used throughout Playwright server code to wrap operations that need timeout and progress tracking.
Code Reference
Source Location
packages/playwright-core/src/server/progress.ts (147 lines)
Class Signature
export class ProgressController {
readonly metadata: CallMetadata;
constructor(metadata?: CallMetadata, onCallLog?: (message: string) => void)
static createForSdkObject(sdkObject: SdkObject, callMetadata: CallMetadata): ProgressController
static runInternalTask(task: (progress: Progress) => Promise<void>, timeout?: number): Promise<void>
async abort(error: Error): Promise<void>
async run<T>(task: (progress: Progress) => Promise<T>, timeout?: number): Promise<T>
}
Import
import { ProgressController } from './server/progress';
import type { Progress } from './server/progress';
I/O Contract
Inputs
metadata: CallMetadata-- optional metadata for the API callonCallLog: (message: string) => void-- optional logging callbacktask: (progress: Progress) => Promise<T>-- the async task to executetimeout: number-- optional timeout in milliseconds
Outputs
- Returns the task's result on success
- Throws
TimeoutErroron timeout - Throws abort error if
abort()is called
Usage Examples
Running a Task with Timeout
const controller = new ProgressController(metadata);
const result = await controller.run(async progress => {
progress.log('Starting operation...');
return await someAsyncOperation();
}, 30000);
Related Pages
- Microsoft_Playwright_Server_Instrumentation -- SdkObject and CallMetadata
- Microsoft_Playwright_ManualPromise -- Promise utilities used internally