-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathconfig.ts
More file actions
47 lines (40 loc) · 1.5 KB
/
Copy pathconfig.ts
File metadata and controls
47 lines (40 loc) · 1.5 KB
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
// Global configuration storage for the tsjs runtime (mode, logging, etc.).
import { log, LogLevel } from './log';
import type { Config, GamConfig } from './types';
let CONFIG: Config = { mode: 'render' };
// Lazy import to avoid circular dependencies - GAM integration may not be present
let setGamConfigFn: ((cfg: GamConfig) => void) | null | undefined = undefined;
function getSetGamConfig(): ((cfg: GamConfig) => void) | null {
if (setGamConfigFn === undefined) {
try {
// Dynamic import path - bundler will include if gam integration is present
// eslint-disable-next-line @typescript-eslint/no-require-imports
const gam = require('../integrations/gam/index');
setGamConfigFn = gam.setGamConfig || null;
} catch {
// GAM integration not available
setGamConfigFn = null;
}
}
return setGamConfigFn ?? null;
}
// Merge publisher-provided config and adjust the log level accordingly.
export function setConfig(cfg: Config): void {
CONFIG = { ...CONFIG, ...cfg };
const debugFlag = cfg.debug;
const l = cfg.logLevel as LogLevel | undefined;
if (typeof l === 'string') log.setLevel(l);
else if (debugFlag === true) log.setLevel('debug');
// Forward GAM config to the GAM integration if present
if (cfg.gam) {
const setGam = getSetGamConfig();
if (setGam) {
setGam(cfg.gam);
}
}
log.info('setConfig:', cfg);
}
// Return a defensive copy so callers can't mutate shared state.
export function getConfig(): Config {
return { ...CONFIG };
}