Skip to content

⬅️ Back to Table of Contents

📄 index.ts

📊 Analysis Summary

Metric Count
🔧 Functions 7
📦 Imports 8
📊 Variables & Constants 3
📐 Interfaces 1
📑 Type Aliases 2

📚 Table of Contents

🛠️ File Location:

📂 packages/shared/watchTriggerable/index.ts

📦 Imports

Name Source
WatchSource vue
MapOldSources ../utils
MapSources ../utils
WatchIgnorableReturn ../watchIgnorable
WatchWithFilterOptions ../watchWithFilter
isReactive vue
toValue vue
watchIgnorable ../watchIgnorable

Variables & Constants

Name Type Kind Value Exported
cleanupFn (() => void) | undefined let/var *not shown*
fn () => void const cleanupFn
res any let/var *not shown*

Functions

watchTriggerable(sources: [...T], cb: WatchTriggerableCallback<MapSources<T>, MapOldSources<T, true>, FnReturnT>, options: WatchWithFilterOptions<boolean>): WatchTriggerableReturn<FnReturnT>

Code
export function watchTriggerable<T extends Readonly<WatchSource<unknown>[]>, FnReturnT>(sources: [...T], cb: WatchTriggerableCallback<MapSources<T>, MapOldSources<T, true>, FnReturnT>, options?: WatchWithFilterOptions<boolean>): WatchTriggerableReturn<FnReturnT>
  • Parameters:
  • sources: [...T]
  • cb: WatchTriggerableCallback<MapSources<T>, MapOldSources<T, true>, FnReturnT>
  • options: WatchWithFilterOptions<boolean>
  • Return Type: WatchTriggerableReturn<FnReturnT>

onEffect(): void

Code
function onEffect() {
    if (!cleanupFn)
      return

    const fn = cleanupFn
    cleanupFn = undefined
    fn()
  }
  • Return Type: void
  • Calls:
  • fn

onCleanup(callback: () => void): void

Code
function onCleanup(callback: () => void) {
    cleanupFn = callback
  }
  • JSDoc:

    /** Register the function `cleanupFn` */
    

  • Parameters:

  • callback: () => void
  • Return Type: void

_cb(value: any, oldValue: any): any

Code
(
    value: any,
    oldValue: any,
  ) => {
    // When a new side effect occurs, clean up the previous side effect
    onEffect()

    return cb(value, oldValue, onCleanup)
  }
  • Parameters:
  • value: any
  • oldValue: any
  • Return Type: any
  • Calls:
  • onEffect
  • cb
  • Internal Comments:
    // When a new side effect occurs, clean up the previous side effect (x3)
    

trigger(): any

Code
() => {
    let res: any
    ignoreUpdates(() => {
      res = _cb(getWatchSources(source), getOldValue(source))
    })
    return res
  }
  • Return Type: any
  • Calls:
  • ignoreUpdates
  • _cb
  • getWatchSources
  • getOldValue

getWatchSources(sources: any): any

Code
function getWatchSources(sources: any) {
  if (isReactive(sources))
    return sources
  if (Array.isArray(sources))
    return sources.map(item => toValue(item))
  return toValue(sources)
}
  • Parameters:
  • sources: any
  • Return Type: any
  • Calls:
  • isReactive (from vue)
  • Array.isArray
  • sources.map
  • toValue (from vue)

getOldValue(source: any): any[]

Code
function getOldValue(source: any) {
  return Array.isArray(source)
    ? source.map(() => undefined)
    : undefined
}
  • Parameters:
  • source: any
  • Return Type: any[]
  • Calls:
  • Array.isArray
  • source.map

Interfaces

WatchTriggerableReturn<FnReturnT = void>

Interface Code
export interface WatchTriggerableReturn<FnReturnT = void> extends WatchIgnorableReturn {
  /** Execute `WatchCallback` immediately */
  trigger: () => FnReturnT
}

Properties

Name Type Optional Description
trigger () => FnReturnT

Type Aliases

OnCleanup

type OnCleanup = (cleanupFn: () => void) => void;

WatchTriggerableCallback<V = any = any, OV = any = any, R = void = void>

type WatchTriggerableCallback<V = any = any, OV = any = any, R = void = void> = (value: V, oldValue: OV, onCleanup: OnCleanup) => R;