Skip to content

⬅️ Back to Table of Contents

📄 provideLocal

📊 Analysis Summary

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

📚 Table of Contents

🛠️ File Location:

📂 packages/shared/provideLocal/index.ts

📦 Imports

Name Source
InjectionKey vue
LocalProvidedKey ./map
getCurrentInstance vue
getCurrentScope vue
provide vue
localProvidedStateMap ./map

Functions

provideLocal(key: K, value: K extends InjectionKey<infer V> ? V : T): ProvideLocalReturn

On the basis of provide, it is allowed to directly call inject to obtain the value after call provide in the same component.

Examples:

```ts
provideLocal('MyInjectionKey', 1)
const injectedValue = injectLocal('MyInjectionKey') // injectedValue === 1
<details><summary>Raw JSDoc</summary>

```typescript
/**
 * On the basis of `provide`, it is allowed to directly call inject to obtain the value after call provide in the same component.
 *
 * @example
 * ```ts
 * provideLocal('MyInjectionKey', 1)
 * const injectedValue = injectLocal('MyInjectionKey') // injectedValue === 1
 * ```
 */

Calls:

  • getCurrentInstance (from vue)
  • getCurrentScope (from vue)
  • localProvidedStateMap.has
  • localProvidedStateMap.set
  • Object.create
  • localProvidedStateMap.get
  • provide (from vue)

Internal Comments:

// @ts-expect-error allow InjectionKey as key (x4)

Code
export function provideLocal<T, K = LocalProvidedKey<T>>(key: K, value: K extends InjectionKey<infer V> ? V : T): ProvideLocalReturn {
  const instance = getCurrentInstance()?.proxy
  const owner = instance ?? getCurrentScope()

  if (owner == null)
    throw new Error('provideLocal must be called in setup')

  if (!localProvidedStateMap.has(owner))
    localProvidedStateMap.set(owner, Object.create(null))

  const localProvidedState = localProvidedStateMap.get(owner)!
  // @ts-expect-error allow InjectionKey as key
  localProvidedState[key] = value
  return provide<T, K>(key, value)
}

Type Aliases

ProvideLocalReturn

type ProvideLocalReturn = void;

Generated by Syntax Scribe