Skip to content

⬅️ Back to Table of Contents

📄 reactify

📊 Analysis Summary

Metric Count
🔧 Functions 1
📦 Imports 7
📊 Variables & Constants 1
🟢 Vue Composition API 1
📐 Interfaces 1
📑 Type Aliases 2

📚 Table of Contents

🛠️ File Location:

📂 packages/shared/reactify/index.ts

📦 Imports

Name Source
ComputedRef vue
MaybeRef vue
MaybeRefOrGetter vue
AnyFn ../utils
computed vue
toValue vue
unref vue

Variables & Constants

Name Type Kind Value Exported
createReactiveFn <T extends AnyFn, K extends boolean =... const reactify

Vue Composition API

Name Type Reactive Variables Composables
computed computed none none

Functions

reactify(fn: T, options: ReactifyOptions<K>): ReactifyReturn<T, K>

Converts plain function into a reactive function. The converted function accepts refs as it's arguments and returns a ComputedRef, with proper typing.

Parameters:

  • fn any: Source function
  • options any: Options

Tags: @__NO_SIDE_EFFECTS__

Raw JSDoc
/**
 * Converts plain function into a reactive function.
 * The converted function accepts refs as it's arguments
 * and returns a ComputedRef, with proper typing.
 *
 * @param fn - Source function
 * @param options - Options
 *
 * @__NO_SIDE_EFFECTS__
 */

Calls:

  • computed (from vue)
  • fn.apply
  • args.map
  • unrefFn
Code
export function reactify<T extends AnyFn, K extends boolean = true>(fn: T, options?: ReactifyOptions<K>): ReactifyReturn<T, K> {
  const unrefFn = options?.computedGetter === false ? unref : toValue
  return function (this: any, ...args: any[]) {
    return computed(() => fn.apply(this, args.map(i => unrefFn(i))))
  } as ReactifyReturn<T, K>
}

Interfaces

ReactifyOptions<T extends boolean>

Interface Code
export interface ReactifyOptions<T extends boolean> {
  /**
   * Accept passing a function as a reactive getter
   *
   * @default true
   */
  computedGetter?: T
}

Properties

Name Type Optional Description
computedGetter T not shown

Type Aliases

Reactified<T, Computed extends boolean>

type Reactified<T, Computed extends boolean> = T extends (...args: infer A) => infer R
  ? (...args: { [K in keyof A]: Computed extends true ? MaybeRefOrGetter<A[K]> : MaybeRef<A[K]> }) => ComputedRef<R>
  : never;

ReactifyReturn<T extends AnyFn = AnyFn, K extends boolean = true>

type ReactifyReturn<T extends AnyFn = AnyFn, K extends boolean = true> = Reactified<T, K>;

Generated by Syntax Scribe