Options
All
  • Public
  • Public/Protected
  • All
Menu

异步队列

新增于v0.0.18

脚本举例

  import { PQueue, sleep } from '@tomato-js/async'
  const queue = new PQueue({
    autoStart:true, // 是否自动开始执行队列
    concurrency: 1 //队列执行并发数
  });
  let str = "";
  queue.add(async () => {
    await sleep(20);
    str = str + 1;
  });
  queue.add(async () => {
    await sleep(30);
    str = str + 2;
  });
  queue.add(async () => {
    await sleep(10);
    str = str + 3;
  });
  // 同步方法监听
  await queue.onIdle()//str=123;
  // 通过事件监听
  queue.on('idle',()=>str)//123

Hierarchy

  • Events
    • PQueue

Index

Constructors

constructor

  • new PQueue(options?: Options): PQueue

Properties

Private concurrency

concurrency: number

Private interval

interval: number

Private intervalCap

intervalCap: number

Private intervalCount

intervalCount: number = 0

Private intervalEnd

intervalEnd: number = 0

Private Optional intervalId

intervalId: ReturnType<typeof setInterval>

Private isIntervalIgnored

isIntervalIgnored: boolean

isPaused

isPaused: boolean

Private pendingCount

pendingCount: number = 0

Private queue

queue: Queue

Private Optional timeoutId

timeoutId: ReturnType<typeof setTimeout>

Accessors

Private doesConcurrentAllowAnother

  • get doesConcurrentAllowAnother(): boolean

Private doesIntervalAllowAnother

  • get doesIntervalAllowAnother(): boolean

pending

  • get pending(): number

Methods

add

  • add<TaskResultsType>(fn: Task<TaskResultsType>): Promise<TaskResultsType>
  • Type parameters

    • TaskResultsType

    Parameters

    • fn: Task<TaskResultsType>

    Returns Promise<TaskResultsType>

addAll

  • addAll<TaskResultsType>(fns: ReadonlyArray<Task<TaskResultsType>>): Promise<TaskResultsType[]>
  • Type parameters

    • TaskResultsType

    Parameters

    • fns: ReadonlyArray<Task<TaskResultsType>>

    Returns Promise<TaskResultsType[]>

clear

  • clear(): boolean

clearAll

  • clearAll(): void
  • 清除所有事件

    新增于v0.0.18

    脚本举例

      import { Events } from '@tomato-js/events'
      const e = new Events();
      function foo() {}
      function bar() {}
      function baz() {}
      e.on('foo', foo);
      e.on('bar', bar);
      e.on('bar', baz);
      e.clearAll();//清除所有key所有事件回调

    Returns void

Private dequeue

  • dequeue(): undefined | (() => Promise<unknown>)

emit

  • emit(event: string, ...args: unknown[]): false | undefined
  • 触发事件

    新增于v0.0.18

    脚本举例

      import { Events } from '@tomato-js/events'
      const e = new Events();
      function foo() {}
      function bar() {}
      function baz() {}
      e.on('foo', foo);
      e.on('bar', bar);
      e.on('bar', baz);
      e.emit('bar');

    Parameters

    • event: string

      事件名称

    • Rest ...args: unknown[]

      透传剩余参数

    Returns false | undefined

Private enqueue

  • enqueue(__namedParameters: { run: () => Promise<unknown> }): void
  • Parameters

    • __namedParameters: { run: () => Promise<unknown> }
      • run: () => Promise<unknown>
          • (): Promise<unknown>
          • Returns Promise<unknown>

    Returns void

Private initializeIntervalIfNeeded

  • initializeIntervalIfNeeded(): void

Private isIntervalPaused

  • isIntervalPaused(): boolean

listeners

  • listeners(event: string): ObjectType<any> | undefined
  • 获取事件函数列表

    新增于v0.0.18

    脚本举例

      import { Events } from '@tomato-js/events'
      const e = new Events();
      function foo() {}
      function bar() {}
      function baz() {}
      e.on('foo', foo);
      e.on('bar', bar);
      e.on('bar', baz);
      e.listeners('bar')
      //[bar,baz]

    Parameters

    • event: string

      事件名称

    Returns ObjectType<any> | undefined

    执行函数列表

Private next

  • next(): void

on

  • on(event: string, fn: FunctionType, context?: unknown): void
  • 注册事件

    新增于v0.0.18

    脚本举例

      import { Events } from '@tomato-js/events'
      const e = new Events();
      function foo() {}
      function bar() {}
      function baz() {}
      e.on('foo', foo);
      e.on('bar', bar);
      e.on('bar', baz);

    Parameters

    • event: string

      事件名称

    • fn: FunctionType

      事件回调函数

    • Optional context: unknown

    Returns void

onIdle

  • onIdle(): Promise<unknown>

Private onInterval

  • onInterval(): void

Private onResumeInterval

  • onResumeInterval(): void

once

  • once(event: string, fn: FunctionType, context?: unknown): void
  • 注册事件,只响应一次

    新增于v0.0.18

    脚本举例

      import { Events } from '@tomato-js/events'
      const e = new Events();
      function foo() {}
      function bar() {}
      function baz() {}
      e.once('foo', foo);
      e.once('bar', bar);
      e.once('bar', baz);

    Parameters

    • event: string

      事件名称

    • fn: FunctionType

      事件回调函数

    • Optional context: unknown

    Returns void

pause

  • pause(): void

Private processQueue

  • processQueue(): void

size

  • size(): number

start

  • start(): this

Private tryToStartAnother

  • tryToStartAnother(): boolean

Generated using TypeDoc