📄 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:
-
Parameters:
stateFactory: Fn- Return Type:
CreateGlobalStateReturn<Fn> - Calls:
effectScope (from vue)scope.runstateFactory