Skip to content

Commit

Permalink
修复 clearQueue Bug
Browse files Browse the repository at this point in the history
  • Loading branch information
carsonxu committed Apr 11, 2019
1 parent 1e00556 commit a8e6f08
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 37 deletions.
3 changes: 2 additions & 1 deletion demo/queue/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<h1>COS 上传队列</h1>
<div id="app">
<form id="form">
<div class="float-right">共{{total}}个文件</div>
<input type="file" value="选择上传文件" multiple @change="selectedFile">
</form>
<table class="file-list">
Expand Down Expand Up @@ -62,4 +63,4 @@ <h1>COS 上传队列</h1>
<script src="./index.js"></script>

</body>
</html>
</html>
6 changes: 4 additions & 2 deletions demo/queue/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ new Vue({
FileParallelLimit: 5,
ChunkParallelLimit: 16,
ChunkMbSize: 2,
list: []
list: [],
total: 0,
};
},
created: function () {
var self = this;
cos.on('list-update', function (data) {
self.list = data.list;
self.total = data.list.length;
});
},
methods: {
Expand Down Expand Up @@ -72,4 +74,4 @@ new Vue({
cos.cancelTask(task.id);
},
},
});
});
4 changes: 4 additions & 0 deletions demo/queue/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ a {
color: #0071ff;
}

.float-right {
float: right;
}

.page {
width: 960px;
margin: 0 auto;
Expand Down
30 changes: 16 additions & 14 deletions dist/cos-js-sdk-v5.js
Original file line number Diff line number Diff line change
Expand Up @@ -1983,7 +1983,7 @@ base.init(COS, task);
advance.init(COS, task);

COS.getAuthorization = util.getAuth;
COS.version = '0.5.13';
COS.version = '0.5.14';

module.exports = COS;

Expand Down Expand Up @@ -3716,19 +3716,21 @@ var initTask = function (cos) {
};

var clearQueue = function () {
if (queue.length > cos.options.UploadQueueSize) {
var i;
for (i = 0; i < queue.length && queue.length > cos.options.UploadQueueSize && // 大于队列才处理
i < nextUploadIndex; // 小于当前操作的 index 才处理
i++) {
var isActive = queue[i].state === 'waiting' || queue[i].state === 'checking' || queue[i].state === 'uploading';
if (!queue[i] || !isActive) {
tasks[queue[i].id] && delete tasks[queue[i].id];
queue.splice(i, 1);
nextUploadIndex--;
}
if (queue.length <= cos.options.UploadQueueSize) return;
for (var i = 0; i < nextUploadIndex && // 小于当前操作的 index 才清理
i < queue.length && // 大于队列才清理
queue.length > cos.options.UploadQueueSize // 如果还太多,才继续清理
;) {
var isActive = queue[i].state === 'waiting' || queue[i].state === 'checking' || queue[i].state === 'uploading';
if (!queue[i] || !isActive) {
tasks[queue[i].id] && delete tasks[queue[i].id];
queue.splice(i, 1);
nextUploadIndex--;
} else {
i++;
}
}
emitListUpdate();
};

var startNextTask = function () {
Expand Down Expand Up @@ -3779,7 +3781,7 @@ var initTask = function (cos) {
}
task.state = switchToState;
cos.emit('inner-kill-task', { TaskId: id, toState: switchToState });
emitListUpdate();
emitListUpdate(true);
if (running) {
uploadingFileCount--;
startNextTask(cos);
Expand All @@ -3800,7 +3802,7 @@ var initTask = function (cos) {
util.each(taskList, function (task) {
cos._addTask(task.api, task.params, task.callback, true);
});
emitListUpdate();
emitListUpdate(true);
};

var isTaskReadyWarning = true;
Expand Down
2 changes: 1 addition & 1 deletion dist/cos-js-sdk-v5.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cos-js-sdk-v5",
"version": "0.5.13",
"version": "0.5.14",
"description": "JavaScript SDK for [腾讯云对象存储](https://cloud.tencent.com/product/cos)",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/cos.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ base.init(COS, task);
advance.init(COS, task);

COS.getAuthorization = util.getAuth;
COS.version = '0.5.13';
COS.version = '0.5.14';

module.exports = COS;
35 changes: 18 additions & 17 deletions src/task.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,22 @@ var initTask = function (cos) {
};

var clearQueue = function () {
if (queue.length > cos.options.UploadQueueSize) {
var i;
for (i = 0;
i < queue.length &&
queue.length > cos.options.UploadQueueSize && // 大于队列才处理
i < nextUploadIndex; // 小于当前操作的 index 才处理
i++) {
var isActive = queue[i].state === 'waiting' || queue[i].state === 'checking' || queue[i].state === 'uploading';
if (!queue[i] || !isActive) {
tasks[queue[i].id] && (delete tasks[queue[i].id]);
queue.splice(i, 1);
nextUploadIndex--;
}
if (queue.length <= cos.options.UploadQueueSize) return;
for (var i = 0;
i < nextUploadIndex && // 小于当前操作的 index 才清理
i < queue.length && // 大于队列才清理
queue.length > cos.options.UploadQueueSize // 如果还太多,才继续清理
;) {
var isActive = queue[i].state === 'waiting' || queue[i].state === 'checking' || queue[i].state === 'uploading';
if (!queue[i] || !isActive) {
tasks[queue[i].id] && (delete tasks[queue[i].id]);
queue.splice(i, 1);
nextUploadIndex--;
} else {
i++;
}
}
emitListUpdate();
};

var startNextTask = function () {
Expand Down Expand Up @@ -113,7 +114,7 @@ var initTask = function (cos) {
}
task.state = switchToState;
cos.emit('inner-kill-task', {TaskId: id, toState: switchToState});
emitListUpdate();
emitListUpdate(true);
if (running) {
uploadingFileCount--;
startNextTask(cos);
Expand All @@ -134,7 +135,7 @@ var initTask = function (cos) {
util.each(taskList, function (task) {
cos._addTask(task.api, task.params, task.callback, true);
});
emitListUpdate();
emitListUpdate(true);
};

var isTaskReadyWarning = true;
Expand Down Expand Up @@ -216,10 +217,10 @@ var initTask = function (cos) {
return util.map(queue, formatTask);
};
cos.cancelTask = function (id) {
killTask(id, 'canceled')
killTask(id, 'canceled');
};
cos.pauseTask = function (id) {
killTask(id, 'paused')
killTask(id, 'paused');
};
cos.restartTask = function (id) {
var task = tasks[id];
Expand Down

0 comments on commit a8e6f08

Please sign in to comment.