Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Queue<T, V>

Class to queue tasks and transfer them to handler in specified concurrency.

typeparam

Type of task.

typeparam

Type of returned values from handler. This can be inferred from handler option normally.

Type parameters

  • T = any

  • V = void

Hierarchy

Index

Constructors

constructor

Properties

concurrency

concurrency: number = 5

Specifies how many tasks to run simultaneously, default value is 5.

continueOnError

continueOnError: boolean = false

If true, will continue processing tasks after error occurred.

Readonly handler

handler: QueueHandler<T, V>

Handler to process each task.

Readonly key

key: null | keyof T = null

If provided, can avoid adding duplicate tasks with same keys.

maxRetryTimes

maxRetryTimes: number = 0

Specifies how many times to retry before one task success. If one task's retry times execeed, it will never retry automatically, but you can still retry all failed tasks by calling retry() manually. Setting this option to values > 0 implies continueOnError is true.

state

state: QueueState = ...

Returns current working state.

tasks

tasks: T[] = ...

The start task array which will be passed to handler in order.

Methods

abort

  • abort(err?: string | number | Error): boolean
  • Abort current queue and all running tasks. After aborted, queue can still be started manually by calling start(). Returns true if queue was successfully aborted.

    Parameters

    • err: string | number | Error = 'manually'

    Returns boolean

add

  • add(...tasks: T[]): void
  • Push each task to queue, if not found duplicate task with same key. Only available when key specified.

    Parameters

    • Rest ...tasks: T[]

    Returns void

addToStart

  • addToStart(...tasks: T[]): void
  • Unshift each task to queue, if not found duplicate task with same key. Only available when key specified.

    Parameters

    • Rest ...tasks: T[]

    Returns void

clear

  • clear(): boolean
  • End and finish queue, abort all running tasks and clear all tasks and processing records. Returns true if queue was cleared successfully.

    Returns boolean

clearNotRunning

  • clearNotRunning(): void
  • Remove all not running tasks and keeps not running tasks and processing records.

    Returns void

emit

  • emit<K>(name: K, ...args: any[]): void
  • Emit specified event with event name and parameters.

    Type parameters

    • K: "taskfinish" | "taskabort" | "error" | "pause" | "resume" | "finish" | "abort" | "end"

    Parameters

    • name: K

      The event name.

    • Rest ...args: any[]

      The parameters that will be passed to event listeners.

    Returns void

find

  • find(fn: (task: T) => boolean): undefined | T
  • Find first task match test function fn. Processed tasks will not be found.

    Parameters

    • fn: (task: T) => boolean
        • (task: T): boolean
        • Parameters

          • task: T

          Returns boolean

    Returns undefined | T

getFailedCount

  • getFailedCount(): number

getFailedTasks

  • getFailedTasks(): T[]

getProcessedCount

  • getProcessedCount(): number

getRunningCount

  • getRunningCount(): number

getRunningTasks

  • getRunningTasks(): T[]

getTotalCount

  • getTotalCount(): number
  • Returns the tount of total tasks, included processed and unprocessed and failed.

    Returns number

getUnprocessedCount

  • getUnprocessedCount(): number
  • Returns the count of unprocessed tasks, not include failed tasks.

    Returns number

getUnprocessedTasks

  • getUnprocessedTasks(): T[]

has

  • has(task: T): boolean
  • Returns true if found same key task. Only available when key specified.

    Parameters

    • task: T

    Returns boolean

hasListener

  • hasListener(name: string, listener: Function, scope?: object): boolean
  • Check whether listener is in the list for listening specified event name.

    Parameters

    • name: string

      The event name.

    • listener: Function

      The event listener to check.

    • Optional scope: object

      The scope binded to listener. If provided, will additionally check whether the scope match.

    Returns boolean

hasListeners

  • hasListeners(name: string): boolean

off

  • off<K>(name: K, listener: EventHandler, scope?: object): void
  • Removes the listener that is listening specified event name.

    Type parameters

    • K: "taskfinish" | "taskabort" | "error" | "pause" | "resume" | "finish" | "abort" | "end"

    Parameters

    • name: K

      The event name.

    • listener: EventHandler

      The event listener, only matched listener will be removed.

    • Optional scope: object

      The scope binded to listener. If provided, remove listener only when scope match.

    Returns void

on

  • on<K>(name: K, listener: EventHandler, scope?: object): void
  • Registers an event listener to listen event with specified name.

    Type parameters

    • K: "taskfinish" | "taskabort" | "error" | "pause" | "resume" | "finish" | "abort" | "end"

    Parameters

    • name: K

      The event name.

    • listener: EventHandler

      The event listener.

    • Optional scope: object

      The scope will be binded to listener.

    Returns void

once

  • once<K>(name: K, listener: EventHandler, scope?: object): void
  • Registers an event listener to listen event with specified name, triggers for only once.

    Type parameters

    • K: "taskfinish" | "taskabort" | "error" | "pause" | "resume" | "finish" | "abort" | "end"

    Parameters

    • name: K

      The event name.

    • listener: EventHandler

      The event listener.

    • Optional scope: object

      The scope will be binded to listener.

    Returns void

pause

  • pause(): boolean
  • Stop processing tasks, running tasks will not be aborted, but will be locked until resume(). Returns true if paused from running state.

    Returns boolean

push

  • push(...tasks: T[]): void
  • Push tasks to queue.

    Parameters

    • Rest ...tasks: T[]

    Returns void

remove

  • remove(...tasksToRemove: T[]): T[]
  • Removes tasks included in tasksToRemove list. Processed tasks will not be removed. Returns the removed tasks.

    Parameters

    • Rest ...tasksToRemove: T[]

    Returns T[]

removeAllListeners

  • removeAllListeners(): void

removeWhere

  • removeWhere(fn: (task: T) => boolean): T[]
  • Removes all tasks that match test function fn. Processed tasks will not be removed. Returns the removed tasks.

    Parameters

    • fn: (task: T) => boolean
        • (task: T): boolean
        • Parameters

          • task: T

          Returns boolean

    Returns T[]

resume

  • resume(): boolean
  • Resume processing tasks. Returns true if resumed from paused state.

    Returns boolean

retry

  • retry(): boolean
  • Retry all failed tasks immediately, ignore their retried times. Returns true if has failed tasks and queued them.

    Returns boolean

start

  • start(): boolean
  • Start processing tasks. Will emit finish event in next tick if no task to run. Returns true if queue started.

    Returns boolean

unshift

  • unshift(...tasks: T[]): void
  • Unshift tasks to queue.

    Parameters

    • Rest ...tasks: T[]

    Returns void

untilFinish

  • untilFinish(): Promise<void>
  • Returns a promise which will be resolved after all tasks finished, or be rejected if error happens.

    Returns Promise<void>

Generated using TypeDoc