enabled() updates existing debug instances, add destroy() function (#440)

* dynamically updatable instances

* add a `destroy()` function to debug instances

So that "dynamically created instances" can clean up after themselves
This commit is contained in:
Nathan Rajlich 2017-04-12 13:25:31 -07:00
parent 9d7c997992
commit bf88540737

View File

@ -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.
*/
@ -114,15 +119,23 @@ 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) {
exports.init(debug);
}
exports.instances.push(debug);
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.
@ -149,6 +162,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);
}
}
/**