|
| 1 | +import { watch } from "chokidar"; |
| 2 | +import type { LogOutputChannel } from "vscode"; |
| 3 | + |
| 4 | +import { createValueEmitter } from "./emitter.ts"; |
| 5 | +import { createOnceImmediate } from "./once-immediate.ts"; |
| 6 | +import type { SetupStatus } from "./setup.ts"; |
| 7 | + |
| 8 | +export interface StatusTracker { |
| 9 | + status(): SetupStatus | undefined; |
| 10 | + onChange(callback: (status: SetupStatus | undefined) => void): void; |
| 11 | + dispose(): Promise<void>; |
| 12 | + check(): void; |
| 13 | +} |
| 14 | +/** |
| 15 | + * Creates a status tracker that monitors the given files for changes. |
| 16 | + * When a file is added, changed, or deleted, the provided check function is called |
| 17 | + * to determine the current setup status. Emits status changes to registered listeners. |
| 18 | + * |
| 19 | + * @param outputChannel - Channel for logging output and trace messages. |
| 20 | + * @param outputChannelPrefix - Prefix for log messages. |
| 21 | + * @param files - Array of file paths to watch. |
| 22 | + * @param check - Function that returns the current SetupStatus (sync or async). |
| 23 | + * @returns A {@link StatusTracker} instance for querying status, subscribing to changes, and disposing resources. |
| 24 | + */ |
| 25 | +export function createFileStatusTracker( |
| 26 | + outputChannel: LogOutputChannel, |
| 27 | + outputChannelPrefix: string, |
| 28 | + files: string[], |
| 29 | + check: () => Promise<SetupStatus | undefined> | SetupStatus | undefined, |
| 30 | +): StatusTracker { |
| 31 | + // let status: SetupStatus | undefined; |
| 32 | + // const emitter = createEmitter<SetupStatus | undefined>(outputChannel); |
| 33 | + const status = createValueEmitter<SetupStatus | undefined>(); |
| 34 | + |
| 35 | + const updateStatus = createOnceImmediate(async () => { |
| 36 | + const newStatus = await Promise.resolve(check()); |
| 37 | + status.setValue(newStatus); |
| 38 | + // if (status !== newStatus) { |
| 39 | + // status = newStatus; |
| 40 | + // outputChannel.trace( |
| 41 | + // `${outputChannelPrefix} File status changed to ${status}`, |
| 42 | + // ); |
| 43 | + // await emitter.emit(status); |
| 44 | + // } |
| 45 | + }); |
| 46 | + |
| 47 | + const watcher = watch(files) |
| 48 | + .on("change", (path) => { |
| 49 | + outputChannel.trace(`${outputChannelPrefix} ${path} changed`); |
| 50 | + updateStatus(); |
| 51 | + }) |
| 52 | + .on("unlink", (path) => { |
| 53 | + outputChannel.trace(`${outputChannelPrefix} ${path} deleted`); |
| 54 | + updateStatus(); |
| 55 | + }) |
| 56 | + .on("add", (path) => { |
| 57 | + outputChannel.trace(`${outputChannelPrefix} ${path} added`); |
| 58 | + updateStatus(); |
| 59 | + }) |
| 60 | + .on("error", (error) => { |
| 61 | + outputChannel.error(`${outputChannelPrefix} Error watching file`); |
| 62 | + outputChannel.error(error instanceof Error ? error : String(error)); |
| 63 | + }); |
| 64 | + |
| 65 | + // Update the status immediately on file tracker initialization |
| 66 | + void updateStatus(); |
| 67 | + |
| 68 | + return { |
| 69 | + status() { |
| 70 | + return status.value(); |
| 71 | + }, |
| 72 | + onChange(callback) { |
| 73 | + status.onChange(callback); |
| 74 | + // emitter.on(callback); |
| 75 | + // if (status) { |
| 76 | + // callback(status); |
| 77 | + // } |
| 78 | + }, |
| 79 | + async dispose() { |
| 80 | + await watcher.close(); |
| 81 | + }, |
| 82 | + check() { |
| 83 | + return updateStatus(); |
| 84 | + }, |
| 85 | + }; |
| 86 | +} |
0 commit comments