Options
All
  • Public
  • Public/Protected
  • All
Menu

Allows executing async tasks with a limit on the number of tasks that is executing at the same time. This is useful for long-running tasks that potentially allocates a lot of memory, like resource fetching/decompression.

The implementation uses a priority queue for tasks. Tasks with higher priority will start executing before tasks with lower priority.

Tasks for the AsyncTaskQueue can be scheduled in two ways:

  1. Pass a "defaultTask" function to the AsyncTaskQueue constructor. Schedule the defaultTask for execution by calling scheduleDefaultTask with an argument that will be sent to the "defaultTask" function when the task executes.
  2. Pass null as "defaultTask" function to the AsyncTaskQueue constructor. Schedule a new task by calling scheduleTask with the async function that should execute sometime in the future.

New and queued tasks can be prevented from starting by pausing the AsyncTaskQueue with pause(). To resume execution of queued tasks, call resume(). Note that tasks that have already started executing will not be paused and are allowed to continue.

Type parameters

  • T

  • U

Hierarchy

  • AsyncTaskQueue

Implements

Index

Constructors

constructor

  • new AsyncTaskQueue<T, U>(concurrency: number, defaultTask: (task: T) => U | PromiseLike<U>): AsyncTaskQueue<T, U>
  • Constructs a new AsyncTaskQueue. The new instance will initially not be in paused state.

    Type parameters

    • T

    • U

    Parameters

    • concurrency: number

      The number of tasks that can be executing simultaneously.

    • defaultTask: (task: T) => U | PromiseLike<U>

      A function responsible for executing tasks scheduled with scheduleDefaultTask. Pass null to not specify any default task.

        • (task: T): U | PromiseLike<U>
        • Parameters

          • task: T

          Returns U | PromiseLike<U>

    Returns AsyncTaskQueue<T, U>

Methods

destroy

  • destroy(reason: any): void
  • Destroys this queue, immediately rejecting any queued tasks with the provided reason. Tasks that are currently executing are allowed to complete.

    Calling scheduleDefaultTask or scheduleTask after this method has been called will result in the returned promise to be rejected immediately with the specified reason

    Parameters

    • reason: any

      The object that should be used for rejecting queued tasks.

    Returns void

getNumberOfRunningTasks

  • getNumberOfRunningTasks(): number

getNumberOfTasksInQueue

  • getNumberOfTasksInQueue(): number

isDestroyed

  • isDestroyed(): boolean
  • If this instance has been destroyed. See destroy() for the behaviour when an instance is destroyed.

    Returns boolean

isPaused

  • isPaused(): boolean

pause

  • pause(): void
  • Pauses this async task queue. No new or queued tasks will be executed while this instance is paused. Tasks that are currently executing will still run to completion.

    Calling this method on an already paused instance has no effect.

    Returns void

resume

  • resume(): void
  • Resumes this async task queue. New and queued tasks will be allowed to execute again.

    Calling this method on an instance that is not paused has no effect.

    Returns void

scheduleDefaultTask

  • scheduleDefaultTask(defaultTaskArgument: T, priority: number): Promise<U>
  • Schedules the default task to run with the specified argument in the future.

    Parameters

    • defaultTaskArgument: T

      The argument to pass to this instance's default task.

    • priority: number

      The priority of the task. Tasks with higher priority values will execute before tasks with lower priority value. Tasks with the same priority value will execute in FIFO order.

    Returns Promise<U>

scheduleTask

  • scheduleTask<TResult>(task: () => PromiseLike<TResult>, priority: number): Promise<TResult>
  • Schedules an async function (task) to be executed in the future.

    Type parameters

    • TResult

    Parameters

    • task: () => PromiseLike<TResult>

      The function that should be executed in the future.

        • (): PromiseLike<TResult>
        • Returns PromiseLike<TResult>

    • priority: number

      The priority of the task. Tasks with higher priority values will execute before tasks with lower priority value. Tasks with the same priority value will execute in FIFO order.

    Returns Promise<TResult>

setOnBeforeExecuteNextTask

  • setOnBeforeExecuteNextTask(callback: () => void): void
  • Sets a callback function to be called just before a task is to be taken from the queue and executed. This enables the callback function to pause this instance.

    Parameters

    • callback: () => void
        • (): void
        • Returns void

    Returns void

Generated using TypeDoc