/ utils / settings / internalWrites.ts
internalWrites.ts
 1  /**
 2   * Tracks timestamps of in-process settings-file writes so the chokidar watcher
 3   * in changeDetector.ts can ignore its own echoes.
 4   *
 5   * Extracted from changeDetector.ts to break the settings.ts → changeDetector.ts →
 6   * hooks.ts → … → settings.ts cycle. settings.ts needs to mark "I'm about to
 7   * write" before the write lands; changeDetector needs to read the mark when
 8   * chokidar fires. The map is the only shared state — everything else in
 9   * changeDetector (chokidar, hooks, mdm polling) is irrelevant to settings.ts.
10   *
11   * Callers pass resolved paths. The path→source resolution (getSettingsFilePathForSource)
12   * lives in settings.ts, so settings.ts does it before calling here. No imports.
13   */
14  
15  const timestamps = new Map<string, number>()
16  
17  export function markInternalWrite(path: string): void {
18    timestamps.set(path, Date.now())
19  }
20  
21  /**
22   * True if `path` was marked within `windowMs`. Consumes the mark on match —
23   * the watcher fires once per write, so a matched mark shouldn't suppress
24   * the next (real, external) change to the same file.
25   */
26  export function consumeInternalWrite(path: string, windowMs: number): boolean {
27    const ts = timestamps.get(path)
28    if (ts !== undefined && Date.now() - ts < windowMs) {
29      timestamps.delete(path)
30      return true
31    }
32    return false
33  }
34  
35  export function clearInternalWrites(): void {
36    timestamps.clear()
37  }