From fd05d2a09c93119bfddac4acad7e5b4b2033b9a6 Mon Sep 17 00:00:00 2001 From: osaton <o@sploosh.org> Date: Tue, 17 Mar 2020 15:37:26 +0200 Subject: [PATCH] Add throttle option to #deleteFiles --- StudioHelper.js | 41 +++++++++++++++++++++++++++++++++-------- test/main.js | 19 +++++++++++++++++-- 2 files changed, 50 insertions(+), 10 deletions(-) diff --git a/StudioHelper.js b/StudioHelper.js index 69e15a5..dd44a43 100644 --- a/StudioHelper.js +++ b/StudioHelper.js @@ -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++) { @@ -1017,11 +1031,11 @@ class StudioHelper { } } - return Promise.resolve({ + return { 'status': 'ok', 'result': true, 'code': 0 - }); + }; }); } @@ -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; diff --git a/test/main.js b/test/main.js index a8821f9..4c96823 100644 --- a/test/main.js +++ b/test/main.js @@ -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) { @@ -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 () {