Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New: MultiputUpload part2 #127

Merged
merged 9 commits into from
Dec 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
"flow-bin": "^0.50.0",
"husky": "^0.14.3",
"intl": "^1.2.5",
"jsuri": "^1.3.1",
"karma": "^1.7.0",
"karma-chai": "^0.1.0",
"karma-chai-sinon": "^0.1.5",
Expand Down Expand Up @@ -179,6 +180,7 @@
"webpack-dev-server": "^2.5.1"
},
"peerDependencies": {
"jsuri": "^1.3.1",
"lodash.clonedeep": "^4.5.0",
"lodash.debounce": "^4.0.8",
"lodash.noop": "^3.0.1",
Expand Down
13 changes: 13 additions & 0 deletions src/api/Base.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* @author Box
*/

import noop from 'lodash.noop';
import Xhr from '../util/Xhr';
import Cache from '../util/Cache';
import { DEFAULT_HOSTNAME_API, DEFAULT_HOSTNAME_UPLOAD } from '../constants';
Expand Down Expand Up @@ -40,6 +41,16 @@ class Base {
*/
options: Options;

/**
* @property {Function}
*/
consoleLog: Function;

/**
* @property {Function}
*/
consoleError: Function;

/**
* [constructor]
*
Expand All @@ -62,6 +73,8 @@ class Base {
});
this.xhr = new Xhr(this.options);
this.destroyed = false;
this.consoleLog = !!options.consoleLog && !!window.console ? window.console.log || noop : noop;
this.consoleError = !!options.consoleError && !!window.console ? window.console.error || noop : noop;
}

/**
Expand Down
80 changes: 80 additions & 0 deletions src/api/uploads/BaseMultiput.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* @flow
* @file Multiput upload base class
* @author Box
*/
import Base from '../Base';
import type { MultiputConfig, Options } from '../../flowTypes';

const DEFAULT_MULTIPUT_CONFIG: MultiputConfig = {
digestReadahead: 5, // How many parts past those currently uploading to precompute digest for
initialRetryDelayMs: 5000, // Base for exponential backoff on retries
maxRetryDelayMs: 60000, // Upper bound for time between retries
parallelism: 5, // Maximum number of parts to upload at a time
requestTimeoutMs: 120000, // Idle timeout on part upload, overall request timeout on other requests
// eslint-disable-next-line max-len
retries: 5 // How many times to retry requests such as upload part or commit. Note that total number of attempts will be retries + 1 in worst case where all attempts fail.
};

class BaseMultiput extends Base {
config: MultiputConfig;
sessionEndpoints: Object;
canConsoleLog: boolean;

/**
* [constructor]
*
* @param {Options} options
* @param {Object} sessionEndpoints
* @param {MultiputConfig} [config]
* @return {void}
*/
constructor(options: Options, sessionEndpoints: Object, config?: MultiputConfig): void {
super(options);

this.config = config || DEFAULT_MULTIPUT_CONFIG;
this.sessionEndpoints = sessionEndpoints;
this.canConsoleLog = !!options.consoleLog && !!window.console && !!window.console.log;
Copy link
Contributor

@priyajeet priyajeet Dec 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still needed? Since this.consoleLog will be a noop, you can just call it without check of existance.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, because if you can't console log, then we won't call the function to generate the console message which could potentially be costly. see consoleLogFunc

}

/**
* Console log a function returned message
*
* @param {Function} msgFunc
* @return {void}
*/
consoleLogFunc = (msgFunc: Function): void => {
if (!this.canConsoleLog) {
return;
}

this.consoleLog(msgFunc());
};

/**
* POST log event
*
* @param {string} eventType
* @param {string} [eventInfo]
* @return {Promise}
*/
logEvent = (eventType: string, eventInfo?: string) => {
const data: {
event_type: string,
event_info?: string
} = {
event_type: eventType
};

if (eventInfo) {
data.event_info = eventInfo;
}

return this.xhr.post({
url: this.sessionEndpoints.logEvent,
data
});
};
}

export default BaseMultiput;
Loading