Skip to content

⬅️ Back to Table of Contents

📄 index.ts

📊 Analysis Summary

Metric Count
🔧 Functions 1
📦 Imports 5
📑 Type Aliases 2

📚 Table of Contents

🛠️ File Location:

📂 packages/shared/computedEager/index.ts

📦 Imports

Name Source
ShallowRef vue
WatchOptionsBase vue
readonly vue
shallowRef vue
watchEffect vue

Functions

computedEager(fn: () => T, options: ComputedEagerOptions): ComputedEagerReturn<T>

Code
export function computedEager<T>(fn: () => T, options?: ComputedEagerOptions): ComputedEagerReturn<T> {
  const result = shallowRef()

  watchEffect(() => {
    result.value = fn()
  }, {
    ...options,
    flush: options?.flush ?? 'sync',
  })

  return readonly(result)
}
  • JSDoc:

    /**
     * Note: If you are using Vue 3.4+, you can straight use computed instead.
     * Because in Vue 3.4+, if computed new value does not change,
     * computed, effect, watch, watchEffect, render dependencies will not be triggered.
     * refer: https://github.com/vuejs/core/pull/5912
     *
     * @param fn effect function
     * @param options WatchOptionsBase
     * @returns readonly shallowRef
     */
    

  • Parameters:

  • fn: () => T
  • options: ComputedEagerOptions
  • Return Type: ComputedEagerReturn<T>
  • Calls:
  • shallowRef (from vue)
  • watchEffect (from vue)
  • fn
  • readonly (from vue)

Type Aliases

ComputedEagerOptions

type ComputedEagerOptions = WatchOptionsBase;

ComputedEagerReturn<T = any = any>

type ComputedEagerReturn<T = any = any> = Readonly<ShallowRef<T>>;