/
processing-stack.js
220 lines (200 loc) · 6.1 KB
/
processing-stack.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
* Mobius-JS
* /\ \
* / \ \
* / \ \
* / \ \
* / /\ \ \
* / / \ \ \
* / / \ \ \
* / / / \ \ \
* / / / \ \ \
* / / /---------' \
* / / /_______________\
* \ / /
* \/_____________________/
*
* Benjamin Coe (BenjaminCoe.com) - MIT Licensed.
*
* Description: This class uses a stack to abstract heavily
* nested asynchronous programming away from a user of the framework.
*/
// Dependencies.
var resig = require('resig/resig');
var sys = require('sys');
var helpers = require('mobius-js/helpers/helpers');
var mongodb = require('mobius-js/db/mongodb')
/**
* A Mobius processing-stack inherits from a Resig class.
*/
var ProcessingStack = resig.Class.extend( {
processingStack: []
});
/**
* Synchronous processing stack abstracts away asynchronous behavior.
* Note: Do not access this method directly, instead use ProcessingStack.extend({});
*
* @type void
* @public
*/
ProcessingStack.prototype.init = function(dbConfiguration, callback) {
var self = this; // Store reference to 'this' for closures.
self.dbConfiguration = dbConfiguration;
setTimeout(function() {
self._processStack(self);
}, 10);
this.connect(callback);
},
/**
* Called by a model to place a create operation on the stack.
*
* @param {object} params key value pairs for creating a new model.
* @param {function} callback function to execute upon this operations completion.
* @type void
* @public
*/
ProcessingStack.prototype.create = function(params, callback) {
callback = callback || function() {};
this.processingStack.push([function(self, params, callback) {
mongodb.create(self, params, callback);
}, params, callback]);
},
/**
* Called by a model to place an update operation on the stack.
*
* @param {object} params key value pairs for creating a new model.
* @param {function} callback function to execute upon this operations completion.
* @type void
* @public
*/
ProcessingStack.prototype.update = function(params, callback) {
callback = callback || function() {};
this.processingStack.push([function(self, params, callback) {
mongodb.update(self, params, callback);
}, params, callback]);
},
/**
* Called by a model to place a remove operation on the stack.
*
* @param {object} params key value pairs for creating a new model.
* @param {function} callback function to execute upon this operations completion.
* @type void
* @public
*/
ProcessingStack.prototype.remove = function(params, callback) {
callback = callback || function() {};
this.processingStack.push([function(self, params, callback) {
mongodb.remove(self, params, callback);
}, params, callback]);
},
/**
* Called by a model to place an operation on the stack which initializes model indexes.
*
* @param {object} params key value pairs for model index creation operation.
* @param {function} callback function to execute upon this operations completion.
* @type void
* @public
*/
ProcessingStack.prototype.createIndex = function(params, callback) {
callback = callback || function() {};
this.processingStack.push([function(self, params, callback) {
mongodb.createIndex(self, params, callback);
}, params, callback]);
},
/**
* Called by a model to place a connect operation on the stack.
*
* @param {function} callback function to execute once connection is complete.
* @type void
* @public
*/
ProcessingStack.prototype.connect = function(callback) {
callback = callback || function(){}
this.processingStack.push([function(self, params, callback) {
mongodb.connect(self, function(db) {
self.db = db;
callback();
});
}, {}, callback]);
},
/**
* Performs a hard reset of the Database connection.
*
* @param {function} callback function to execute once connection is complete.
* @type void
* @public
*/
ProcessingStack.prototype.reset = function(callback) {
callback = callback || function(){}
this.processingStack.push([function(self, params, callback) {
mongodb.close(self, params, function() {
mongodb.connect(self, function(db) {
self.db = db;
callback();
});
});
}, {}, callback]);
},
/**
* Called by a model to place a close operation on the stack.
*
* @type void
* @public
*/
ProcessingStack.prototype.close = function() {
this.processingStack.push([function(self, params, callback) {
mongodb.close(self, params, callback);
}, {}, function(){}])
},
/**
* A stack operation that returns models from the database as a parameter
* to the callback provided.
*
* @param {object} Parameters for restricting and sorting the models returned from the DB.
* @param {function} callback function to return all models retrieved to.
* @type void
* @public
*/
ProcessingStack.prototype.find = function(params, callback) {
this.processingStack.push([function(self, params, callback) {
mongodb.find(self, params, callback);
}, params, callback])
},
/**
* Called by a MobiusController to queue up the rendering of a view,
* so that it is performed after all other database operations.
*
* @param {object} parameters needed by Express' render function.
* @param {function} callback the function that actually performs rendering.
* @type void
* @public
*/
ProcessingStack.prototype.render = function(params, callback) {
this.processingStack.push([function(self, params, callback) {
callback(params);
}, params, callback])
},
/**
* Consumes from the processingStack and performs asynchronous operations
* in a synchronous fashion.
*
* @param {object} self a reference to the processing stack.
* @type void
* @private
*/
ProcessingStack.prototype._processStack = function(self) {
var callMe = self.processingStack.shift();
if (callMe) {
callMe[0](self, callMe[1], function(data) {
callMe[2](data);
setTimeout(function() {
self._processStack(self);
}, 10);
}); // Call the action of this controller.
} else {
setTimeout(function() {
self._processStack(self);
}, 10);
}
}
exports.ProcessingStack = ProcessingStack;