Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ function setup(env) {

/**
* Active `debug` instances.
* @type {Object<String, Function>}
*/
createDebug.instances = [];
createDebug.instances = {};

/**
* The currently active debug mode names, and names to skip.
Expand Down Expand Up @@ -63,6 +64,11 @@ function setup(env) {
*/
function createDebug(namespace) {
let prevTime;
const value = createDebug.instances[namespace];

if (value !== undefined) {
return value;
}

function debug(...args) {
// Disabled?
Expand All @@ -74,8 +80,7 @@ function setup(env) {

// Set `diff` timestamp
const curr = Number(new Date());
const ms = curr - (prevTime || curr);
self.diff = ms;
self.diff = curr - (prevTime || curr);
self.prev = prevTime;
self.curr = curr;
prevTime = curr;
Expand Down Expand Up @@ -128,15 +133,14 @@ function setup(env) {
createDebug.init(debug);
}

createDebug.instances.push(debug);
createDebug.instances[namespace] = debug;

return debug;
}

function destroy() {
const index = createDebug.instances.indexOf(this);
if (index !== -1) {
createDebug.instances.splice(index, 1);
if (createDebug.instances[this.namespace] !== undefined) {
delete createDebug.instances[this.namespace];
return true;
}
return false;
Expand Down Expand Up @@ -180,8 +184,10 @@ function setup(env) {
}
}

for (i = 0; i < createDebug.instances.length; i++) {
const instance = createDebug.instances[i];
const keys = Object.keys(createDebug.instances);

for (i = 0; i < keys.length; i++) {
const instance = createDebug.instances[keys[i]];
instance.enabled = createDebug.enabled(instance.namespace);
}
}
Expand Down