From eb8d4515ad5b0c0b551aa69dbe669c64200172f8 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Wed, 12 Apr 2017 12:28:10 -0700 Subject: [PATCH 1/2] dynamically updatable instances --- src/debug.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/debug.js b/src/debug.js index 3dcad348..edbd22f7 100644 --- a/src/debug.js +++ b/src/debug.js @@ -13,6 +13,11 @@ exports.enable = enable; exports.enabled = enabled; exports.humanize = require('ms'); +/** + * Active `debug` instances. + */ +exports.instances = []; + /** * The currently active debug mode names, and names to skip. */ @@ -120,6 +125,8 @@ function createDebug(namespace) { exports.init(debug); } + exports.instances.push(debug); + return debug; } @@ -149,6 +156,11 @@ function enable(namespaces) { exports.names.push(new RegExp('^' + namespaces + '$')); } } + + for (var i = 0; i < exports.instances.length; i++) { + var instance = exports.instances[i]; + instance.enabled = exports.enabled(instance.namespace); + } } /** From b5f559783131be127fa668417b36e6517ea5af49 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Wed, 12 Apr 2017 12:28:28 -0700 Subject: [PATCH 2/2] add a `destroy()` function to debug instances So that "dynamically created instances" can clean up after themselves --- src/debug.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/debug.js b/src/debug.js index edbd22f7..6edb3d52 100644 --- a/src/debug.js +++ b/src/debug.js @@ -119,6 +119,7 @@ function createDebug(namespace) { debug.enabled = exports.enabled(namespace); debug.useColors = exports.useColors(); debug.color = selectColor(namespace); + debug.destroy = destroy; // env-specific initialization logic for debug instances if ('function' === typeof exports.init) { @@ -130,6 +131,11 @@ function createDebug(namespace) { return debug; } +function destroy () { + const index = exports.instances.indexOf(this) + exports.instances.splice(index, 1) +} + /** * Enables a debug mode by namespaces. This can include modes * separated by a colon and wildcards.