import type { AppContext } from "../app-types.js"; import type { InjectionToken } from "./token.js"; /** * Swap the active app context, returning the previous one so the caller can * restore it (nested runWithContext calls). Used by `app.runWithContext()`. * @internal */ export declare function setActiveAppContext(context: AppContext | null): AppContext | null; /** * Sentinel distinguishing "not provided" from an explicitly provided * `undefined` — `lookupProvidedEntry` returns it on a lookup miss. * @internal */ export declare const NOT_PROVIDED: unique symbol; /** * Lookup a provided entry by token, traversing the component tree. * The AppContext is provided at the root component level, so it's found * just like any other provided value. * On a tree miss, falls back to the app-level provides of the AppContext * found at the root — a live read, so `app.defineProvide` calls made after * mount are visible too. Outside any component, falls back to the app * context made current by `app.runWithContext(fn)`. * Returns `NOT_PROVIDED` when the token is not provided anywhere, so callers * can tell a lookup miss from an explicitly provided `undefined`. * @internal */ export declare function lookupProvidedEntry(token: symbol): unknown; /** * Like `lookupProvidedEntry`, but conflates "not provided" with a provided * `undefined`. Exported for the factory system's scoped-lifetime resolution * and internal seam reads that never provide `undefined`. * The `InjectionToken` overload infers the value type from the token. * @internal */ export declare function lookupProvided(token: InjectionToken): T | undefined; export declare function lookupProvided(token: symbol): T | undefined; /** * Injectable function type with metadata */ export interface InjectableFunction { (): T; _factory: () => T; _token: symbol; } /** * Options for the factory form of `defineInjectable`. */ export interface DefineInjectableOptions { /** * Diagnostics name for this injectable — the token description used by dev * warnings and devtools. Without it the name comes from `factory.name`, * which inline arrow factories don't have. */ name?: string; } /** * Options for the required (name-string) form of `defineInjectable`. */ export interface DefineRequiredInjectableOptions { /** * Replaces the generated `suggestion` on the SIGX202 error thrown when the * injectable is used unprovided. Use it when the remedy is not * `defineProvide` — e.g. a pack whose injectable is satisfied by rendering * something: `'Render the component as a route inside .'` * * Read only in dev builds, but note the string lives in YOUR module, so it * ships in your production bundle regardless. Gate it yourself if the bytes * matter: `hint: __DEV__ ? '...' : undefined`. */ hint?: string; } /** * The metadata defineProvide actually needs — satisfied by both * InjectableFunction and parameterized FactoryFunction use-functions. */ export interface Providable { _factory: () => T; _token: symbol; } /** * Define an injectable service/value that can be provided at app or component level. * * The returned function can be called to get the current instance: * - If provided at component level via `defineProvide()`, returns that instance * - If provided at app level via `app.defineProvide()`, returns that instance * - Otherwise falls back to a global singleton created by the factory * * Pass a **name string** instead of a factory to declare a *required* * injectable: there is no fallback, and using it without a provider throws a * structured error (SIGX202) naming the injectable. Use this for services * that must be provided per app — e.g. per-request services under SSR. * * Each form takes its own options: `{ name }` on the factory form gives * diagnostics something to call an injectable whose factory is an inline arrow * (see {@link DefineInjectableOptions}); `{ hint }` on the required form * replaces the SIGX202 suggestion when the remedy isn't `defineProvide` (see * {@link DefineRequiredInjectableOptions}). * * @example * ```typescript * // Define a service with a zero-config fallback * const useApiConfig = defineInjectable(() => ({ * baseUrl: 'https://api.example.com' * })); * * // Use it in any component - gets nearest provided instance or global singleton * const config = useApiConfig(); * console.log(config.baseUrl); * * // A required service: no fallback, must be provided * const useRouter = defineInjectable('Router'); * app.defineProvide(useRouter, () => createRouter(url)); * * // A pack whose injectable is satisfied by rendering, not by defineProvide * const useScreen = defineInjectable('Screen', { * hint: 'Render the component as a route inside .', * }); * ``` */ export declare function defineInjectable(factory: () => T, options?: DefineInjectableOptions): InjectableFunction; export declare function defineInjectable(name: string, options?: DefineRequiredInjectableOptions): InjectableFunction; /** * Provide a new instance of an injectable at the current component level. * Child components will receive this instance when calling the injectable function. * * @param useFn - A use-function created by defineInjectable or defineFactory * @param factory - Optional custom factory to create the instance (overrides default) * * @example * ```typescript * const useApiConfig = defineInjectable(() => ({ baseUrl: 'https://api.example.com' })); * * const MyComponent = component(() => { * // Create and provide a new instance for this subtree * const config = defineProvide(useApiConfig); * config.baseUrl = 'https://custom.api.com'; * * return () => ; * }); * * // Or provide a pre-constructed instance: * const MyComponent2 = component(() => { * const customService = createMyService({ custom: 'options' }); * defineProvide(useMyService, () => customService); * * return () => ; * }); * ``` */ export declare function defineProvide(useFn: Providable, factory?: () => T): T; /** * Get the current AppContext from the component tree. * The AppContext is provided at the root component level during mount/hydrate/SSR. * Outside any component, returns the context made current by * `app.runWithContext(fn)`, or null. * * @example * ```typescript * const appContext = useAppContext(); * console.log(appContext?.app); * ``` */ export declare function useAppContext(): AppContext | null; /** * Get the AppContext token. * Used by renderers to provide the AppContext at the root component level. * @internal */ export declare function getAppContextToken(): symbol; /** * Provide the AppContext on a component's provides Map. * Called by the renderer for the ROOT component only. * @internal */ export declare function provideAppContext(ctx: unknown, appContext: AppContext): void; //# sourceMappingURL=injectable.d.ts.map