File size: 2,416 Bytes
064bfd6 | 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 78 79 80 81 | import type { SettingSource } from './constants.js'
import type { SettingsJson } from './types.js'
import type { SettingsWithErrors, ValidationError } from './validation.js'
let sessionSettingsCache: SettingsWithErrors | null = null
export function getSessionSettingsCache(): SettingsWithErrors | null {
return sessionSettingsCache
}
export function setSessionSettingsCache(value: SettingsWithErrors): void {
sessionSettingsCache = value
}
/**
* Per-source cache for getSettingsForSource. Invalidated alongside the
* merged sessionSettingsCache — same resetSettingsCache() triggers
* (settings write, --add-dir, plugin init, hooks refresh).
*/
const perSourceCache = new Map<SettingSource, SettingsJson | null>()
export function getCachedSettingsForSource(
source: SettingSource,
): SettingsJson | null | undefined {
// undefined = cache miss; null = cached "no settings for this source"
return perSourceCache.has(source) ? perSourceCache.get(source) : undefined
}
export function setCachedSettingsForSource(
source: SettingSource,
value: SettingsJson | null,
): void {
perSourceCache.set(source, value)
}
/**
* Path-keyed cache for parseSettingsFile. Both getSettingsForSource and
* loadSettingsFromDisk call parseSettingsFile on the same paths during
* startup — this dedupes the disk read + zod parse.
*/
type ParsedSettings = {
settings: SettingsJson | null
errors: ValidationError[]
}
const parseFileCache = new Map<string, ParsedSettings>()
export function getCachedParsedFile(path: string): ParsedSettings | undefined {
return parseFileCache.get(path)
}
export function setCachedParsedFile(path: string, value: ParsedSettings): void {
parseFileCache.set(path, value)
}
export function resetSettingsCache(): void {
sessionSettingsCache = null
perSourceCache.clear()
parseFileCache.clear()
}
/**
* Plugin settings base layer for the settings cascade.
* pluginLoader writes here after loading plugins;
* loadSettingsFromDisk reads it as the lowest-priority base.
*/
let pluginSettingsBase: Record<string, unknown> | undefined
export function getPluginSettingsBase(): Record<string, unknown> | undefined {
return pluginSettingsBase
}
export function setPluginSettingsBase(
settings: Record<string, unknown> | undefined,
): void {
pluginSettingsBase = settings
}
export function clearPluginSettingsBase(): void {
pluginSettingsBase = undefined
}
|