a zip code crypto-currency system good for red ONLY

Semaphore.js 535B

123456789101112131415161718192021222324252627282930313233
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. class Semaphore {
  7. constructor(available) {
  8. this.available = available;
  9. this.waiters = [];
  10. }
  11. acquire(callback) {
  12. if(this.available > 0) {
  13. this.available--;
  14. callback();
  15. } else {
  16. this.waiters.push(callback);
  17. }
  18. }
  19. release() {
  20. if(this.waiters.length > 0) {
  21. const callback = this.waiters.pop();
  22. process.nextTick(callback);
  23. } else {
  24. this.available++;
  25. }
  26. }
  27. }
  28. module.exports = Semaphore;