Skip to content
66 changes: 58 additions & 8 deletions src/lib/RepStatus.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,25 @@ class RepStatus extends EventEmitter {
return code;
}

/**
* Reads the status state out of a representation info response, tolerating a malformed payload
*
* @private
* @param {Object} info - representation info response
* @return {string|undefined} status state, or undefined when the payload has none
*/
static getStateFromInfo(info) {
if (!info) {
return undefined;
}

if (typeof info.status === 'object') {
return info.status ? info.status.state : undefined;
}

return info.temp_status ? info.temp_status.state : undefined;
}

/**
* [constructor]
*
Expand All @@ -50,13 +69,23 @@ class RepStatus extends EventEmitter {
* @param {Object} [options.logger] - Optional logger instance
* @return {RepStatus} RepStatus instance
*/
constructor({ api, representation, token, sharedLink, sharedLinkPassword, logger, fileId }) {
constructor({
api,
representation,
token,
sharedLink,
sharedLinkPassword,
logger,
fileId,
enableConvertTimer = true,
}) {
super();

this.api = api;
this.representation = representation;
this.logger = logger;
this.fileId = fileId;
this.enableConvertTimer = enableConvertTimer;

// Some representations (e.g. ORIGINAL) may not have an info url
const repInfo = this.representation.info;
Expand All @@ -75,6 +104,7 @@ class RepStatus extends EventEmitter {
* @return {void}
*/
destroy() {
this.destroyed = true;
clearTimeout(this.statusTimeout);
}

Expand All @@ -85,34 +115,52 @@ class RepStatus extends EventEmitter {
* @return {Promise} Promise to update status
*/
updateStatus() {
if (!this.infoUrl) {
if (!this.infoUrl || this.destroyed) {
return Promise.resolve();
}

const tag = Timer.createTag(this.fileId, LOAD_METRIC.convertTime);
Timer.start(tag);
if (this.enableConvertTimer) {
Timer.start(Timer.createTag(this.fileId, LOAD_METRIC.convertTime));
}

const options = this.headers ? { headers: this.headers } : {};
return this.api.get(this.infoUrl, options).then(
info => {
if (this.destroyed) {
return;
}

clearTimeout(this.statusTimeout);

const state = RepStatus.getStateFromInfo(info);
if (!state) {
this.reject(
new PreviewError(ERROR_CODE.CONVERSION_GENERIC, __('error_refresh'), {
representation: this.representation,
}),
);
return;
}

if (info.metadata) {
this.representation.metadata = info.metadata;
}

const status = typeof info.status === 'object' ? info.status.state : info.temp_status.state;
if (typeof this.representation.status === 'object') {
this.representation.status.state = status;
this.representation.status.state = state;
} else {
this.representation.temp_status.state = status;
this.representation.temp_status.state = state;
}

this.handleResponse();
},
// Reject instead of hanging if the info request itself fails (e.g. the representation
// endpoint returns a 4xx/5xx), so the viewer can surface an error rather than spin forever.
() => {
if (this.destroyed) {
return;
}

clearTimeout(this.statusTimeout);
const error = new PreviewError(ERROR_CODE.CONVERSION_GENERIC, __('error_refresh'), {
representation: this.representation,
Expand Down Expand Up @@ -167,7 +215,9 @@ class RepStatus extends EventEmitter {

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

Expand Down
110 changes: 110 additions & 0 deletions src/lib/__tests__/RepStatus-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,32 @@ describe('lib/RepStatus', () => {
});
});

describe('getStateFromInfo()', () => {
test('should return undefined when there is no info', () => {
expect(RepStatus.getStateFromInfo(undefined)).toBeUndefined();
});

test('should return the state from the status object', () => {
expect(RepStatus.getStateFromInfo({ status: { state: 'success' } })).toBe('success');
});

test('should return undefined when the status object has no state', () => {
expect(RepStatus.getStateFromInfo({ status: {} })).toBeUndefined();
});

test('should return undefined when the status object is null', () => {
expect(RepStatus.getStateFromInfo({ status: null })).toBeUndefined();
});

test('should fall back to temp_status when status is not an object', () => {
expect(RepStatus.getStateFromInfo({ temp_status: { state: 'pending' } })).toBe('pending');
});

test('should return undefined when neither status nor temp_status carry a state', () => {
expect(RepStatus.getStateFromInfo({})).toBeUndefined();
});
});

describe('constructor()', () => {
const infoUrl = 'someUrl';

Expand All @@ -92,6 +118,18 @@ describe('lib/RepStatus', () => {
expect(typeof repStatus.logger).toBe('object');
expect(repStatus.infoUrl).toBe(infoUrl);
expect(repStatus.promise).toBeInstanceOf(Promise);
expect(repStatus.enableConvertTimer).toBe(true);
});

test('should disable the convert timer when told to', () => {
repStatus = new RepStatus({
api: {},
representation: rep,
logger: {},
enableConvertTimer: false,
});

expect(repStatus.enableConvertTimer).toBe(false);
});

test('should use appendAuthParamsV2 and set Authorization headers', () => {
Expand Down Expand Up @@ -120,6 +158,11 @@ describe('lib/RepStatus', () => {
sandbox.mock(window).expects('clearTimeout');
repStatus.destroy();
});

test('should mark the instance destroyed', () => {
repStatus.destroy();
expect(repStatus.destroyed).toBe(true);
});
});

describe('updateStatus()', () => {
Expand Down Expand Up @@ -171,6 +214,14 @@ describe('lib/RepStatus', () => {
expect(repStatus.api.get).not.toBeCalled();
});

test('should return a resolved promise without fetching if destroyed', () => {
jest.spyOn(repStatus.api, 'get');

repStatus.destroyed = true;
expect(repStatus.updateStatus()).toBeInstanceOf(Promise);
expect(repStatus.api.get).not.toBeCalled();
});

test('should start a convert time Timer', () => {
jest.spyOn(repStatus.api, 'get').mockResolvedValue({
info: {},
Expand All @@ -186,6 +237,42 @@ describe('lib/RepStatus', () => {
});
});

test('should not start a convert time Timer when the timer is disabled', () => {
repStatus.enableConvertTimer = false;
jest.spyOn(repStatus.api, 'get').mockResolvedValue({
info: {},
status: {
state,
},
});
jest.spyOn(Timer, 'start');

return repStatus.updateStatus().then(() => {
expect(Timer.start).not.toBeCalled();
});
});

test('should not process a response that arrives after it is destroyed', () => {
jest.spyOn(repStatus.api, 'get').mockImplementation(() => {
repStatus.destroyed = true;
return Promise.resolve({ info: {}, status: { state } });
});

return repStatus.updateStatus().then(() => {
expect(repStatus.handleResponse).not.toBeCalled();
});
});

test('should reject when the info response has no status state', () => {
jest.spyOn(repStatus.api, 'get').mockResolvedValue({ info: {} });
jest.spyOn(repStatus, 'reject').mockImplementation();

return repStatus.updateStatus().then(() => {
expect(repStatus.reject).toBeCalled();
expect(repStatus.handleResponse).not.toBeCalled();
});
});

test('should reject the status promise when the info request fails instead of hanging', () => {
jest.spyOn(repStatus.api, 'get').mockRejectedValue(new Error('Not Found'));
jest.spyOn(repStatus, 'reject').mockImplementation();
Expand All @@ -195,6 +282,18 @@ describe('lib/RepStatus', () => {
expect(repStatus.handleResponse).not.toBeCalled();
});
});

test('should not reject a failed info request after it is destroyed', () => {
jest.spyOn(repStatus.api, 'get').mockImplementation(() => {
repStatus.destroyed = true;
return Promise.reject(new Error('Not Found'));
});
jest.spyOn(repStatus, 'reject').mockImplementation();

return repStatus.updateStatus().then(() => {
expect(repStatus.reject).not.toBeCalled();
});
});
});

describe('handleResponse()', () => {
Expand Down Expand Up @@ -359,6 +458,17 @@ describe('lib/RepStatus', () => {
// Elapsed will not exist if stop isn't called
expect(Timer.get(tag).elapsed).toBeDefined();
});

test('should not stop a convert time Timer on success when the timer is disabled', () => {
repStatus.enableConvertTimer = false;
repStatus.representation.status.state = STATUS_SUCCESS;
const tag = Timer.createTag(fileId, LOAD_METRIC.convertTime);
Timer.start(tag);
repStatus.handleResponse();

// Elapsed will exist only if stop is called
expect(Timer.get(tag).elapsed).toBeUndefined();
});
});

describe('getPromise()', () => {
Expand Down
1 change: 1 addition & 0 deletions src/lib/viewers/BaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,7 @@ class BaseViewer extends EventEmitter {
sharedLinkPassword,
fileId: file.id,
logger: representation ? null : logger, // Do not log to main preview status if rep is passed in
enableConvertTimer: !representation || representation === this.options.representation,
});

// Don't time out while conversion is pending
Expand Down
15 changes: 15 additions & 0 deletions src/lib/viewers/__tests__/BaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,21 @@ describe('lib/viewers/BaseViewer', () => {
expect(repStatus.representation).toBe(representation);
});

test('should enable the convert timer for the main representation', () => {
const repStatus = base.getRepStatus();
expect(repStatus.enableConvertTimer).toBe(true);
});

test('should disable the convert timer for a passed in secondary representation', () => {
const repStatus = base.getRepStatus({ info: { url: 'someOtherUrl' } });
expect(repStatus.enableConvertTimer).toBe(false);
});

test('should enable the convert timer when the main representation is passed in', () => {
const repStatus = base.getRepStatus(base.options.representation);
expect(repStatus.enableConvertTimer).toBe(true);
});

test('should emit conversionpending once when the main representation is pending', () => {
jest.spyOn(base, 'emit');
const repStatus = base.getRepStatus();
Expand Down
Loading
Loading