Skip to content

@toolcog/util/task

Toolcog utilities

Classes

Dispatcher<EventTypes>

Extends

Type Parameters

EventTypes extends DispatcherEvents = DispatcherEvents

Constructors

new Dispatcher()

new Dispatcher<EventTypes>(options?): Dispatcher<EventTypes>

Parameters

options?: DispatcherOptions

Returns

Dispatcher<EventTypes>

Overrides

Emitter.constructor

Defined in

task/src/dispatcher.ts:117

Properties

error

readonly static error: typeof error

Inherited from

Emitter.error

Defined in

emit/src/emitter.ts:30

errorMonitor

readonly static errorMonitor: typeof errorMonitor

Inherited from

Emitter.errorMonitor

Defined in

emit/src/emitter.ts:31

Accessors

concurrency

get concurrency(): number

Returns

number

Defined in

task/src/dispatcher.ts:213

paused

get paused(): boolean

Returns

boolean

Defined in

task/src/dispatcher.ts:217

pending

get pending(): number

Returns

number

Defined in

task/src/dispatcher.ts:246

size

get size(): number

Returns

number

Defined in

task/src/dispatcher.ts:138

Methods

addListener()

addListener<Event>(event, listener, options?): this

Type Parameters

Event extends string | number | symbol

Parameters

event: Event

listener: EmitListener<EventTypes[Event]>

options?: EmitListenerOptions

Returns

this

Inherited from

Emitter.addListener

Defined in

emit/src/emitter.ts:41

emit()

emit<Event>(event, …args): boolean

Type Parameters

Event extends string | number | symbol

Parameters

event: Event

• …args: EventTypes[Event]

Returns

boolean

Inherited from

Emitter.emit

Defined in

emit/src/emitter.ts:218

enqueue()

enqueue<T>(task, options?): Promise<T>

Type Parameters

T

Parameters

task: Task<T>

options?: DispatchOptions

Returns

Promise<T>

Defined in

task/src/dispatcher.ts:146

enqueueAll()

enqueueAll<T>(tasks, options): Promise<T[]>

Type Parameters

T

Parameters

tasks: readonly Task<T>[]

options: DispatchOptions = {}

Returns

Promise<T[]>

Defined in

task/src/dispatcher.ts:206

events()

events(): IterableIterator<keyof EventTypes, any, any>

Returns

IterableIterator<keyof EventTypes, any, any>

Inherited from

Emitter.events

Defined in

emit/src/emitter.ts:268

isEmpty()

isEmpty(): boolean

Returns

boolean

Defined in

task/src/dispatcher.ts:142

isIdle()

isIdle(): boolean

Returns

boolean

Defined in

task/src/dispatcher.ts:266

listeners()

listeners<Event>(event): IterableIterator<EmitListener<EventTypes[Event]>, any, any>

Type Parameters

Event extends string | number | symbol

Parameters

event: Event

Returns

IterableIterator<EmitListener<EventTypes[Event]>, any, any>

Inherited from

Emitter.listeners

Defined in

emit/src/emitter.ts:272

on()

on<Event>(event, options?): AsyncIterableIterator<EventTypes[Event], any, any>

Type Parameters

Event extends string | number | symbol

Parameters

event: Event

options?: EmitAsyncOptions

Returns

AsyncIterableIterator<EventTypes[Event], any, any>

Inherited from

Emitter.on

Defined in

emit/src/emitter.ts:72

onAvailable()

onAvailable(backlog): Promise<void>

Parameters

backlog: number

Returns

Promise<void>

Defined in

task/src/dispatcher.ts:250

once()

once<Event>(event, options?): Promise<EventTypes[Event]>

Type Parameters

Event extends string | number | symbol

Parameters

event: Event

options?: EmitAsyncOptions

Returns

Promise<EventTypes[Event]>

Inherited from

Emitter.once

Defined in

emit/src/emitter.ts:169

onEmpty()

onEmpty(): Promise<void>

Returns

Promise<void>

Defined in

task/src/dispatcher.ts:260

onIdle()

onIdle(): Promise<void>

Returns

Promise<void>

Defined in

task/src/dispatcher.ts:270

pause()

pause(): void

Returns

void

Defined in

task/src/dispatcher.ts:221

removeListener()

removeListener<Event>(event, listener, options?): this

Type Parameters

Event extends string | number | symbol

Parameters

event: Event

listener: EmitListener<EventTypes[Event]>

options?: EmitListenerOptions

Returns

this

Inherited from

Emitter.removeListener

Defined in

emit/src/emitter.ts:51

resume()

resume(): void

Returns

void

Defined in

task/src/dispatcher.ts:235

Interfaces

DispatcherOptions

Extends

Properties

captureRejections?

optional captureRejections: boolean

Inherited from

EmitterOptions.captureRejections

Defined in

emit/src/emitter.ts:26

concurrency?

optional concurrency: number

The maximum number of tasks that can be concurrently executing at any given time. Used to limit the concurrency of task execution.

Default
Infinity
Defined in

task/src/dispatcher.ts:33

paused?

optional paused: boolean

Whether to initialize the dispatcher in the paused state. Used to defer executing tasks until Dispatcher.resume is called.

Default
false
Defined in

task/src/dispatcher.ts:71

pendingCarryover?

optional pendingCarryover: boolean

Whether to count pending tasks towards the rateLimit when entering a new rate limit interval. Used to ensure that tasks started during a previous interval are counted towards the rate limit of the next interval if they haven’t finished executing yet.

Default
false
Defined in

task/src/dispatcher.ts:61

Queue()?

optional Queue: <T>() => Queue<T>

The function used to create the dispatcher’s run queue.

Returns

Queue<T>

Default

PriorityQueue

Defined in

task/src/dispatcher.ts:78

rateInterval?

optional rateInterval: number

The number of milliseconds in each rate limit interval. If zero or infinite, then task execution will not be rate limited.

Default
0
Defined in

task/src/dispatcher.ts:51

rateLimit?

optional rateLimit: number

The maximum number of tasks that can be executed in any given rateInterval. Used to rate limit task execution. If rateLimit is infinite, or if rateInterval is zero or infinite, then task execution will not be rate limited.

Default
Infinity
Defined in

task/src/dispatcher.ts:43

retry?

optional retry: number | boolean | RetryOptions

Defined in

task/src/dispatcher.ts:63


DispatchOptions

Properties

priority?

optional priority: number

The execution priority of the task. Lower numeric values correspond to higher priority tasks.

Default
0
Defined in

task/src/dispatcher.ts:16

retry?

optional retry: number | boolean | RetryOptions

Defined in

task/src/dispatcher.ts:23

signal?

optional signal: null | AbortSignal

The signal used to cancel execution of the task.

Defined in

task/src/dispatcher.ts:21


RetryOptions

Properties

factor?

optional factor: number

Defined in

task/src/retry.ts:5

jitter?

optional jitter: boolean

Defined in

task/src/retry.ts:8

maxTimeout?

optional maxTimeout: number

Defined in

task/src/retry.ts:7

minTimeout?

optional minTimeout: number

Defined in

task/src/retry.ts:6

onFailedAttempt()?

optional onFailedAttempt: (reason) => void

Parameters

reason: unknown

Returns

void

Defined in

task/src/retry.ts:10

retries?

optional retries: number

Defined in

task/src/retry.ts:4

shouldRetry()?

optional shouldRetry: (reason) => boolean

Parameters

reason: unknown

Returns

boolean

Defined in

task/src/retry.ts:11

signal?

optional signal: null | AbortSignal

Defined in

task/src/retry.ts:9


TaskOptions

Properties

attempt?

optional attempt: number

The number of times the task has been retried.

Defined in

task/src/task.ts:10

signal?

optional signal: null | AbortSignal

A signal to cancel the execution of the task.

Defined in

task/src/task.ts:5

Type Aliases

DispatcherEvents

DispatcherEvents: object

Type declaration

[error]

[error]: [unknown]

complete

complete: []

empty

empty: []

enqueue

enqueue: []

execute

execute: []

finish

finish: []

idle

idle: []

pause

pause: []

resume

resume: []

Defined in

task/src/dispatcher.ts:81


Task()<T>

Task<T>: (options) => T | PromiseLike<T>

Type Parameters

T = unknown

Parameters

options: TaskOptions

Returns

T | PromiseLike<T>

Defined in

task/src/task.ts:13

Functions

retry()

retry<T>(task, options?): Promise<T>

Type Parameters

T

Parameters

task: Task<T>

options?: RetryOptions

Returns

Promise<T>

Defined in

task/src/retry.ts:14