πŸ“„ File detail

utils/settings/internalWrites.ts

🧩 .tsπŸ“ 38 linesπŸ’Ύ 1,380 bytesπŸ“ text
← Back to All Files

🎯 Use case

This file lives under β€œutils/”, which covers cross-cutting helpers (shell, tempfiles, settings, messages, process input, …). On the API surface it exposes markInternalWrite, consumeInternalWrite, and clearInternalWrites β€” mainly functions, hooks, or classes. What the file header says: Tracks timestamps of in-process settings-file writes so the chokidar watcher in changeDetector.ts can ignore its own echoes. Extracted from changeDetector.ts to break the settings.ts β†’ changeDetector.ts β†’ hooks.ts β†’ … β†’ settings.ts cycle. settings.ts needs to mark "I'm about to w.

Generated from folder role, exports, dependency roots, and inline comments β€” not hand-reviewed for every path.

🧠 Inline summary

Tracks timestamps of in-process settings-file writes so the chokidar watcher in changeDetector.ts can ignore its own echoes. Extracted from changeDetector.ts to break the settings.ts β†’ changeDetector.ts β†’ hooks.ts β†’ … β†’ settings.ts cycle. settings.ts needs to mark "I'm about to write" before the write lands; changeDetector needs to read the mark when chokidar fires. The map is the only shared state β€” everything else in changeDetector (chokidar, hooks, mdm polling) is irrelevant to settings.ts. Callers pass resolved paths. The pathβ†’source resolution (getSettingsFilePathForSource) lives in settings.ts, so settings.ts does it before calling here. No imports.

πŸ“€ Exports (heuristic)

  • markInternalWrite
  • consumeInternalWrite
  • clearInternalWrites

πŸ–₯️ Source preview

/**
 * Tracks timestamps of in-process settings-file writes so the chokidar watcher
 * in changeDetector.ts can ignore its own echoes.
 *
 * Extracted from changeDetector.ts to break the settings.ts β†’ changeDetector.ts β†’
 * hooks.ts β†’ … β†’ settings.ts cycle. settings.ts needs to mark "I'm about to
 * write" before the write lands; changeDetector needs to read the mark when
 * chokidar fires. The map is the only shared state β€” everything else in
 * changeDetector (chokidar, hooks, mdm polling) is irrelevant to settings.ts.
 *
 * Callers pass resolved paths. The path→source resolution (getSettingsFilePathForSource)
 * lives in settings.ts, so settings.ts does it before calling here. No imports.
 */

const timestamps = new Map<string, number>()

export function markInternalWrite(path: string): void {
  timestamps.set(path, Date.now())
}

/**
 * True if `path` was marked within `windowMs`. Consumes the mark on match β€”
 * the watcher fires once per write, so a matched mark shouldn't suppress
 * the next (real, external) change to the same file.
 */
export function consumeInternalWrite(path: string, windowMs: number): boolean {
  const ts = timestamps.get(path)
  if (ts !== undefined && Date.now() - ts < windowMs) {
    timestamps.delete(path)
    return true
  }
  return false
}

export function clearInternalWrites(): void {
  timestamps.clear()
}