Skip to content

Commit

Permalink
cache enabled status per-logger (#799)
Browse files Browse the repository at this point in the history
Co-authored-by: Qix <Qix-@users.noreply.github.com>
  • Loading branch information
containmentbreach and Qix- authored Dec 9, 2020
1 parent 0d3d66b commit 1e9d38c
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ function setup(env) {
function createDebug(namespace) {
let prevTime;
let enableOverride = null;
let namespacesCache;
let enabledCache;

function debug(...args) {
// Disabled?
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -149,6 +161,7 @@ function setup(env) {
*/
function enable(namespaces) {
createDebug.save(namespaces);
createDebug.namespaces = namespaces;

createDebug.names = [];
createDebug.skips = [];
Expand Down

0 comments on commit 1e9d38c

Please sign in to comment.