@intuitionrobotics/ts-common
Version:
36 lines • 1.45 kB
JavaScript
import { deepClone } from "../utils/object-tools.js";
import {} from "../utils/types.js";
/**
* Process-level application config store.
*
* One frozen copy of the app config lives in the process; modules resolve their
* own slice lazily (and memoize a private mutable copy) on first `this.config`
* access — see Module. This replaces the old eager loop that deep-merged a
* config slice into every registered module's memory whether it was used or not.
*/
let appConfig;
// Bumped on every setAppConfig() so Module memos resolved against an older
// store version re-resolve on next access (keeps test harnesses, which set
// config per run, order-independent).
let configEpoch = 0;
function deepFreeze(obj) {
if (obj && typeof obj === "object" && !Object.isFrozen(obj)) {
Object.freeze(obj);
Object.values(obj).forEach(deepFreeze);
}
return obj;
}
/** Store the app config (one frozen copy per process). Call once at bootstrap, before build(). */
export function setAppConfig(config) {
appConfig = deepFreeze(deepClone(config || {}));
configEpoch++;
}
/** The frozen config slice for a module name, or undefined if none was provided. */
export function getModuleConfigSlice(moduleName) {
return appConfig?.[moduleName];
}
/** @see setAppConfig — used by Module to invalidate stale config memos. */
export function getConfigEpoch() {
return configEpoch;
}
//# sourceMappingURL=app-config.js.map