MCPcopy Create free account
hub / github.com/freecodexyz/free-code / startGlobalConfigFreshnessWatcher

Function startGlobalConfigFreshnessWatcher

src/utils/config.ts:1017–1054  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1015// fs.watchFile polls stat on the libuv threadpool and only calls us when mtime
1016// changed — a stalled stat never blocks the main thread.
1017function startGlobalConfigFreshnessWatcher(): void {
1018 if (freshnessWatcherStarted || process.env.NODE_ENV === 'test') return
1019 freshnessWatcherStarted = true
1020 const file = getGlobalClaudeFile()
1021 watchFile(
1022 file,
1023 { interval: CONFIG_FRESHNESS_POLL_MS, persistent: false },
1024 curr => {
1025 // Our own writes fire this too — the write-through's Date.now()
1026 // overshoot makes cache.mtime > file mtime, so we skip the re-read.
1027 // Bun/Node also fire with curr.mtimeMs=0 when the file doesn't exist
1028 // (initial callback or deletion) — the <= handles that too.
1029 if (curr.mtimeMs <= globalConfigCache.mtime) return
1030 void getFsImplementation()
1031 .readFile(file, { encoding: 'utf-8' })
1032 .then(content => {
1033 // A write-through may have advanced the cache while we were reading;
1034 // don't regress to the stale snapshot watchFile stat'd.
1035 if (curr.mtimeMs <= globalConfigCache.mtime) return
1036 const parsed = safeParseJSON(stripBOM(content))
1037 if (parsed === null || typeof parsed !== 'object') return
1038 globalConfigCache = {
1039 config: migrateConfigFields({
1040 ...createDefaultGlobalConfig(),
1041 ...(parsed as Partial<GlobalConfig>),
1042 }),
1043 mtime: curr.mtimeMs,
1044 }
1045 lastReadFileStats = { mtime: curr.mtimeMs, size: curr.size }
1046 })
1047 .catch(() => {})
1048 },
1049 )
1050 registerCleanup(async () => {
1051 unwatchFile(file)
1052 freshnessWatcherStarted = false
1053 })
1054}
1055
1056// Write-through: what we just wrote IS the new config. cache.mtime overshoots
1057// the file's real mtime (Date.now() is recorded after the write) so the

Callers 1

getGlobalConfigFunction · 0.85

Calls 6

stripBOMFunction · 0.90
getFsImplementationFunction · 0.85
migrateConfigFieldsFunction · 0.85
registerCleanupFunction · 0.85
readFileMethod · 0.80

Tested by

no test coverage detected