2018-11-14 21:39:12 +00:00
|
|
|
/*
|
|
|
|
* 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();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-05-07 21:48:06 +00:00
|
|
|
// needed on public share page to properly register this
|
|
|
|
if (!OCA.Files) {
|
|
|
|
OCA.Files = {};
|
|
|
|
}
|
2018-11-14 21:39:12 +00:00
|
|
|
OCA.Files.Semaphore = Semaphore;
|
|
|
|
|
|
|
|
})();
|