API / @aihu/context

@aihu/context

Runtime core

Async-context-friendly request/SSR context primitives for aihu.

version
0.2.0
exports
10
values
9
types
1
01

_enterContext

function
function _enterContext( parent: Record<symbol, unknown> | null, onOwn: (own: Record<symbol, unknown>) => void, ): [Record<symbol, unknown> | null, boolean, ((own: Record<symbol, unknown>) => void) | null]
02

_exitContext

function
function _exitContext( prev: [Record<symbol, unknown> | null, boolean, ((own: Record<symbol, unknown>) => void) | null], ): void
03

clearSsrContextMap

function
function clearSsrContextMap(): void

Clear the active context map (SSR teardown).

04

contextKey

function
function contextKey(key: string): ContextToken<unknown>

Intern a stable context token for a string key, so the compiler-lowered `$context` provide/consume (which are string-keyed) resolve to one shared token app-wide.

05

createContext

function
function createContext<T>(defaultValue?: T): ContextToken<T>

Create a new context token.

06

inject

function
function inject<T>(token: ContextToken<T>): T | undefined

Read a value for the given token.

07

provide

function
function provide<T>(token: ContextToken<T>, value: T): void

Write a value for the given token into the active context map.

08

runWithContext

function
function runWithContext<R>(map: Map<symbol, unknown>, fn: () => R): R

Run fn() with the given map as the active context map, then restore the previous state (null) in a finally block.

09

setSsrContextMap

function
function setSsrContextMap(map: Map<symbol, unknown>): void

Set the active context map (SSR entry point).

10

ContextToken

interface
interface ContextToken<T> {
  readonly _id: symbol
  readonly _default: T | undefined
}

Opaque token that identifies a context slot and carries its default value.