Skip to content

Commit

Permalink
优化 list-update 事件,加 setTimeout 截流
Browse files Browse the repository at this point in the history
  • Loading branch information
carsonxu committed Jun 12, 2019
1 parent 00d04fc commit 0f010d0
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 21 deletions.
28 changes: 19 additions & 9 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.17';
COS.version = '0.5.18';

module.exports = COS;

Expand Down Expand Up @@ -3712,9 +3712,16 @@ var initTask = function (cos) {
};

var emitListUpdate = function () {
cos.emit('task-list-update', { list: util.map(queue, formatTask) });
cos.emit('list-update', { list: util.map(queue, formatTask) });
};
var timer;
var emit = function () {
timer = 0;
cos.emit('task-list-update', { list: util.map(queue, formatTask) });
cos.emit('list-update', { list: util.map(queue, formatTask) });
};
return function () {
if (!timer) timer = setTimeout(emit);
};
}();

var clearQueue = function () {
if (queue.length <= cos.options.UploadQueueSize) return;
Expand Down Expand Up @@ -3782,7 +3789,7 @@ var initTask = function (cos) {
}
task.state = switchToState;
cos.emit('inner-kill-task', { TaskId: id, toState: switchToState });
emitListUpdate(true);
emitListUpdate();
if (running) {
uploadingFileCount--;
startNextTask(cos);
Expand All @@ -3803,7 +3810,7 @@ var initTask = function (cos) {
util.each(taskList, function (task) {
cos._addTask(task.api, task.params, task.callback, true);
});
emitListUpdate(true);
emitListUpdate();
};

var isTaskReadyWarning = true;
Expand Down Expand Up @@ -3887,16 +3894,19 @@ var initTask = function (cos) {
return util.map(queue, formatTask);
};
cos.cancelTask = function (id) {
killTask(id, 'canceled');
var options = (typeof id === 'string' ? { id: id } : id) || {};
killTask(id, 'canceled', options.IgnoreListUpdate);
};
cos.pauseTask = function (id) {
killTask(id, 'paused');
var options = (typeof id === 'string' ? { id: id } : id) || {};
killTask(id, 'paused', options.IgnoreListUpdate);
};
cos.restartTask = function (id) {
var options = (typeof id === 'string' ? { id: id } : id) || {};
var task = tasks[id];
if (task && (task.state === 'paused' || task.state === 'error')) {
task.state = 'waiting';
emitListUpdate();
options && emitListUpdate();
nextUploadIndex = Math.min(nextUploadIndex, task.index);
startNextTask();
}
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.17",
"version": "0.5.18",
"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.17';
COS.version = '0.5.18';

module.exports = COS;
28 changes: 19 additions & 9 deletions src/task.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,17 @@ var initTask = function (cos) {
return t;
};

var emitListUpdate = function () {
cos.emit('task-list-update', {list: util.map(queue, formatTask)});
cos.emit('list-update', {list: util.map(queue, formatTask)});
};
var emitListUpdate = (function () {
var timer;
var emit = function () {
timer = 0;
cos.emit('task-list-update', {list: util.map(queue, formatTask)});
cos.emit('list-update', {list: util.map(queue, formatTask)});
};
return function () {
if (!timer) timer = setTimeout(emit);
}
})();

var clearQueue = function () {
if (queue.length <= cos.options.UploadQueueSize) return;
Expand Down Expand Up @@ -115,7 +122,7 @@ var initTask = function (cos) {
}
task.state = switchToState;
cos.emit('inner-kill-task', {TaskId: id, toState: switchToState});
emitListUpdate(true);
emitListUpdate();
if (running) {
uploadingFileCount--;
startNextTask(cos);
Expand All @@ -136,7 +143,7 @@ var initTask = function (cos) {
util.each(taskList, function (task) {
cos._addTask(task.api, task.params, task.callback, true);
});
emitListUpdate(true);
emitListUpdate();
};

var isTaskReadyWarning = true;
Expand Down Expand Up @@ -219,16 +226,19 @@ var initTask = function (cos) {
return util.map(queue, formatTask);
};
cos.cancelTask = function (id) {
killTask(id, 'canceled');
var options = (typeof id === 'string' ? {id: id}: id) || {};
killTask(id, 'canceled', options.IgnoreListUpdate);
};
cos.pauseTask = function (id) {
killTask(id, 'paused');
var options = (typeof id === 'string' ? {id: id}: id) || {};
killTask(id, 'paused', options.IgnoreListUpdate);
};
cos.restartTask = function (id) {
var options = (typeof id === 'string' ? {id: id}: id) || {};
var task = tasks[id];
if (task && (task.state === 'paused' || task.state === 'error')) {
task.state = 'waiting';
emitListUpdate();
options && emitListUpdate();
nextUploadIndex = Math.min(nextUploadIndex, task.index);
startNextTask();
}
Expand Down

0 comments on commit 0f010d0

Please sign in to comment.