All files / src/Watch Watch.ts

100% Statements 21/21
100% Branches 4/4
100% Functions 6/6
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 43 44 45 46 477x 7x     7x   36x 36x 36x 36x       36x   36x 36x   36x 8x   8x 5x         36x 36x 36x           6x       28x 27x 27x          
import { scope } from '../constants'
import { destroyWatchers, watchWithScope } from '../helpers'
import { type Observer } from '../types'
 
export class Watch implements Observer {
  // Observer
  destructors = new Set<Function>()
  childWatchers = new Set<Observer>()
  destroyed = false
  isCache = false
 
  readonly watcher: (update: boolean) => void
  constructor (watcher: (update: boolean) => void, freeParent?: boolean, freeUpdate?: boolean) {
    this.watcher = watcher
 
    if (!freeParent) {
      const { activeWatcher } = scope
 
      if (activeWatcher) {
        activeWatcher.childWatchers.add(this)
 
        activeWatcher.destructors.add(() => {
          activeWatcher.childWatchers.delete(this)
        })
      }
    }
 
    if (!freeUpdate) {
      watchWithScope(this, () => {
        watcher(false)
      })
    }
  }
 
  destroy () {
    destroyWatchers(this)
  }
 
  update () {
    if (!this.destroyed) {
      watchWithScope(this, () => {
        this.watcher(true)
      })
    }
  }
}