Inital Commit
[oweals/finalsclub.git] / node_modules / mongodb / deps / step / lib / step.js
1 /*
2 Copyright (c) 2011 Tim Caswell <tim@creationix.com>
3
4 MIT License
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24
25 // Inspired by http://github.com/willconant/flow-js, but reimplemented and
26 // modified to fit my taste and the node.JS error handling system.
27 function Step() {
28   var steps = Array.prototype.slice.call(arguments),
29       pending, counter, results, lock;
30
31   // Define the main callback that's given as `this` to the steps.
32   function next() {
33
34     // Check if there are no steps left
35     if (steps.length === 0) {
36       // Throw uncaught errors
37       if (arguments[0]) {
38         throw arguments[0];
39       }
40       return;
41     }
42
43     // Get the next step to execute
44     var fn = steps.shift();
45     counter = pending = 0;
46     results = [];
47
48     // Run the step in a try..catch block so exceptions don't get out of hand.
49     try {
50       lock = true;
51       var result = fn.apply(next, arguments);
52     } catch (e) {
53       // Pass any exceptions on through the next callback
54       next(e);
55     }
56
57
58     // If a syncronous return is used, pass it to the callback
59     if (result !== undefined) {
60       next(undefined, result);
61     }
62     lock = false;
63   }
64
65   // Add a special callback generator `this.parallel()` that groups stuff.
66   next.parallel = function () {
67     var index = 1 + counter++;
68     pending++;
69
70     function check() {
71       if (pending === 0) {
72         // When they're all done, call the callback
73         next.apply(null, results);
74       }
75     }
76     process.nextTick(check); // Ensures that check is called at least once
77
78     return function () {
79       pending--;
80       // Compress the error from any result to the first argument
81       if (arguments[0]) {
82         results[0] = arguments[0];
83       }
84       // Send the other results as arguments
85       results[index] = arguments[1];
86       if (!lock) { check(); }
87     };
88   };
89
90   // Generates a callback generator for grouped results
91   next.group = function () {
92     var localCallback = next.parallel();
93     var counter = 0;
94     var pending = 0;
95     var result = [];
96     var error = undefined;
97
98     function check() {
99       if (pending === 0) {
100         // When group is done, call the callback
101         localCallback(error, result);
102       }
103     }
104     process.nextTick(check); // Ensures that check is called at least once
105
106     // Generates a callback for the group
107     return function () {
108       var index = counter++;
109       pending++;
110       return function () {
111         pending--;
112         // Compress the error from any result to the first argument
113         if (arguments[0]) {
114           error = arguments[0];
115         }
116         // Send the other results as arguments
117         result[index] = arguments[1];
118         if (!lock) { check(); }
119       };
120     };
121   };
122
123   // Start the engine an pass nothing to the first step.
124   next();
125 }
126
127 // Tack on leading and tailing steps for input and output and return
128 // the whole thing as a function.  Basically turns step calls into function
129 // factories.
130 Step.fn = function StepFn() {
131   var steps = Array.prototype.slice.call(arguments);
132   return function () {
133     var args = Array.prototype.slice.call(arguments);
134
135     // Insert a first step that primes the data stream
136     var toRun = [function () {
137       this.apply(null, args);
138     }].concat(steps);
139
140     // If the last arg is a function add it as a last step
141     if (typeof args[args.length-1] === 'function') {
142       toRun.push(args.pop());
143     }
144
145
146     Step.apply(null, toRun);
147   }
148 }
149
150
151 // Hook into commonJS module systems
152 if (typeof module !== 'undefined' && "exports" in module) {
153   module.exports = Step;
154 }