UNPKG

@stacksjs/stx

Version:

A performant UI Framework. Powered by Bun.

140 lines (138 loc) 3.69 kB
/** * Get a store by name * * @example * ```ts * const appStore = useStore('appStore') * console.log(appStore.state.count) * * appStore.setState({ count: appStore.state.count + 1 }) * ``` */ export declare function useStore<T = unknown>(name: string): StoreRef<T> | null; /** * Get a store, throwing if not found */ export declare function useStoreOrThrow<T = unknown>(name: string): StoreRef<T>; /** * Check if a store exists */ export declare function hasStore(name: string): boolean; /** * Get all store names */ export declare function getStoreNames(): string[]; /** * Wait for a store to be ready (for async initialization) */ export declare function waitForStore<T = unknown>(name: string, timeout?: any): Promise<StoreRef<T>>; /** * Get current route parameters * * @example * ```ts * // URL: /users/123 * const params = useRouteParams() * console.log(params.id) // '123' * ``` */ export declare function useRouteParams(): RouteParams; /** * Get a specific route parameter * * @example * ```ts * const userId = useRouteParam('id') * ``` */ export declare function useRouteParam(name: string): string | undefined; /** * Get a route parameter or throw if missing */ export declare function useRouteParamOrThrow(name: string): string; /** * Get state passed from middleware * * @example * ```ts * // In middleware: * ctx.state.user = { id: 1, name: 'John' } * * // In component: * const state = useMiddlewareState() * console.log(state.user) * ``` */ export declare function useMiddlewareState<T = MiddlewareState>(): T; /** * Get a specific middleware state value */ export declare function useMiddlewareValue<T = unknown>(key: string): T | undefined; /** * @internal * Initialize the store registry */ export declare function initStoreRegistry(): void; /** * @internal * Register a store in the registry */ export declare function registerStore(name: string, store: StoreRef): void; /** * @internal * Get serialized store state for hydration */ export declare function getHydratedStoreState(): Record<string, unknown> | null; /** * @internal * Set route params (called by router) */ export declare function setRouteParams(params: RouteParams): void; /** * @internal * Set middleware state (called by middleware runner) */ export declare function setMiddlewareState(state: MiddlewareState): void; /** * Check if running on the server */ export declare function isServer(): boolean; /** * Check if running on the client */ export declare function isClient(): boolean; /** * Run callback only on client * * @example * ```ts * onClient(() => { * document.title = 'Hello' * }) * ``` */ export declare function onClient(callback: () => void): void; /** * Run callback only on server */ export declare function onServer(callback: () => void): void; // ============================================================================= // Store Access // ============================================================================= export declare interface StoreRef<T = unknown> { state: T getState: () => T setState: (updater: Partial<T> | ((state: T) => Partial<T>)) => void subscribe: (listener: (state: T) => void) => () => void reset: () => void } // ============================================================================= // Route Params // ============================================================================= export declare interface RouteParams { } // ============================================================================= // Middleware State // ============================================================================= export declare interface MiddlewareState { }