Skip to content

Commit

Permalink
Chore: pass file id to RepStatus for easier timers (#660)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinHoldstock authored Feb 14, 2018
1 parent efe8b04 commit 6bfb193
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 29 deletions.
23 changes: 6 additions & 17 deletions src/lib/RepStatus.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const STATUS_UPDATE_INTERVAL_MS = 2000;
const ERROR_PASSWORD_PROTECTED = 'error_password_protected';
const ERROR_TRY_AGAIN_LATER = 'error_try_again_later';
const ERROR_UNSUPPORTED_FORMAT = 'error_unsupported_format';
const FILE_ID_CAPTURE = /(?:internal_files\/)(.+)(?:\/versions)/;

class RepStatus extends EventEmitter {
/**
Expand Down Expand Up @@ -49,10 +48,11 @@ class RepStatus extends EventEmitter {
* @param {Object} [options.logger] - Optional logger instance
* @return {RepStatus} RepStatus instance
*/
constructor({ representation, token, sharedLink, sharedLinkPassword, logger }) {
constructor({ representation, token, sharedLink, sharedLinkPassword, logger, fileId }) {
super();
this.representation = representation;
this.logger = logger;
this.fileId = fileId;

// Some representations (e.g. ORIGINAL) may not have an info url
const repInfo = this.representation.info;
Expand Down Expand Up @@ -84,12 +84,8 @@ class RepStatus extends EventEmitter {
return Promise.resolve();
}

// Getting file id to track conversion time, if there's a url tempate
if (this.representation.content && this.representation.content.url_template) {
const fileId = this.representation.content.url_template.match(FILE_ID_CAPTURE)[1];
const tag = Timer.createTag(fileId, LOAD_METRIC.convertTime);
Timer.start(tag);
}
const tag = Timer.createTag(this.fileId, LOAD_METRIC.convertTime);
Timer.start(tag);

return get(this.infoUrl).then((info) => {
clearTimeout(this.statusTimeout);
Expand Down Expand Up @@ -120,12 +116,7 @@ class RepStatus extends EventEmitter {
const errCode = RepStatus.getErrorCode(this.representation);
let errorMessage;
let error;
let convertTag;

if (this.representation.content && this.representation.content.url_template) {
const fileId = this.representation.content.url_template.match(FILE_ID_CAPTURE)[1];
convertTag = Timer.createTag(fileId, LOAD_METRIC.convertTime);
}
const convertTag = Timer.createTag(this.fileId, LOAD_METRIC.convertTime);

switch (status) {
case 'error':
Expand All @@ -150,9 +141,7 @@ class RepStatus extends EventEmitter {

case STATUS_SUCCESS:
case STATUS_VIEWABLE:
if (convertTag) {
Timer.stop(convertTag);
}
Timer.stop(convertTag);
this.resolve();
break;

Expand Down
16 changes: 5 additions & 11 deletions src/lib/__tests__/RepStatus-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const STATUS_UPDATE_INTERVAL_MS = 2000;

describe('lib/RepStatus', () => {
let rep;
const fileId = '12345';

beforeEach(() => {
rep = {
Expand All @@ -24,7 +25,8 @@ describe('lib/RepStatus', () => {
const logger = () => {};
repStatus = new RepStatus({
representation: rep,
logger
logger,
fileId
});
});

Expand Down Expand Up @@ -140,11 +142,7 @@ describe('lib/RepStatus', () => {
expect(repStatus.updateStatus()).to.be.instanceof(Promise);
});

it('should start a convert time Timer if a content url_template exists', () => {
const fileId = 123456;
rep.content = {
url_template: `https://box.com/files/internal_files/${fileId}/versions`
};
it('should start a convert time Timer', () => {
const tag = Timer.createTag(fileId, LOAD_METRIC.convertTime);
repStatus.updateStatus();

Expand Down Expand Up @@ -240,11 +238,7 @@ describe('lib/RepStatus', () => {
clock.restore();
});

it('should stop a convert time Timer if a content url_template exists, and success converting', () => {
const fileId = 123456;
rep.content = {
url_template: `https://box.com/files/internal_files/${fileId}/versions`
};
it('should stop a convert time Timer on success converting', () => {
repStatus.representation.status.state = STATUS_SUCCESS;
const tag = Timer.createTag(fileId, LOAD_METRIC.convertTime);
Timer.start(tag);
Expand Down
3 changes: 2 additions & 1 deletion src/lib/viewers/BaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -648,12 +648,13 @@ class BaseViewer extends EventEmitter {
* @return {RepStatus} Instance of RepStatus
*/
getRepStatus(representation) {
const { token, sharedLink, sharedLinkPassword, logger } = this.options;
const { token, sharedLink, sharedLinkPassword, logger, file } = this.options;
const repStatus = new RepStatus({
representation: representation || this.options.representation,
token,
sharedLink,
sharedLinkPassword,
fileId: file.id,
logger: representation ? null : logger // Do not log to main preview status if rep is passed in
});

Expand Down

0 comments on commit 6bfb193

Please sign in to comment.