All files / src/utils queue.ts

87.5% Statements 28/32
62.5% Branches 5/8
100% Functions 7/7
92.31% Lines 24/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  13x 13x   6x 6x   6x 6x   13x 4x 4x       4x 4x 3x 3x     13x 2x   13x 2x   13x 3x 3x 3x 3x 4x     13x  
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]()
    }
  }
}