Skip to content

Commit

Permalink
Chore: Refactor Timer to allow a reset of multiple tags (#778)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Press authored Apr 23, 2018
1 parent 31265d8 commit 4f2f577
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 18 deletions.
15 changes: 7 additions & 8 deletions src/lib/Preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -1474,22 +1474,21 @@ class Preview extends EventEmitter {
*/
emitLoadMetrics() {
if (!this.file || !this.file.id) {
Timer.reset();
return;
}

// Do nothing if there is nothing worth logging.
const infoTag = Timer.createTag(this.file.id, LOAD_METRIC.fileInfoTime);
const convertTag = Timer.createTag(this.file.id, LOAD_METRIC.convertTime);
const downloadTag = Timer.createTag(this.file.id, LOAD_METRIC.downloadResponseTime);
const fullLoadTag = Timer.createTag(this.file.id, LOAD_METRIC.fullDocumentLoadTime);

// Do nothing if there is nothing worth logging.
const infoTime = Timer.get(infoTag) || {};
if (!infoTime.elapsed) {
Timer.reset();
Timer.reset([infoTag, convertTag, downloadTag, fullLoadTag]);
return;
}

const convertTag = Timer.createTag(this.file.id, LOAD_METRIC.convertTime);
const downloadTag = Timer.createTag(this.file.id, LOAD_METRIC.downloadResponseTime);
const fullLoadTag = Timer.createTag(this.file.id, LOAD_METRIC.fullDocumentLoadTime);

const timerList = [
infoTime,
Timer.get(convertTag) || {},
Expand All @@ -1511,7 +1510,7 @@ class Preview extends EventEmitter {

this.emit(PREVIEW_METRIC, event);

Timer.reset();
Timer.reset([infoTag, convertTag, downloadTag, fullLoadTag]);
}

/**
Expand Down
20 changes: 12 additions & 8 deletions src/lib/Timer.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,20 @@ class Timer {
* Resets the values in a certain time structure, if it exists.
*
* @public
* @param {string} [tag] - If provided, will reset a specific time structure associated with the tag.
* If empty, resets everything.
* @param {string|string[]} [tagOrTags] - If provided, will reset a specific time structure associated with
* the tag or array of tags. If empty, resets all tags.
*
* @return {void}
*/
reset(tag) {
if (tag) {
const time = this.get(tag);
reset(tagOrTags) {
if (!tagOrTags) {
Object.keys(this.times).forEach(this.reset.bind(this));
return;
}

const tagArray = typeof tagOrTags === 'string' ? [tagOrTags] : tagOrTags;
tagArray.forEach((tag) => {
const time = this.get(tag);
// If nothing exists, there's no reason to reset it
if (!time) {
return;
Expand All @@ -79,9 +85,7 @@ class Timer {
time.start = undefined;
time.end = undefined;
time.elapsed = undefined;
} else {
Object.keys(this.times).forEach(this.reset.bind(this));
}
});
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/lib/__tests__/Preview-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2222,12 +2222,12 @@ describe('lib/Preview', () => {
Timer.reset();
});

it('should reset the Timer and escape early if no file or file id', () => {
it('should do nothingescape early if no file or file id', () => {
sandbox.stub(Timer, 'reset');
sandbox.stub(preview, 'emit');
preview.file = undefined;
preview.emitLoadMetrics();
expect(Timer.reset).to.be.called;
expect(Timer.reset).to.not.be.called;
expect(preview.emit).to.not.be.called;
});

Expand Down
22 changes: 22 additions & 0 deletions src/lib/__tests__/Timer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,28 @@ describe('lib/Timer', () => {
expect(Timer.get(other).end).to.not.exist;
expect(Timer.get(other).elapsed).to.not.exist;
});

it('should reset multiple tags given an array of tags', () => {
Timer.start(tag);
Timer.stop(tag);
const other = 'other_tag';
Timer.start(other);
Timer.stop(other);
const another = 'another_tag';
Timer.start(another);
Timer.stop(another);
Timer.reset([tag, other]);

expect(Timer.get(tag).start).to.not.exist;
expect(Timer.get(tag).end).to.not.exist;
expect(Timer.get(tag).elapsed).to.not.exist;
expect(Timer.get(other).start).to.not.exist;
expect(Timer.get(other).end).to.not.exist;
expect(Timer.get(other).elapsed).to.not.exist;
expect(Timer.get(another).start).to.exist;
expect(Timer.get(another).end).to.exist;
expect(Timer.get(another).elapsed).to.exist;
});
});

describe('createTag()', () => {
Expand Down

0 comments on commit 4f2f577

Please sign in to comment.