All files / src/helpers/queueWatchers queueWatchers.ts

100% Statements 21/21
100% Branches 8/8
100% Functions 3/3
100% Lines 21/21

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 39 40 41 42  7x   7x 7x   7x 7x       7x 70x 47x 12x 12x     35x   35x       7x 71x   71x 35x   35x 12x       71x 6x     65x    
import { type Cache } from '../../Cache'
import { scope } from '../../constants'
import { type Observer } from '../../types'
import { shiftSet } from '../../utils/shiftSet'
import { clearWatcher } from '../clearWatchers'
 
const cacheStack = new Set<Cache>()
const observersStack = new Set<Observer>()
let currentCache: Cache
let currentObserver: Observer
 
export function forceQueueWatchers () {
  while ((currentCache = shiftSet(cacheStack)) || (currentObserver = shiftSet(observersStack))) {
    if (currentCache) {
      currentCache.invalid = true
      continue
    }
 
    clearWatcher(currentObserver)
 
    currentObserver.update()
  }
}
 
export function queueWatchers (watchers: Set<Observer>) {
  const useLoop = !scope.eventDeep && !observersStack.size && !cacheStack.size
 
  watchers.forEach(watcher => {
    observersStack.add(watcher)
 
    if (watcher.isCache) {
      cacheStack.add(watcher as Cache)
    }
  })
 
  if (!useLoop) {
    return
  }
 
  forceQueueWatchers()
}