All files / utils queue.ts

84.38% Statements 27/32
62.5% Branches 5/8
85.71% Functions 6/7
88.46% Lines 23/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38  7x 7x   1x 1x   1x 1x   7x 2x 2x       2x 2x 1x 1x     7x     7x 2x   7x 1x 1x 1x 1x 2x     7x  
import { voidFun } from '../common/constant'
import { _global } from './global'
export class Queue {
  private micro: Promise<void>
  private stack: any[] = []
  private isFlushing = false
  constructor() {
    Iif (!('Promise' in _global)) return
    this.micro = Promise.resolve()
  }
  addFn(fn: voidFun): void {
    Iif (typeof fn !== 'function') return
    Iif (!('Promise' in _global)) {
      fn()
      return
    }
    this.stack.push(fn)
    if (!this.isFlushing) {
      this.isFlushing = true
      this.micro.then(() => this.flushStack())
    }
  }
  clear() {
    this.stack = []
  }
  getStack() {
    return this.stack
  }
  flushStack(): void {
    const temp = this.stack.slice(0)
    this.stack.length = 0
    this.isFlushing = false
    for (let i = 0; i < temp.length; i++) {
      temp[i]()
    }
  }
}