Skip to content

Commit

Permalink
wip...
Browse files Browse the repository at this point in the history
  • Loading branch information
sbellity committed Jan 13, 2013
1 parent 7ba85cb commit a064967
Show file tree
Hide file tree
Showing 10 changed files with 331 additions and 264 deletions.
133 changes: 55 additions & 78 deletions lib/aura.app.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,53 @@
define([
'./base',
'./aura.extensions'
], function(Ext) {
], function(base, ExtManager) {

var _ = base.util._,
noop = function() {},
freeze = Object.freeze || noop;

/**
* Aura constructor and main entry point
* This is used as a factory to create new apps
* Loads mediator & widgets extensiony by default.
* Loads mediator & widgets extensions by default.
*/
function App() {
this.ref = env.appRef;
env.config = (base.configure(config) || {});
env.config.widgets = (env.config.widgets || {});
env.config.widgets.sources = (env.config.widgets.sources || { "default" : "widgets" });
if (env.config.debug) {
this.use('aura/ext/debug');
}
function App(config) {
this.ref = _.uniqueId('aura_');
this.config = config = (base.configure(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} ext the reference of the extension
* @param {String|Object|Function} ref the reference of the extension
* @return {Aura} the Aura app object
*/
App.prototype.use = function(ext) {
if (env.initStatus.state() === 'resolved') {
throw new Error("You cannot extend an already initialized app !"); // really ?
}
registerExtension(ext);
App.prototype.use = function(ref) {
this.extensions.add({ ref: ref, context: this });
return this;
};

Expand All @@ -42,12 +57,11 @@ define([
* @param {String} name the name of the source
* @param {String} baseUrl the base url for those widgets
*/
App.prototype.registerWidgetsSource =
env.core.registerWidgetsSource = function(name, baseUrl) {
if (env.config.widgets.sources[name]) {
App.prototype.registerWidgetsSource = function(name, baseUrl) {
if (this.config.widgets.sources[name]) {
throw new Error("Widgets source '" + name + "' is already registered");
}
env.config.widgets.sources[name] = baseUrl;
this.config.widgets.sources[name] = baseUrl;
return this;
};

Expand All @@ -58,77 +72,40 @@ define([
* @return {Promise} a promise that resolves when the app is started
*/
App.prototype.start = function(options) {
if (env.started) {
var app = this;

if (app.started) {
console.error("Aura already started... !");
return env.initStatus;
return app.extensions.initStatus;
}

env.options = options || {};

env.started = true;

var self = this,
loadingExtensions = deferred(),
loadedExtensions;

// Enforce sequencial loading of extensions...
// The `loadingExtensions` promise resolves to the actually resolved and loaded extensions...
(function extLoader(i) {
if (typeof(i) !== 'number') {
i = 0;
loadedExtensions = [];
}
if (extensions[i]) {
var ext = loadExtension(extensions[i], env),
w = when(ext);

w.then(function(e) {
loadedExtensions[i] = e;
extLoader(i+1);
});
w.fail(function() {
loadingExtensions.reject();
});
} else {
loadingExtensions.resolve(loadedExtensions);
}
})();

loadingExtensions.then(function(exts) {
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, onStart) {
if (typeof(onStart) === 'function') {
onStart();
base.util.each(exts, function(i, ext) {
if (ext && typeof(ext.afterAppStart) === 'function') {
ext.afterAppStart(app);
}
});

// If a callback is provided as an argument
// of the app.start method, we call it here
if (typeof env.options.onInit === 'function') {
env.options.onInit.call(self, env);
}

// Once all the extensions are loaded, we augment
// the application with its own sandbox
// that can be used... from the outside world...
base.util.extend(self, createSandbox());

// Then we finally resolve the main `initStatus` promise
env.initStatus.resolve(self);
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.
loadingExtensions.fail(function() {
env.initStatus.reject("Error initializing app...", env.config.name, arguments);
self.stop();
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 env.initStatus;
return app.extensions.init();
};


Expand All @@ -138,9 +115,9 @@ define([
* @return {void}
*/
App.prototype.stop = function() {
env.started = false;
env.initStatus = deferred();
unregisterDeps(env.appRef, Object.keys(allDeps.apps[env.appRef] || {}));
this.started = false;

// unregisterDeps(this.ref, Object.keys(allDeps.apps[env.appRef] || {}));
};


Expand Down
3 changes: 3 additions & 0 deletions lib/aura.deps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
define(function() {

});
Loading

0 comments on commit a064967

Please sign in to comment.