From 72e7f864bd75fc8353e4dd450de96d9104ba9f35 Mon Sep 17 00:00:00 2001 From: Josh Junon Date: Sun, 12 Jan 2020 20:14:48 +0100 Subject: [PATCH] fix memory leak within debug instance --- src/common.js | 33 ++++++++++----------------------- 1 file changed, 10 insertions(+), 23 deletions(-) diff --git a/src/common.js b/src/common.js index a7eeb0b1..9d7e4dce 100644 --- a/src/common.js +++ b/src/common.js @@ -17,11 +17,6 @@ function setup(env) { createDebug[key] = env[key]; }); - /** - * Active `debug` instances. - */ - createDebug.instances = []; - /** * The currently active debug mode names, and names to skip. */ @@ -63,6 +58,7 @@ function setup(env) { */ function createDebug(namespace) { let prevTime; + let enableOverride = null; function debug(...args) { // Disabled? @@ -115,31 +111,27 @@ function setup(env) { } debug.namespace = namespace; - debug.enabled = createDebug.enabled(namespace); debug.useColors = createDebug.useColors(); debug.color = createDebug.selectColor(namespace); - debug.destroy = destroy; debug.extend = extend; + Object.defineProperty(debug, 'enabled', { + enumerable: true, + configurable: false, + get: () => enableOverride === null ? createDebug.enabled(namespace) : enableOverride, + set: v => { + enableOverride = v; + } + }); + // Env-specific initialization logic for debug instances if (typeof createDebug.init === 'function') { createDebug.init(debug); } - createDebug.instances.push(debug); - return debug; } - function destroy() { - const index = createDebug.instances.indexOf(this); - if (index !== -1) { - createDebug.instances.splice(index, 1); - return true; - } - return false; - } - function extend(namespace, delimiter) { const newDebug = createDebug(this.namespace + (typeof delimiter === 'undefined' ? ':' : delimiter) + namespace); newDebug.log = this.log; @@ -177,11 +169,6 @@ function setup(env) { createDebug.names.push(new RegExp('^' + namespaces + '$')); } } - - for (i = 0; i < createDebug.instances.length; i++) { - const instance = createDebug.instances[i]; - instance.enabled = createDebug.enabled(instance.namespace); - } } /**