diff options
author | Jose Antonio Marquez | 2012-04-20 10:44:02 -0700 |
---|---|---|
committer | Jose Antonio Marquez | 2012-04-20 10:44:02 -0700 |
commit | 0b7848540a7ca84b7c67821fedc275cf243eff2f (patch) | |
tree | a26b2986d412a02eb93fc7fa1b0f495cab40e757 /node_modules/montage/core/promise-queue.js | |
parent | 154d1a8fdbbe050dbfc97cd13baa8981d8fc1e47 (diff) | |
parent | 8a546291a9d0de3bda1a7765ae9008f2ad052bba (diff) | |
download | ninja-0b7848540a7ca84b7c67821fedc275cf243eff2f.tar.gz |
Merge branch 'refs/heads/Ninja-Internal' into FileIO
Diffstat (limited to 'node_modules/montage/core/promise-queue.js')
-rw-r--r-- | node_modules/montage/core/promise-queue.js | 65 |
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 | |||
2 | var Montage = require("./core").Montage; | ||
3 | var Promise = require("./promise").Promise; | ||
4 | |||
5 | exports.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 | |||