1
0

semaphore.js 773 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /*
  2. * Copyright (c) 2018
  3. *
  4. * This file is licensed under the Affero General Public License version 3
  5. * or later.
  6. *
  7. * See the COPYING-README file.
  8. *
  9. */
  10. (function(){
  11. var Semaphore = function(max) {
  12. var counter = 0;
  13. var waiting = [];
  14. this.acquire = function() {
  15. if(counter < max) {
  16. counter++;
  17. return new Promise(function(resolve) { resolve(); });
  18. } else {
  19. return new Promise(function(resolve) { waiting.push(resolve); });
  20. }
  21. };
  22. this.release = function() {
  23. counter--;
  24. if (waiting.length > 0 && counter < max) {
  25. counter++;
  26. var promise = waiting.shift();
  27. promise();
  28. }
  29. };
  30. };
  31. // needed on public share page to properly register this
  32. if (!OCA.Files) {
  33. OCA.Files = {};
  34. }
  35. OCA.Files.Semaphore = Semaphore;
  36. })();