Skip to content

⬅️ Back to Table of Contents

📄 index.ts

📊 Analysis Summary

Metric Count
🔧 Functions 1
📦 Imports 2
📊 Variables & Constants 2
📑 Type Aliases 1

📚 Table of Contents

🛠️ File Location:

📂 packages/shared/createGlobalState/index.ts

📦 Imports

Name Source
AnyFn ../utils
effectScope vue

Variables & Constants

Name Type Kind Value Exported
initialized boolean let/var false
state any let/var *not shown*

Functions

createGlobalState(stateFactory: Fn): CreateGlobalStateReturn<Fn>

Code
export function createGlobalState<Fn extends AnyFn>(
  stateFactory: Fn,
): CreateGlobalStateReturn<Fn> {
  let initialized = false
  let state: any
  const scope = effectScope(true)

  return ((...args: any[]) => {
    if (!initialized) {
      state = scope.run(() => stateFactory(...args))!
      initialized = true
    }
    return state
  }) as Fn
}
  • JSDoc:

    /**
     * Keep states in the global scope to be reusable across Vue instances.
     *
     * @see https://vueuse.org/createGlobalState
     * @param stateFactory A factory function to create the state
     */
    

  • Parameters:

  • stateFactory: Fn
  • Return Type: CreateGlobalStateReturn<Fn>
  • Calls:
  • effectScope (from vue)
  • scope.run
  • stateFactory

Type Aliases

CreateGlobalStateReturn<Fn extends AnyFn = AnyFn extends AnyFn = AnyFn>

type CreateGlobalStateReturn<Fn extends AnyFn = AnyFn extends AnyFn = AnyFn> = Fn;