/** * Copyright (c) 2025 Elara AI Pty Ltd * Dual-licensed under AGPL-3.0 and commercial license. See LICENSE for details. */ /** * Runtime implementation for the unified `Data.bind` platform function. * * Replaces three older runtimes (direct / staged / overlay) with one * mode-dispatched implementation. The (mode, patch?) combination resolved * at call time selects which closures to install for `read` / `write` / * `commit` / `discard`. * * State (queue, registry, cache singleton, tracking context) is * encapsulated in the {@link BindRuntime} class. A process-global * {@link defaultBindRuntime} powers the `BindPlatform` export and the * free-function helpers; tests construct their own `BindRuntime` for * isolation. * * @packageDocumentation */ import { type EastTypeValue, type ValueTypeOf, OptionType } from "@elaraai/east"; import { type PlatformFunction } from "@elaraai/east/internal"; import { type DataManifest, type DataBindModeLiteral, DataBindModeType } from "@elaraai/e3-ui/internal"; import { type TreePath } from "@elaraai/e3-types"; import { type ReactiveDatasetCacheInterface } from "./dataset-store.js"; import { type StagedStoreInterface } from "./staged-store.js"; /** * Per-binding metadata a binding's readers need to decode its source and * patch bytes: the Ontology editor's commit, the decision handle's views and * the staged store's decoders. Keyed by `datasetCacheKey(workspace, * sourcePath)`. */ export interface BindingTypes { readonly workspace: string; readonly sourceType: EastTypeValue; readonly patchType: EastTypeValue; readonly mode: DataBindModeLiteral; readonly hasPatchDataset: boolean; } /** The struct returned by `Data.bind` — an East-side handle on a * dataset binding. JS-side test callers use the same shape. */ export interface BindHandle { read: () => unknown; write: (value: unknown) => null; writeAndStart: (value: unknown) => null; start: () => null; source: () => unknown; pending: () => boolean; commit: () => null; discard: () => null; has: () => boolean; status: () => unknown; binding: { source: TreePath; patch: ValueTypeOf>; mode: ValueTypeOf; }; } /** * A `BindRuntime` owns one set of bind-runtime state: pending writes * queue, error listeners, cache singleton, tracking context, and * binding-types registry. Exists as an instantiable class so tests can * construct an isolated runtime per `describe` rather than wrestling * with shared module state. The default process-global runtime is * {@link defaultBindRuntime}; production callers continue to use the * free-function exports. */ export declare class BindRuntime { private cache; /** The staged store this runtime was given, if any. Without one it uses * the package's ({@link getStagedStore}) as it is when a binding reads or * writes, so a store a host installs (`initializeStagedStore`) reaches * `Data.bind` as it reaches the reactive tracker and the providers: the * default runtime is made as this module loads, before any host can. */ private readonly givenStaged; /** The staged stores whose persistence failures this runtime reports. */ private readonly reported; private readonly pendingWrites; private isProcessingWrites; private drainPromise; private resolveDrain; private readonly writeErrorListeners; private trackingContext; private readonly bindingRegistry; private readonly fallbackWarningEmitted; private readonly listCache; private readonly handleCache; /** * @param staged - The staged store this runtime uses for staged-mode * buffers. Without one, the package's, as it is when a binding uses it. */ constructor(staged?: StagedStoreInterface); /** The staged store a binding buffers in now. */ private get staged(); /** Surface a staged store's persistence failures (IndexedDB save/remove) * through the same channel as dataset write failures, so a host that * registers onWriteError sees 'edit not durably saved' too. Once per * store: a store lives as long as the page, so the subscription is never * torn down. */ private reportPersistErrors; /** Read the active cache. Throws if none has been initialized. */ requireCache(): ReactiveDatasetCacheInterface; /** Read the active cache, returning null if not yet initialized. */ getCache(): ReactiveDatasetCacheInterface | null; /** Set the active cache. When the reference changes, drops any * leftover queued writes that captured the old cache. Same-cache * re-init (Strict Mode double-invoke; HMR) preserves the queue. */ initializeCache(cache: ReactiveDatasetCacheInterface): void; /** * Clear the active cache and drop any queued writes. * * @param cache - Clear only while this is the active cache, so a provider * tearing down after another installed its own leaves that one in place */ clearCache(cache?: ReactiveDatasetCacheInterface): void; /** Subscribe to write-queue errors. Returns an unsubscribe fn. */ onWriteError(cb: (error: unknown) => void): () => void; /** Fan a write/persistence error out to every registered listener. */ private emitWriteError; /** Resolve once every currently-queued write has finished * (success or failure). Errors are NOT thrown — subscribe via * {@link onWriteError} for those. */ awaitPendingWrites(): Promise; /** Drop every queued write without running it. */ clearPendingWrites(): void; /** Queue a write — internal API used by the bind-handle closures. */ private queueWrite; private processWriteQueue; /** * Start recording the datasets an evaluation reads. * * @returns What it records: each dataset's key, with its version as first read */ enableTracking(): ReadonlyMap; /** * Stop recording. * * @returns Each dataset's key the evaluation read, with its version as it first read it — a staged write it made * since stands at another */ disableTracking(): ReadonlyMap; isTracking(): boolean; /** * A dataset's version as a reader of it sees it: its cache's and its * staged buffer's together, which move as either does. * * @param key - The dataset's key ({@link datasetCacheKey}) * @returns Its version */ keyVersion(key: string): number; trackPath(workspace: string, path: TreePath): void; getBindingTypes(workspace: string, sourcePath: TreePath): BindingTypes | undefined; /** Clear binding-registry entries. Pass a workspace to clear only * that workspace's entries. Match is by stored field, not key * prefix — workspace names that share a textual prefix never * collide. */ clearBindingRegistry(workspace?: string): void; private registerBindingTypes; /** * Build a `Data.bind` handle. Its methods are thin IR-bearing * `East.function`s over the `data_*` primitives, capturing only the * plain-data `{source, patch, mode}` descriptor — so the handle is ordinary * serializable East data (issue #106) and re-binds to the decoder's cache. * Public so tests can call it directly without East's compile/dispatch. * * @throws `EastError` if the cache is uninitialized, the workspace is not * configured, or `allowed` is non-null and any referenced path is not in it. */ buildBindHandle(sourceType: EastTypeValue, sourcePath: TreePath, patchPath: TreePath | undefined, mode: DataBindModeLiteral, allowed?: ReadonlySet): BindHandle; /** * The low-level primitives backing handle methods, bound to THIS runtime. * Registered globally (extension registry) and included by the scoped * platform (e3 `ui()` tasks) so a decoded handle re-binds here. Each impl * recovers the `{source, patch, mode}` descriptor from its args and delegates * to {@link buildRawHandle}, reusing the full mode×patch logic verbatim. */ buildPrimitives(): PlatformFunction[]; /** * Build the raw (host-closure) bind handle for a single * `(sourcePath, mode, patch?)` triple. The mode×patch matrix lives here; the * serializable {@link buildBindHandle} wraps each method as an `East.function` * over a primitive that delegates back here. Re-registers binding types on * every call (decode self-heal). Public so tests can drive the raw closures. */ buildRawHandle(sourceType: EastTypeValue, sourcePath: TreePath, patchPath: TreePath | undefined, mode: DataBindModeLiteral): BindHandle; /** Build a `Data.bind` PlatformFunction bound to this runtime. * Pass `allowed=null` for an unscoped impl (the global * `BindPlatform`); pass a Set for manifest scoping. */ buildPlatform(allowed: ReadonlySet | null): PlatformFunction; preloadList(workspace: string, path: TreePath): Promise; clearListCache(): void; } /** Process-global runtime backing the `BindPlatform` export and the * free-function helpers. */ export declare const defaultBindRuntime: BindRuntime; /** Read the active cache singleton. Throws if no provider has been mounted. */ export declare function getReactiveDatasetCache(): ReactiveDatasetCacheInterface; /** Set the active cache singleton — called by the React provider on mount. */ export declare function initializeReactiveDatasetCache(cache: ReactiveDatasetCacheInterface): void; /** * Clear the active cache singleton — called on unmount or test teardown. Also * drops any queued writes that captured the old cache. * * @param cache - Clear only while this is the active cache */ export declare function clearReactiveDatasetCache(cache?: ReactiveDatasetCacheInterface): void; export declare function enableBindingTracking(): ReadonlyMap; export declare function disableBindingTracking(): ReadonlyMap; export declare function isBindingTracking(): boolean; export declare function trackDatasetPath(workspace: string, path: TreePath): void; export declare function getBindingTypes(workspace: string, sourcePath: TreePath): BindingTypes | undefined; export declare function clearBindingRegistry(workspace?: string): void; /** Subscribe to write-queue errors. Called once for every failed write. */ export declare function onWriteError(cb: (error: unknown) => void): () => void; /** Resolve once every currently-queued write has finished. */ export declare function awaitPendingWrites(): Promise; /** Drop every queued write without running it. */ export declare function clearPendingWrites(): void; /** Global, manifest-unscoped `Data.bind` impl + its backing primitives. * Registered on module load (powers the extension registry decode path). */ export declare const BindPlatform: PlatformFunction[]; /** Build a manifest-scoped `Data.bind` implementation + its backing primitives. * * The `data_*` primitives MUST ship with the scoped platform: e3 `ui()` tasks * render through `createScoped*()` arrays (UITaskPreview), NOT the global * registry, so a serialized handle's methods would otherwise decode to * "Platform function 'data_read' is not available". */ export declare function createScopedBindPlatform(manifest: DataManifest): PlatformFunction[]; export declare function preloadReactiveDatasetList(workspace: string, path: TreePath): Promise; export declare function clearReactiveDatasetListCache(): void; //# sourceMappingURL=bind-runtime.d.ts.map