aboutsummaryrefslogtreecommitdiff
path: root/node_modules/montage/core/promise-queue.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/montage/core/promise-queue.js')
-rw-r--r--node_modules/montage/core/promise-queue.js65
1 files changed, 65 insertions, 0 deletions
diff --git a/node_modules/montage/core/promise-queue.js b/node_modules/montage/core/promise-queue.js
new file mode 100644
index 00000000..11eeb788
--- /dev/null
+++ b/node_modules/montage/core/promise-queue.js
@@ -0,0 +1,65 @@
1
2var Montage = require("./core").Montage;
3var Promise = require("./promise").Promise;
4
5exports.PromiseQueue = Montage.create(Montage, {
6 init: {
7 value: function () {
8 this._ends = Promise.defer();
9 this._closed = Promise.defer();
10 this.closed = this._closed.promise;
11 return this;
12 }
13 },
14 put: {
15 value: function (value) {
16 var next = Promise.defer();
17 this._ends.resolve({
18 head: value,
19 tail: next.promise
20 });
21 this._ends.resolve = function (resolution) {
22 next.resolve(resolution);
23 };
24 }
25 },
26 get: {
27 value: function () {
28 var ends = this._ends;
29 var result = ends.promise.get("head");
30 this._ends = {
31 resolve: function (resolution) {
32 ends.resolve(resolution);
33 },
34 promise: ends.promise.get("tail")
35 };
36 return result.fail(function (reason, error, rejection) {
37 this._closed.resolve();
38 return rejection;
39 });
40 }
41 },
42 close: {
43 value: function (reason, error, rejection) {
44 var end = {
45 head: rejections || Promise.reject(reason, error)
46 };
47 end.tail = end;
48 this._ends.resolve(end);
49 return this._closed.promise;
50 }
51 },
52 forEach: {
53 value: function (put, thisp) {
54 var queue = this;
55 function loop() {
56 return queue.get().then(function (value) {
57 put.call(thisp, value);
58 })
59 .then(loop);
60 }
61 return loop();
62 }
63 }
64});
65