Skip to content

Commit 557e0ba

Browse files
committed
Release v2.6.0
1 parent e6dff61 commit 557e0ba

4 files changed

Lines changed: 59 additions & 22 deletions

File tree

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "flow.js",
3-
"version": "2.5.0",
3+
"version": "2.6.0",
44
"main": "./dist/flow.js",
55
"ignore": [
66
"**/.*",

dist/flow.js

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
* @param {number} [opts.progressCallbacksInterval]
1616
* @param {number} [opts.speedSmoothingFactor]
1717
* @param {Object|Function} [opts.query]
18-
* @param {Object} [opts.headers]
18+
* @param {Object|Function} [opts.headers]
1919
* @param {bool} [opts.withCredentials]
2020
* @param {Function} [opts.preprocess]
2121
* @param {string} [opts.method]
2222
* @param {bool} [opts.prioritizeFirstAndLastChunk]
23-
* @param {string} [opts.target]
23+
* @param {string|Function} [opts.target]
2424
* @param {number} [opts.maxChunkRetries]
2525
* @param {number} [opts.chunkRetryInterval]
2626
* @param {Array.<number>} [opts.permanentErrors]
@@ -443,19 +443,44 @@
443443
return uploading;
444444
},
445445

446+
/**
447+
* should upload next chunk
448+
* @function
449+
* @returns {boolean|number}
450+
*/
451+
_shouldUploadNext: function () {
452+
var num = 0;
453+
var should = true;
454+
var simultaneousUploads = this.opts.simultaneousUploads;
455+
each(this.files, function (file) {
456+
each(file.chunks, function(chunk) {
457+
if (chunk.status() === 'uploading') {
458+
num++;
459+
if (num >= simultaneousUploads) {
460+
should = false;
461+
return false;
462+
}
463+
}
464+
});
465+
});
466+
// if should is true then return uploading chunks's length
467+
return should && num;
468+
},
469+
446470
/**
447471
* Start or resume uploading.
448472
* @function
449473
*/
450474
upload: function () {
451475
// Make sure we don't start too many uploads at once
452-
if (this.isUploading()) {
476+
var ret = this._shouldUploadNext();
477+
if (ret === false) {
453478
return;
454479
}
455480
// Kick off the queue
456481
this.fire('uploadStart');
457482
var started = false;
458-
for (var num = 1; num <= this.opts.simultaneousUploads; num++) {
483+
for (var num = 1; num <= this.opts.simultaneousUploads - ret; num++) {
459484
started = this.uploadNextChunk(true) || started;
460485
}
461486
if (!started) {
@@ -1174,8 +1199,7 @@
11741199
* @param params
11751200
* @returns {string}
11761201
*/
1177-
getTarget: function(params){
1178-
var target = this.flowObj.opts.target;
1202+
getTarget: function(target, params){
11791203
if(target.indexOf('?') < 0) {
11801204
target += '?';
11811205
} else {
@@ -1194,7 +1218,7 @@
11941218
this.xhr = new XMLHttpRequest();
11951219
this.xhr.addEventListener("load", this.testHandler, false);
11961220
this.xhr.addEventListener("error", this.testHandler, false);
1197-
var data = this.prepareXhrRequest('GET');
1221+
var data = this.prepareXhrRequest('GET', true);
11981222
this.xhr.send(data);
11991223
},
12001224

@@ -1246,8 +1270,7 @@
12461270
this.xhr.addEventListener("load", this.doneHandler, false);
12471271
this.xhr.addEventListener("error", this.doneHandler, false);
12481272

1249-
var data = this.prepareXhrRequest('POST', this.flowObj.opts.method, bytes);
1250-
1273+
var data = this.prepareXhrRequest('POST', false, this.flowObj.opts.method, bytes);
12511274
this.xhr.send(data);
12521275
},
12531276

@@ -1342,27 +1365,25 @@
13421365
/**
13431366
* Prepare Xhr request. Set query, headers and data
13441367
* @param {string} method GET or POST
1368+
* @param {bool} isTest is this a test request
13451369
* @param {string} [paramsMethod] octet or form
13461370
* @param {Blob} [blob] to send
13471371
* @returns {FormData|Blob|Null} data to send
13481372
*/
1349-
prepareXhrRequest: function(method, paramsMethod, blob) {
1373+
prepareXhrRequest: function(method, isTest, paramsMethod, blob) {
13501374
// Add data from the query options
1351-
var query = this.flowObj.opts.query;
1352-
if (typeof query === "function") {
1353-
query = query(this.fileObj, this);
1354-
}
1375+
var query = evalOpts(this.flowObj.opts.query, this.fileObj, this, isTest);
13551376
query = extend(this.getParams(), query);
13561377

1357-
var target = this.flowObj.opts.target;
1378+
var target = evalOpts(this.flowObj.opts.target, this.fileObj, this, isTest);
13581379
var data = null;
13591380
if (method === 'GET' || paramsMethod === 'octet') {
13601381
// Add data from the query options
13611382
var params = [];
13621383
each(query, function (v, k) {
13631384
params.push([encodeURIComponent(k), encodeURIComponent(v)].join('='));
13641385
});
1365-
target = this.getTarget(params);
1386+
target = this.getTarget(target, params);
13661387
data = blob || null;
13671388
} else {
13681389
// Add data from the query options
@@ -1377,7 +1398,7 @@
13771398
this.xhr.withCredentials = this.flowObj.opts.withCredentials;
13781399

13791400
// Add data from header options
1380-
each(this.flowObj.opts.headers, function (v, k) {
1401+
each(evalOpts(this.flowObj.opts.headers, this.fileObj, this, isTest), function (v, k) {
13811402
this.xhr.setRequestHeader(k, v);
13821403
}, this);
13831404

@@ -1397,6 +1418,22 @@
13971418
}
13981419
}
13991420

1421+
/**
1422+
* If option is a function, evaluate it with given params
1423+
* @param {*} data
1424+
* @param {...} args arguments of a callback
1425+
* @returns {*}
1426+
*/
1427+
function evalOpts(data, args) {
1428+
if (typeof data === "function") {
1429+
// `arguments` is an object, not array, in FF, so:
1430+
args = Array.prototype.slice.call(arguments);
1431+
data = data.apply(null, args.slice(1));
1432+
}
1433+
return data;
1434+
}
1435+
Flow.evalOpts = evalOpts;
1436+
14001437
/**
14011438
* Execute function asynchronously
14021439
* @param fn
@@ -1471,7 +1508,7 @@
14711508
* Library version
14721509
* @type {string}
14731510
*/
1474-
Flow.version = '2.5.0';
1511+
Flow.version = '2.6.0';
14751512

14761513
if ( typeof module === "object" && module && typeof module.exports === "object" ) {
14771514
// Expose Flow as module.exports in loaders that implement the Node

0 commit comments

Comments
 (0)