Skip to content
This repository has been archived by the owner on Sep 26, 2024. It is now read-only.

Commit

Permalink
Add throttle option to #deleteFiles
Browse files Browse the repository at this point in the history
  • Loading branch information
osaton committed Mar 17, 2020
1 parent 68e2191 commit fd05d2a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
41 changes: 33 additions & 8 deletions StudioHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -995,16 +995,30 @@ class StudioHelper {
* Delete files
*
* @param {Array<string>} files - Array of file ids
* @param {Object} options
* @param {number} [options.throttle=1] - Number of concurrent delete file requests. Max 5
* @return {Promise<Object>}
**/
deleteFiles(files) {
let self = this;
deleteFiles(files, options = {}) {
const defaults = {
'throttle': 1
};

const settings = Object.assign(defaults, options);

// Max 5 concurrent delete operations
if (settings.throttle > 5) {
settings.throttle = 5;
}

return Promise.resolve(files).mapSeries(function(file) {
//return self._delete('file', file);
return self._delete('file', file);
const chunkThroat = throat(settings.throttle);

return Promise.resolve(files).map(file => {
return chunkThroat(() => {
return this._delete('file', file);
});
}).then(function (res) {
let resArr = res;
const resArr = res;

// Check that all delete actions are ok
for (let i=0, l=resArr.length; i<l; i++) {
Expand All @@ -1017,11 +1031,11 @@ class StudioHelper {
}
}

return Promise.resolve({
return {
'status': 'ok',
'result': true,
'code': 0
});
};
});
}

Expand Down Expand Up @@ -1334,6 +1348,17 @@ class StudioHelper {
return replaceReadyFiles;
}

/**
* @private
*
* @param {Object[]} studioFiles
* @param {string[]} localFiles
* @param {string} dirPath
* @param {string} studioFolderId
* @param {Object} options
* @param {boolean} [options.createNewVersion=true]
* @param {Object} [options.createdFileHeaders]
*/
getChangedFiles(studioFiles, localFiles, dirPath, studioFolderId, options = {}) {
let self = this;

Expand Down
19 changes: 17 additions & 2 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,7 @@ describe('StudioHelper', function() {
})

describe('#deleteFiles', function () {
before(function () {
beforeEach(function () {
let files = [path.join(getFolder('folders/testfolder1'), 'file1.js'), path.join(getFolder('folders/testfolder1'), 'file-2.js')];

return studio.uploadFiles(files, mainFolder).then(function (res) {
Expand All @@ -992,12 +992,27 @@ describe('StudioHelper', function() {
fileIds.push(files[i].id);
}

fileIds.length.should.be.above(0);
fileIds.length.should.be.above(1);
return studio.deleteFiles(fileIds).then(function (res) {
return res.result.should.equal(true);
});
});
});

it('should delete files when throttle option is used', function () {
return studio.getFiles(mainFolder).then(function (files) {
let fileIds = [];

for (let i=0, l=files.length; i<l; i++) {
fileIds.push(files[i].id);
}

fileIds.length.should.be.above(1);
return studio.deleteFiles(fileIds,{ 'throttle': 5 }).then(function (res) {
return res.result.should.equal(true);
});
});
});
});

describe('Prompt test', function () {
Expand Down

0 comments on commit fd05d2a

Please sign in to comment.