/
aura.app.js
130 lines (106 loc) · 3.45 KB
/
aura.app.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
define([
'./base',
'./aura.extensions'
], function(base, ExtManager) {
var _ = base.util._,
noop = function() {},
freeze = Object.freeze || noop;
/**
* Aura constructor and main entry point
* Loads mediator & widgets extensions by default.
*/
function App(config) {
this.ref = _.uniqueId('aura_');
this.config = config = config || {};
this.extensions = new ExtManager(this.ref, config.require);
this.core = Object.create(base);
this.sandbox = Object.create(base);
if (config.debug) { this.use('aura/ext/debug'); }
this.use('aura/ext/mediator');
config.widgets = (config.widgets || {});
config.widgets.sources = (config.widgets.sources || { "default" : "widgets" });
this.use('aura/ext/widgets');
return this;
}
/**
* Creates a brand new sandboxe, using the app sandbox object as a prototype
*
*/
App.prototype.createSandbox = function() {
return Object.create(this.sandbox);
};
/**
* Tells the app to init with the given extension.
*
* This method can only be called before the app is actually started.
*
* @param {String|Object|Function} ref the reference of the extension
* @return {Aura} the Aura app object
*/
App.prototype.use = function(ref) {
this.extensions.add({ ref: ref, context: this });
return this;
};
/**
* Adds a new source for widgets
*
* @param {String} name the name of the source
* @param {String} baseUrl the base url for those widgets
*/
App.prototype.registerWidgetsSource = function(name, baseUrl) {
if (this.config.widgets.sources[name]) {
throw new Error("Widgets source '" + name + "' is already registered");
}
this.config.widgets.sources[name] = baseUrl;
return this;
};
/**
* Application start.
* Bootstraps the extensions loading process
* @return {Promise} a promise that resolves when the app is started
*/
App.prototype.start = function(options) {
var app = this;
if (app.started) {
console.error("Aura already started... !");
return app.extensions.initStatus;
}
app.startOptions = options || {};
app.started = true;
app.extensions.onReady(function(exts) {
// Then we call all the `afterAppStart` provided by the extensions
base.util.each(exts, function(i, ext) {
if (ext && typeof(ext.afterAppStart) === 'function') {
try {
ext.afterAppStart(app);
} catch(e) {
console.error("Error on ", (ext.name || ext) , ".afterAppStart callback: (", e.message, ")", e);
}
}
});
// freeze(app.sandbox);
// freeze(app.core);
});
// If there was an error in the boot sequence we
// reject every body an do some cleanup
// TODO: Provide a better error message to the user.
app.extensions.onFailure(function() {
console.error("Error initializing app...", app.config.name, arguments);
app.stop();
});
// Finally... we return a promise that allows
// to keep track of the loading process...
//
return app.extensions.init();
};
/**
* Stops the application and unregister its loaded dependencies.
* TODO: We need to do a little more cleanup here...
* @return {void}
*/
App.prototype.stop = function() {
this.started = false;
// unregisterDeps(this.ref, Object.keys(allDeps.apps[env.appRef] || {}));
};
return App;
});