server/apps/files/js/semaphore.js
Tomasz Grobelny 1f6f276fa0 Add progress reporting to move and copy operations
Signed-off-by: Tomasz Grobelny <tomasz@grobelny.net>
2018-11-24 23:55:33 +01:00

37 lines
675 B
JavaScript

/*
* Copyright (c) 2018
*
* This file is licensed under the Affero General Public License version 3
* or later.
*
* See the COPYING-README file.
*
*/
(function(){
var Semaphore = function(max) {
var counter = 0;
var waiting = [];
this.acquire = function() {
if(counter < max) {
counter++;
return new Promise(function(resolve) { resolve(); });
} else {
return new Promise(function(resolve) { waiting.push(resolve); });
}
};
this.release = function() {
counter--;
if (waiting.length > 0 && counter < max) {
counter++;
var promise = waiting.shift();
promise();
}
};
};
OCA.Files.Semaphore = Semaphore;
})();