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
|
Line | |
---|
1 | 'use strict';
|
---|
2 |
|
---|
3 | var RingBuffer = require('./ring_buffer');
|
---|
4 |
|
---|
5 | var Pledge = function() {
|
---|
6 | this._complete = false;
|
---|
7 | this._callbacks = new RingBuffer(Pledge.QUEUE_SIZE);
|
---|
8 | };
|
---|
9 |
|
---|
10 | Pledge.QUEUE_SIZE = 4;
|
---|
11 |
|
---|
12 | Pledge.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 |
|
---|
26 | Pledge.prototype.then = function(callback) {
|
---|
27 | if (this._complete) callback();
|
---|
28 | else this._callbacks.push(callback);
|
---|
29 | };
|
---|
30 |
|
---|
31 | Pledge.prototype.done = function() {
|
---|
32 | this._complete = true;
|
---|
33 | var callbacks = this._callbacks, callback;
|
---|
34 | while (callback = callbacks.shift()) callback();
|
---|
35 | };
|
---|
36 |
|
---|
37 | module.exports = Pledge;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.