Skip to content

Commit

Permalink
fix memory leak within debug instance
Browse files Browse the repository at this point in the history
  • Loading branch information
Qix- committed Sep 19, 2020
1 parent 27152ca commit 72e7f86
Showing 1 changed file with 10 additions and 23 deletions.
33 changes: 10 additions & 23 deletions src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -63,6 +58,7 @@ function setup(env) {
*/
function createDebug(namespace) {
let prevTime;
let enableOverride = null;

function debug(...args) {
// Disabled?
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

/**
Expand Down

0 comments on commit 72e7f86

Please sign in to comment.