Skip to content

Commit

Permalink
enabled() updates existing debug instances, add destroy() function (
Browse files Browse the repository at this point in the history
#440)

* dynamically updatable instances

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

So that "dynamically created instances" can clean up after themselves
  • Loading branch information
TooTallNate authored and thebigredgeek committed Apr 12, 2017
1 parent 9e227d3 commit ae88bce
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/debug.js
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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.
Expand All @@ -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);
}
}

/**
Expand Down

0 comments on commit ae88bce

Please sign in to comment.