/
bearcat.js
380 lines (338 loc) · 8.83 KB
/
bearcat.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*!
* .______ _______ ___ .______ ______ ___ .__________.
* ( _ ) ( ____) / \ ( _ ) ( ) / \ ( )
* | |_) ) | |__ / ^ \ | |_) ) | ,----' / ^ \ `---| |---`
* | _ < | __) / /_\ \ | ) | | / /_\ \ | |
* | |_) ) | |____ / _____ \ | |) ----.| `----./ _____ \ | |
* (______) (_______/__/ \__\ ( _| `.____) (______)__/ \__\ |__|
*
* Bearcat App
* Copyright(c) 2014 fantasyni <fantasyni@163.com>
* MIT Licensed
*/
var logger = require('pomelo-logger').getLogger('bearcat', 'app');
var ApplicationContext = require('./context/applicationContext');
var BeanFactory = require('./beans/beanFactory');
var Package = require('../package.json');
var Utils = require('./util/utils');
var Root;
(function() {
Root = this;
}());
/*!
* Application states
*/
var STATE_NEW = 1; // app new
var STATE_INITED = 2; // app has inited
var STATE_START = 3; // app start
var STATE_STARTED = 4; // app has started
/**
* Bearcat app object.
* @api public
*/
var Bearcat = {
opts: null,
configLocations: null,
applicationContext: null,
state: STATE_NEW,
startTime: null,
version: Package.version
};
module.exports = Bearcat;
/**
* Bearcat createApp constructor function.
*
* @param {Array} configLocations context path array
* @param {Object} opts
* @param {String} opts.NODE_ENV setup env
* @param {String} opts.BEARCAT_ENV setup env
* @param {String} opts.NODE_CPATH setup config path
* @param {String} opts.BEARCAT_CPATH setup config path
* @param {String} opts.BEARCAT_LOGGER setup 'off' to turn off bearcat logger configuration
* @param {String} opts.BEARCAT_HOT setup 'off' to turn off bearcat hot code reloading
* @param {String} opts.BEARCAT_ANNOTATION setup 'off' to turn off bearcat $ based annotation
* @param {String} opts.BEARCAT_GLOBAL setup bearcat to be global object
*
* @return {Object} bearcat object
* @api public
*/
Bearcat.createApp = function(configLocations, opts) {
if (!Utils.checkArray(configLocations) && Utils.checkObject(configLocations)) {
opts = configLocations;
configLocations = [];
}
this.opts = opts || {};
this.configLocations = configLocations || [];
if (this.opts['BEARCAT_GLOBAL']) {
Root.bearcat = Bearcat;
}
var configLocations = this.configLocations;
if (!configLocations) {
return cb(new Error('Bearcat createApp error, configLocations null'));
}
if (!Utils.checkObject(this.opts)) {
logger.warn('Bearcat createApp opts must be object...');
}
this.applicationContext = new ApplicationContext(configLocations, this.opts);
this.state = STATE_INITED;
return Bearcat;
}
/**
* Bearcat start app.
*
* @param {Function} cb start callback function
* @api public
*/
Bearcat.start = function(cb) {
if (!Utils.checkFunction(cb)) {
cb = function() {}
}
if (this.state > STATE_INITED) {
logger.warn('Bearcat has already start, run bearcat.stop to start again.');
return cb();
}
if (this.state < STATE_INITED) {
logger.warn('Bearcat does not inited, run bearcat.createApp to init.');
return cb();
}
this.state = STATE_START;
this.startTime = Date.now();
var self = this;
var env = this.applicationContext.getEnv();
if (Utils.checkBrowser()) {
this.applicationContext.setEnv(env);
env = 'browser';
}
this.applicationContext.on('finishRefresh', function() {
self.state = STATE_STARTED;
logger.info('Bearcat startup in %s with %s ms', env, Date.now() - self.startTime);
cb();
});
this.applicationContext.refresh();
}
/**
* Bearcat stop app.
* it will stop internal applicationContext, destroy all singletonBeans
*
* @api public
*/
Bearcat.stop = function() {
if (this.applicationContext) {
this.applicationContext.destroy();
}
this.applicationContext = null;
this.configLocations = null;
this.startTime = null;
this.state = STATE_NEW;
}
/**
* Bearcat get beanFactory instance.
*
* @return {Object} beanFactory instance
* @api public
*/
Bearcat.getBeanFactory = function() {
if (this.state <= STATE_INITED) {
logger.warn('Bearcat application is not running now for %j', "getBeanFactory");
return;
}
return this.applicationContext.getBeanFactory();
}
/**
* Bearcat get applicationContext.
*
* @return {Object} applicationContext
* @api public
*/
Bearcat.getApplicationContext = function() {
if (this.state <= STATE_INITED) {
logger.warn('Bearcat application is not running now for %j', "getApplicationContext");
return;
}
return this.applicationContext;
}
/**
* Bearcat get bean from IoC container through meta argument.
*
* Examples:
*
* bearcat.getBeanByMeta({
* id: "car",
* func: Car // Car is a function constructor
* });
*
* @param {Object} meta meta object
* @api public
*/
Bearcat.getBeanByMeta = function(meta) {
if (this.state <= STATE_INITED) {
logger.warn('Bearcat application is not running now for %j', "getBeanByMeta");
return;
}
return this.applicationContext.getBeanByMeta(meta);
}
/**
* Bearcat get bean from IoC container through $ annotation function.
*
* Examples:
*
* bearcat.getBeanByFunc(function() {
* this.$id = "car";
* this.$scope = "prototype";
* });
*
* @param {Function} func $ annotation function
* @api public
*/
Bearcat.getBeanByFunc = function(func) {
if (this.state <= STATE_INITED) {
logger.warn('Bearcat application is not running now for %j', "getBeanByFunc");
return;
}
return this.applicationContext.getBeanByFunc(func);
}
/**
* Bearcat add async loading beans, this just add beans needed to be loaded to bearcat.
*
* Examples:
*
* bearcat.use(['car']);
* bearcat.use('car');
*
* @param {Array|String} async loading beans id
* @api public
*/
Bearcat.use = function(ids) {
if (Utils.checkArray(ids)) {
return this.applicationContext.use(ids);
}
if (Utils.checkString(ids)) {
return this.applicationContext.use([ids]);
}
}
/**
* Bearcat async loading beans.
*
* Examples:
*
* bearcat.async(['car'], function(car) {
* // car is ready
* });
*
* @param {Array|String} async loading beans id
* @return {Function} callback with loaded bean instances
* @api public
*/
Bearcat.async = function(ids, cb) {
if (Utils.checkArray(ids)) {
return this.applicationContext.async(ids, cb);
}
if (Utils.checkString(ids)) {
return this.applicationContext.async([ids], cb);
}
}
/**
* Bearcat add module(bean) to IoC container through $ annotation function.
*
* Examples:
*
* bearcat.module(function() {
* this.$id = "car";
* this.$scope = "prototype";
* });
*
* @param {Function} func $ annotation function
* @api public
*/
Bearcat.module = function(func, context) {
if (this.state < STATE_STARTED) {
return this.applicationContext.module(func, context);
} else {
return this.getBean(func);
}
}
/**
* Bearcat get bean from IoC container through beanName or meta argument.
*
* Examples:
*
*
* // through beanName
* var car = bearcat.getBean("car");
*
* // through meta
* var car = bearcat.getBean({
* id: "car",
* func: Car // Car is a function constructor
* });
*
* // through $ annotation func
* var car = bearcat.getBean(function() {
* this.$id = "car";
* this.$scope = "prototype";
* });
*
* @param {String} beanName
* @return {Object} bean
* @api public
*/
Bearcat.getBean = function(beanName) {
if (this.state <= STATE_INITED) {
logger.warn('Bearcat application is not running now for %j %j', "getBean", this.state);
return;
}
arguments = Array.prototype.slice.apply(arguments);
var firstarg = arguments[0];
var func = "";
if (typeof firstarg === 'object') {
func = "getBeanByMeta";
} else if (typeof firstarg === 'function') {
func = "getBeanByFunc";
} else if (typeof firstarg === 'string') {
func = "getBean";
} else {
logger.error('Bearcat application unsupported getBean arguments for %s', beanName);
return;
}
return this.applicationContext[func].apply(this.applicationContext, arguments);
}
/**
* Bearcat get bean constructor function from IoC container through beanName.
*
* Examples:
*
*
* // through beanName
* var Car = bearcat.getFunction("car");
*
*
* @param {String} beanName
* @return {Function} bean constructor function
* @api public
*/
Bearcat.getFunction = function(beanName) {
if (this.state <= STATE_INITED) {
logger.warn('Bearcat application is not running now for %j %j', "getFunction", this.state);
return;
}
return Bearcat.getBeanFactory().getBeanFunction(beanName);
}
/**
* Bearcat convenient function for using in MVC route mapping.
*
* Examples:
*
*
* // express
* var app = express();
* app.get('/', bearcat.getRoute('bearController', 'index'));
*
*
* @param {String} beanName
* @param {String} fnName routeName
* @api public
*/
Bearcat.getRoute = function(beanName, fnName) {
var bean = Bearcat.getBean(beanName);
return bean[fnName].bind(bean);
}