source: trip-planner-front/node_modules/websocket-extensions/lib/pipeline/pledge.js@ 188ee53

Last change on this file since 188ee53 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 779 bytes
RevLine 
[6a3a178]1'use strict';
2
3var RingBuffer = require('./ring_buffer');
4
5var Pledge = function() {
6 this._complete = false;
7 this._callbacks = new RingBuffer(Pledge.QUEUE_SIZE);
8};
9
10Pledge.QUEUE_SIZE = 4;
11
12Pledge.all = function(list) {
13 var pledge = new Pledge(),
14 pending = list.length,
15 n = pending;
16
17 if (pending === 0) pledge.done();
18
19 while (n--) list[n].then(function() {
20 pending -= 1;
21 if (pending === 0) pledge.done();
22 });
23 return pledge;
24};
25
26Pledge.prototype.then = function(callback) {
27 if (this._complete) callback();
28 else this._callbacks.push(callback);
29};
30
31Pledge.prototype.done = function() {
32 this._complete = true;
33 var callbacks = this._callbacks, callback;
34 while (callback = callbacks.shift()) callback();
35};
36
37module.exports = Pledge;
Note: See TracBrowser for help on using the repository browser.