From 1e9d38c2e6e170abde6cfeaf7b2024d8b456f906 Mon Sep 17 00:00:00 2001 From: omg Date: Wed, 9 Dec 2020 17:34:56 +0200 Subject: [PATCH] cache enabled status per-logger (#799) Co-authored-by: Qix --- src/common.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/common.js b/src/common.js index 392a8e00..50ce2925 100644 --- a/src/common.js +++ b/src/common.js @@ -60,6 +60,8 @@ function setup(env) { function createDebug(namespace) { let prevTime; let enableOverride = null; + let namespacesCache; + let enabledCache; function debug(...args) { // Disabled? @@ -120,7 +122,17 @@ function setup(env) { Object.defineProperty(debug, 'enabled', { enumerable: true, configurable: false, - get: () => enableOverride === null ? createDebug.enabled(namespace) : enableOverride, + get: () => { + if (enableOverride !== null) { + return enableOverride; + } + if (namespacesCache !== createDebug.namespaces) { + namespacesCache = createDebug.namespaces; + enabledCache = createDebug.enabled(namespace); + } + + return enabledCache; + }, set: v => { enableOverride = v; } @@ -149,6 +161,7 @@ function setup(env) { */ function enable(namespaces) { createDebug.save(namespaces); + createDebug.namespaces = namespaces; createDebug.names = []; createDebug.skips = [];