Skip to content

⬅️ Back to Table of Contents

📄 createGlobalState

📊 Analysis Summary

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

📚 Table of Contents

🛠️ File Location:

📂 packages/shared/createGlobalState/index.ts

📦 Imports

Name Source
AnyFn ../utils
effectScope vue

Functions

createGlobalState(stateFactory: Fn): CreateGlobalStateReturn<Fn>

Keep states in the global scope to be reusable across Vue instances.

Parameters:

  • stateFactory any: A factory function to create the state

See: https://vueuse.org/createGlobalState

Tags: @__NO_SIDE_EFFECTS__

Raw 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
 *
 * @__NO_SIDE_EFFECTS__
 */

Calls:

  • effectScope (from vue)
  • scope.run
  • stateFactory
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
}

Type Aliases

CreateGlobalStateReturn<Fn extends AnyFn = AnyFn>

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

Generated by Syntax Scribe