From 367b8ee07f2950c52bc32154199d5a9b55648630 Mon Sep 17 00:00:00 2001 From: Gustavo de Paula Date: Mon, 22 Nov 2021 10:32:00 -0300 Subject: [PATCH] fix: globalThis fallback (#503) --- src/loggerSystem.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/loggerSystem.ts b/src/loggerSystem.ts index e69472d55..fbbd3af2c 100644 --- a/src/loggerSystem.ts +++ b/src/loggerSystem.ts @@ -5,6 +5,11 @@ declare namespace globalThis { let VIRTUOSO_LOG_LEVEL: LogLevel | undefined } +// eslint-disable-next-line @typescript-eslint/no-namespace +declare namespace window { + let VIRTUOSO_LOG_LEVEL: LogLevel | undefined +} + export enum LogLevel { DEBUG, INFO, @@ -26,11 +31,13 @@ const CONSOLE_METHOD_MAP = { [LogLevel.ERROR]: 'error', } as const +const getGlobalThis = () => (typeof globalThis === 'undefined' ? window : globalThis) + export const loggerSystem = u.system( () => { const logLevel = u.statefulStream(LogLevel.ERROR) const log = u.statefulStream((label: string, message: any, level: LogLevel = LogLevel.INFO) => { - const currentLevel = (globalThis || window)['VIRTUOSO_LOG_LEVEL'] ?? u.getValue(logLevel) + const currentLevel = getGlobalThis()['VIRTUOSO_LOG_LEVEL'] ?? u.getValue(logLevel) if (level >= currentLevel) { console[CONSOLE_METHOD_MAP[level]]('%creact-virtuoso: %c%s %o', 'color: #0253b3; font-weight: bold', 'color: black', label, message) }