/** * Atlas Rebuild Trigger API * * Provides external API for triggering Atlas rebuilds and receiving notifications. * This is the public interface for LexRunner and other external callers. * * Features: * - triggerAtlasRebuild(): Promise-based trigger that resolves on completion * - Callback registration for rebuild events * - Rate-limiting via debounce to prevent excessive rebuilds * * @module atlas/trigger */ import type { Frame } from "../types/frame.js"; import { type Atlas } from "./rebuild.js"; import { type ValidationResult } from "./validate.js"; /** * Result of an Atlas rebuild operation */ export interface RebuildResult { /** Whether the rebuild completed successfully */ success: boolean; /** The rebuilt Atlas (only present on success) */ atlas?: Atlas; /** Validation result from Atlas integrity check */ validation?: ValidationResult; /** Error message (only present on failure) */ error?: string; /** Time taken to complete rebuild in milliseconds */ durationMs: number; /** Number of frames processed */ frameCount: number; /** Timestamp when rebuild completed */ timestamp: string; } /** * Callback function type for rebuild completion events */ export type RebuildCallback = (result: RebuildResult) => void; /** * Configuration for AtlasRebuildManager */ export interface AtlasRebuildManagerConfig { /** Debounce interval in milliseconds (default: 1000 = 1s) */ debounceMs?: number; /** Whether to validate Atlas after rebuild (default: true) */ validateAfterRebuild?: boolean; /** Callback to fetch all frames for rebuild */ fetchFrames: () => Promise | Frame[]; } /** * Atlas Rebuild Manager * * Singleton manager for Atlas rebuilds with Promise-based trigger API, * callback registration, and rate-limiting via debounce. * * Usage: * ```typescript * const manager = new AtlasRebuildManager({ * fetchFrames: () => getAllFrames(getDb()), * debounceMs: 1000, * }); * * // Register callbacks * const unsubscribe = manager.onRebuildComplete((result) => { * console.log('Rebuild complete:', result); * }); * * // Trigger rebuild and wait for result * const result = await manager.triggerRebuild(); * * // Cleanup * unsubscribe(); * manager.dispose(); * ``` */ export declare class AtlasRebuildManager { private config; private callbacks; private debounceTimer; private isRebuilding; private pendingPromises; constructor(config: AtlasRebuildManagerConfig); /** * Trigger an Atlas rebuild * * If a rebuild is already in progress or pending, this call will join * the existing operation and receive the same result. Multiple rapid * calls are debounced to prevent excessive rebuilds. * * @returns Promise that resolves with the rebuild result */ triggerRebuild(): Promise; /** * Execute the actual rebuild operation */ private executeRebuild; /** * Resolve all pending promises with the given result */ private resolvePendingPromises; /** * Notify all registered callbacks with the rebuild result */ private notifyCallbacks; /** * Register a callback for rebuild completion events * * @param callback - Function to call when rebuild completes * @returns Unsubscribe function to remove the callback */ onRebuildComplete(callback: RebuildCallback): () => void; /** * Remove a rebuild completion callback * * @param callback - The callback to remove * @returns true if callback was found and removed, false otherwise */ removeRebuildCallback(callback: RebuildCallback): boolean; /** * Check if a rebuild is currently in progress */ isRebuildInProgress(): boolean; /** * Get the number of registered callbacks */ getCallbackCount(): number; /** * Dispose the manager and cancel any pending rebuilds */ dispose(): void; } /** * Initialize the global Atlas rebuild manager * * Must be called before using triggerAtlasRebuild() or onRebuildComplete(). * * @param config - Manager configuration * @returns The initialized manager */ export declare function initAtlasRebuildManager(config: AtlasRebuildManagerConfig): AtlasRebuildManager; /** * Get the global Atlas rebuild manager * * @throws Error if manager has not been initialized */ export declare function getAtlasRebuildManager(): AtlasRebuildManager; /** * Trigger an Atlas rebuild using the global manager * * Convenience function that delegates to the global manager. * * @returns Promise that resolves with the rebuild result * @throws Error if manager has not been initialized */ export declare function triggerAtlasRebuild(): Promise; /** * Register a callback for rebuild completion events on the global manager * * @param callback - Function to call when rebuild completes * @returns Unsubscribe function to remove the callback * @throws Error if manager has not been initialized */ export declare function onRebuildComplete(callback: RebuildCallback): () => void; /** * Remove a rebuild completion callback from the global manager * * @param callback - The callback to remove * @returns true if callback was found and removed, false otherwise * @throws Error if manager has not been initialized */ export declare function removeRebuildCallback(callback: RebuildCallback): boolean; /** * Reset the global manager (for testing) */ export declare function resetAtlasRebuildManager(): void;