Implementation:Webdriverio Webdriverio RequestQueueHandler Class
| Knowledge Sources | |
|---|---|
| Domains | BrowserStack, Request_Management |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
The RequestQueueHandler class is a singleton batching queue that accumulates upload requests and dispatches them in configurable batches via interval-based polling.
Description
RequestQueueHandler implements a producer-consumer pattern for batching test observability events before sending them to BrowserStack's data collection API. Events are added to an internal queue via add(). A polling interval (configured by DATA_BATCH_INTERVAL, default 2000ms) periodically invokes sendBatch(), which splices up to DATA_BATCH_SIZE (default 1000) events from the queue and passes them to a callback function. The queue also triggers immediate batch dispatch when the queue reaches the batch size threshold, or when tearDownInvoked is set (force-flushing remaining events). The shutdown() method stops the polling interval and drains all remaining events. The class enforces singleton access via getInstance().
Usage
Use this handler as the transport layer for sending test, hook, log, and screenshot events to the BrowserStack observability API. It is initialized by the Listener class with a callback that calls batchAndPostEvents.
Code Reference
Source Location
- Repository: Webdriverio_Webdriverio
- File: packages/wdio-browserstack-service/src/request-handler.ts
- Lines: 9-92
Signature
export default class RequestQueueHandler {
private queue: UploadType[]
private readonly callback: Function | undefined
public static tearDownInvoked: boolean
static instance: RequestQueueHandler
private constructor(callback: Function)
public static getInstance(callback?: Function): RequestQueueHandler
add(event: UploadType): void
async shutdown(): Promise<void>
startEventBatchPolling(): void
async sendBatch(): Promise<void>
resetEventBatchPolling(): void
removeEventBatchPolling(tag: string): void
shouldProceed(): boolean
}
Import
import RequestQueueHandler from './request-handler.js'
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| callback | Function |
Yes (on first getInstance) | Callback function invoked with batched data; receives (data: UploadType[], kind: string)
|
| event | UploadType |
Yes | Individual event to add to the queue (test run, hook run, log, or screenshot) |
Outputs
| Name | Type | Description |
|---|---|---|
| Batched callback invocation | void |
The callback is invoked with arrays of up to DATA_BATCH_SIZE events
|
| Queue draining on shutdown | void |
All remaining events are flushed through the callback during shutdown |
Usage Examples
Initializing the queue handler with a callback
import RequestQueueHandler from './request-handler.js'
import { batchAndPostEvents } from './util.js'
import { DATA_BATCH_ENDPOINT } from './constants.js'
const handler = RequestQueueHandler.getInstance(async (data: UploadType[]) => {
await batchAndPostEvents(DATA_BATCH_ENDPOINT, 'BATCH', data)
})
Adding events to the queue
const handler = RequestQueueHandler.getInstance()
handler.add({
event_type: 'TestRunFinished',
test_run: testData
})
// Event is queued; will be sent in the next batch interval or
// immediately if queue size >= DATA_BATCH_SIZE
Graceful shutdown
const handler = RequestQueueHandler.getInstance()
await handler.shutdown()
// Stops polling, drains all remaining events through the callback