/** * Copyright (c) 2025 Elara AI Pty Ltd * Dual-licensed under AGPL-3.0 and commercial license. See LICENSE for details. */ /** * React hooks and provider for ReactiveDataset platform functions. * * @packageDocumentation */ import { type ReactNode } from "react"; import type { TreePath } from "@elaraai/e3-types"; import { type ReactiveDatasetCacheInterface } from "./dataset-store.js"; /** * Props for {@link ReactiveDatasetProvider}. * * @property children - Subtree that should see the dataset cache. */ export interface ReactiveDatasetProviderProps { children: ReactNode; /** * The interval the cache polls a workspace at while what it shows * changes, in milliseconds, unless a watch asks for a shorter one * ({@link ReactiveDatasetCacheConfig.pollIntervalMs}). Default 1000. */ pollIntervalMs?: number; /** * The longest the cache's poll of a workspace backs off to while nothing * it shows changes, in milliseconds, unless a watch asks for a shorter one * ({@link ReactiveDatasetCacheConfig.pollBackoffMs}). Default 5000. */ pollBackoffMs?: number; } /** * Provide a {@link ReactiveDatasetCache} to the component tree. * * @remarks * Reads server identity from the surrounding `` to build a * default {@link DatasetApi} adapter and a workspace-scoped cache, and installs * them — with the `Func.bind`, `Data.bindPaged` and `Record.bind` adapters — in * the process-global runtimes the East-side bindings resolve through. The * cache's scheduler uses `queueMicrotask`, so cache notifications never fire * during a React render pass. * * The children render once the provider's cache is installed, so a * `Data.bind`'s first read finds it; they render again from nothing when the * server identity changes. A provider tears down only what it installed, so * one replacing another — re-keyed to a new workspace — or StrictMode's * mount → unmount → mount never leaves the runtimes empty. * * Must be mounted inside an `` (which also owns the * `` wrap that this package's TanStack hooks * depend on). Throws otherwise. * * @example * ```tsx * * * * * * ``` */ export declare function ReactiveDatasetProvider({ children, pollIntervalMs, pollBackoffMs, }: ReactiveDatasetProviderProps): import("react/jsx-runtime").JSX.Element | null; /** * Hook to access the ReactiveDatasetCache from context. * * @returns The ReactiveDatasetCache instance * @throws Error if used outside of a ReactiveDatasetProvider */ export declare function useReactiveDatasetCache(): ReactiveDatasetCacheInterface; /** * Like {@link useReactiveDatasetCache} but returns `null` instead of throwing * when used outside a ``. Use this when a component * has a graceful fallback (e.g. accepts an explicit config prop too). */ export declare function useReactiveDatasetCacheOptional(): ReactiveDatasetCacheInterface | null; /** * Hook to subscribe to reactive dataset cache changes using React 18's useSyncExternalStore. * * @returns The current snapshot version */ export declare function useReactiveDatasetCacheSubscription(): number; /** * Hook to subscribe to a specific reactive dataset key. * * @param workspace - The workspace name * @param path - The dataset path * @returns The cached value, or undefined if not loaded */ export declare function useReactiveDatasetKey(workspace: string, path: TreePath): Uint8Array | undefined; /** * Reactive dataset to preload. */ export interface ReactiveDatasetToPreload { workspace: string; path: TreePath; } /** * Result of usePreloadReactiveDatasets hook. */ export interface PreloadReactiveDatasetsResult { /** True while the first preload is in flight */ loading: boolean; /** The latest failure, while the preload is failing; it is tried again * until it succeeds */ error: Error | null; /** Preload every dataset again, at once */ reload: () => void; } /** * Hook to preload reactive datasets before rendering. * * @remarks * A preload that fails is tried again for as long as the hook is mounted, so a * view recovers by itself from a network error, a repository whose upgrade is * being applied, or a dataset that moved while it was read. The waits back off * from 1 s to 30 s, each a random point in the second half of its own * ({@link recoveryDelay}), so views that failed together do not try again * together. `error` is the latest failure meanwhile. It clears as soon as the * cache holds every dataset that failed, whoever brought them in: a status poll * that watches a dataset fetches it once it can be read, usually before the * next try. * * @param datasets - Array of datasets to preload * @returns Loading state and error * * @example * ```tsx * import { variant } from "@elaraai/east"; * * function MyComponent() { * const { loading, error } = usePreloadReactiveDatasets([ * { workspace: "production", path: [variant("field", "inputs"), variant("field", "config")] }, * { workspace: "production", path: [variant("field", "data")] }, * ]); * * if (loading) return ; * if (error) return ; * * return ; * } * ``` */ export declare function usePreloadReactiveDatasets(datasets: ReactiveDatasetToPreload[]): PreloadReactiveDatasetsResult; /** * Props for the ReactiveDatasetLoader component. */ export interface ReactiveDatasetLoaderProps { /** Datasets to preload */ datasets: ReactiveDatasetToPreload[]; /** Children to render when loaded */ children: ReactNode; /** Loading fallback */ fallback?: ReactNode; /** Error render function */ onError?: (error: Error, reload: () => void) => ReactNode; } /** * Component that preloads reactive datasets before rendering children. * * @example * ```tsx * import { variant } from "@elaraai/east"; * * function App() { * return ( * * } * onError={(err, reload) => } * > * * * * ); * } * ``` */ export declare function ReactiveDatasetLoader({ datasets, children, fallback, onError, }: ReactiveDatasetLoaderProps): import("react/jsx-runtime").JSX.Element; /** * Hook to write to a reactive dataset from React code. * * @returns A function to write to a dataset * * @example * ```tsx * import { encodeBeast2For, IntegerType, variant } from "@elaraai/east"; * * function UpdateButton() { * const writeDataset = useReactiveDatasetWrite(); * * const handleUpdate = async () => { * await writeDataset( * "production", * [variant("field", "inputs"), variant("field", "count")], * encodeBeast2For(IntegerType)(42n) * ); * }; * * return ; * } * ``` */ export declare function useReactiveDatasetWrite(): (workspace: string, path: TreePath, value: Uint8Array) => Promise; /** * Hook to check if a reactive dataset is cached. * * @param workspace - The workspace name * @param path - The dataset path * @returns True if the dataset is cached */ export declare function useReactiveDatasetHas(workspace: string, path: TreePath): boolean; //# sourceMappingURL=dataset-hooks.d.ts.map