-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCore.js
180 lines (151 loc) · 3.99 KB
/
Core.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
///////////////////////////////////////////////////////////////////////////////
//
// @Class : Wax_Core
//
///////////////////////////////////////////////////////////////////////////////
define(function(require){
// @imports
var $ = require('jquery');
var _ = require('underscore');
var Base = require('Wax/Base');
var Facade = require('Wax/Facade');
var Router = require('Wax/Router');
// Class //////////////////////////////////////////////////////////////////
var Wax_Core = Base.extend(function(){
// @private
var _self;
var _trace;
var _facade;
var _router;
var _defer;
var _modules;
// INIT ///////////////////////////////////////////////////////////////
function init(){
_self = this;
_trace = this.Trace;
_facade = new Facade(_self);
_router = new Router();
_defer = [];
_modules = [];
_trace.log('Wax_Core::init', _self.config);
}
// pick route config and initialize all resources
function boot(){
var cfg = _router.getRouteConfig();
if (cfg){
// aquire module resources
_.each(cfg, function(resource, i){
// requirejs - load defined resources
require([resource['PATH']], function(M){
// store module obj
register({
ID : resource['ID'],
Class : M,
instance : undefined,
config : undefined
});
// do we need the module at startup?
if (resource['INIT']){
// if def is async, start immediately else defer
if ( resource['ASYNC'] ){
start(resource['ID']);
} else {
_defer.push(resource['ID']);
}
// if we've booted all resources, start deferred modules
if (i == cfg.length-1 && _defer.length>0){
defer();
}
}
});
});
} else {
throw new Error('Route Config is Missing!');
}
}
// instantiate module
function invoke(module){
if (module){
if (typeof module.Class == 'function'){
//try{
module.instance = new module.Class(_facade);
module.config = module.instance.getConfig();
module.instance.start();
//} catch(e){
//_trace.log('Failed to Instatiate module!', e, module);
//}
} else {
throw new Error('Failed to determine Module Class!', module);
}
} else {
throw new Error('Error invoking module resource!', module);
}
}
// start a module
function start(ID){
var module = pick(ID);
if (module){
if (module.instance !== undefined){
if (module.config.isStarted === false){
module.instance.start();
}
} else {
invoke(module);
}
} else {
throw new Error('Error starting module!', ID, module);
}
}
// if we have stored any syncronous modules, start them
function defer(){
_.each(_defer, function(ID){
start(ID);
});
}
// stop a module
function stop(ID){
var module = pick(ID);
if (module){
if (module.instance !== undefined){
if (module.config.isStarted === true){
module.instance.stop();
}
}
} else {
throw new Error('Error stopping module!', ID, module);
}
}
function startAll(){}
function stopAll(){}
// register module object in module list
function register(module){
_trace.log('Wax_Core::register', module);
_modules.push(module);
}
function unregister(module){}
// Helpers ////////////////////////////////////////////////////////////
// get module status
function status(ID){
var module = pick(ID);
return module.config;
}
// find module node
function pick(ID){
return _.find(_modules, function(obj){
return obj.ID == ID;
});
}
// Public /////////////////////////////////////////////////////////////
return {
init : init,
boot : boot,
start : start,
stop : stop,
startAll : startAll,
stopAll : stopAll,
status : status
}
}());
// Export /////////////////////////////////////////////////////////////////
return Wax_Core;
});