All files / backend/dist/services/workspace model-watcher.js

0% Statements 0/19
0% Branches 0/2
0% Functions 0/7
0% Lines 0/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77                                                                                                                                                         
/**
 * Model Watcher
 *
 * Watches the quiqr/model directory for changes and clears the workspace
 * configuration cache when files are added, changed, or removed.
 */
import chokidar from 'chokidar';
import path from 'path';
/**
 * ModelWatcher - Watches model directory for changes
 *
 * When any file in {workspacePath}/quiqr/model changes,
 * it clears the WorkspaceConfigProvider cache.
 */
export class ModelWatcher {
    watcher;
    workspacePath;
    workspaceConfigProvider;
    onCacheCleared;
    constructor(options) {
        this.workspacePath = options.workspacePath;
        this.workspaceConfigProvider = options.workspaceConfigProvider;
        this.onCacheCleared = options.onCacheCleared;
    }
    /**
     * Start watching the model directory
     */
    start() {
        // Stop any existing watcher first
        this.stop();
        const watchDir = path.join(this.workspacePath, 'quiqr', 'model');
        const watchOptions = {
            ignored: /(^|[/\\])\../, // ignore dotfiles
            persistent: true,
            ignoreInitial: true, // Don't fire events for existing files on startup
        };
        this.watcher = chokidar.watch(watchDir, watchOptions);
        const handleChange = () => {
            this.workspaceConfigProvider.clearCache();
            this.onCacheCleared?.();
        };
        this.watcher
            .on('add', handleChange)
            .on('change', handleChange)
            .on('unlink', handleChange);
    }
    /**
     * Stop watching and clean up resources
     */
    async stop() {
        if (this.watcher) {
            await this.watcher.close();
            this.watcher = undefined;
        }
    }
    /**
     * Check if the watcher is currently active
     */
    isWatching() {
        return this.watcher !== undefined;
    }
    /**
     * Get the path being watched
     */
    getWatchPath() {
        return path.join(this.workspacePath, 'quiqr', 'model');
    }
}
/**
 * Factory function to create and start a ModelWatcher
 */
export function createModelWatcher(options) {
    const watcher = new ModelWatcher(options);
    watcher.start();
    return watcher;
}