Refactor timer privates (#639)

* Make Timer.current public

* Make _indentString non-static
This commit is contained in:
toasted-nutbread 2020-07-03 11:58:12 -04:00 committed by GitHub
parent 897d85d1ac
commit d776125ead
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -22,12 +22,12 @@ class Timer {
this.parent = null; this.parent = null;
this.sample(name); this.sample(name);
const current = Timer._current; const current = Timer.current;
if (current !== null) { if (current !== null) {
current.samples[current.samples.length - 1].children.push(this); current.samples[current.samples.length - 1].children.push(this);
this.parent = current; this.parent = current;
} }
Timer._current = this; Timer.current = this;
} }
sample(name) { sample(name) {
@ -42,7 +42,7 @@ class Timer {
complete(skip) { complete(skip) {
this.sample('complete'); this.sample('complete');
Timer._current = this.parent; Timer.current = this.parent;
if (this.parent === null) { if (this.parent === null) {
if (!skip) { if (!skip) {
console.log(this.toString()); console.log(this.toString());
@ -67,7 +67,7 @@ class Timer {
const name = this.samples[0].name; const name = this.samples[0].name;
const duration = this.samples[this.samples.length - 1].time - this.samples[0].time; const duration = this.samples[this.samples.length - 1].time - this.samples[0].time;
const extensionName = chrome.runtime.getManifest().name; const extensionName = chrome.runtime.getManifest().name;
return `${name} took ${duration.toFixed(8)}ms [${extensionName}]` + Timer._indentString(this.getSampleString(), indent); return `${name} took ${duration.toFixed(8)}ms [${extensionName}]` + this._indentString(this.getSampleString(), indent);
} }
getSampleString() { getSampleString() {
@ -80,16 +80,16 @@ class Timer {
const sampleDuration = this.samples[i + 1].time - sample.time; const sampleDuration = this.samples[i + 1].time - sample.time;
message += `\nSample[${i}] took ${sampleDuration.toFixed(8)}ms (${((sampleDuration / duration) * 100.0).toFixed(1)}%) [${sample.name}]`; message += `\nSample[${i}] took ${sampleDuration.toFixed(8)}ms (${((sampleDuration / duration) * 100.0).toFixed(1)}%) [${sample.name}]`;
for (const child of sample.children) { for (const child of sample.children) {
message += Timer._indentString(child.getSampleString(), indent); message += this._indentString(child.getSampleString(), indent);
} }
} }
return message; return message;
} }
static _indentString(message, indent) { _indentString(message, indent) {
return message.replace(/\n/g, `\n${indent}`); return message.replace(/\n/g, `\n${indent}`);
} }
} }
Timer._current = null; Timer.current = null;