Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented #172 with promises #257

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions lib/aura.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ define([
*/
Aura.prototype.start = function(options) {
var app = this;
var extsPromisses = [];
var appInitDfd = base.data.deferred();

if (app.started) {
app.logger.error("Aura already started... !");
Expand All @@ -119,10 +121,28 @@ define([

// Then we call all the `afterAppStart` provided by the extensions
base.util.each(exts, function(i, ext) {
if (ext && typeof(ext.afterAppStart) === 'function') {
if (ext) {
try {
ext.afterAppStart(app);
extsPromisses.push((ext.afterAppStart || noop)(app));

// application is loaded when all widgets has been loaded
if (extsPromisses.length === exts.length) { // all widgets are here
base.data.when.apply(undefined, extsPromisses).then(
function () {
appInitDfd.resolve();
},

function (e) {
appInitDfd.reject();

app.logger.error('Error in widget loading: ' + e);
}
);
}

} catch(e) {
appInitDfd.reject();

app.logger.error("Error on ", (ext.name || ext) , ".afterAppStart callback: (", e.message, ")", e);
}
}
Expand All @@ -133,6 +153,8 @@ define([
// reject every body an do some cleanup
// TODO: Provide a better error message to the user.
app.extensions.onFailure(function() {
appInitDfd.reject();

app.logger.error("Error initializing app...", app.config.name, arguments);
app.stop();
});
Expand All @@ -141,7 +163,9 @@ define([
// to keep track of the loading process...
//

return app.extensions.init();
app.extensions.init();

return appInitDfd.promise();
};

/**
Expand Down
24 changes: 17 additions & 7 deletions lib/ext/widgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ define('aura/ext/widgets', function() {
this._ref = options._ref;
this.$el = core.dom.find(options.el);

this.initialize.call(this, this.options);
this.options.onLoadPromise = this.initialize.call(this, this.options);

return this;
}

Expand Down Expand Up @@ -143,12 +144,20 @@ define('aura/ext/widgets', function() {

var newWidget = new WidgetConstructor(options);

var initialized = core.data.when(newWidget);
core.data.when(newWidget.options.onLoadPromise).then(
function(ret) {
dfd.resolve(ret);

app.logger.log("Widget loaded:", name, newWidget);
},

function(ret) {
dfd.reject(ret);

app.logger.error("Widget not loaded:", name, newWidget);
}
);

initialized.then(function(ret) { dfd.resolve(ret); });
initialized.fail(function(err) { dfd.reject(err); });
app.logger.log("Widget loaded:", name, newWidget);
return initialized;
} catch(err) {
app.logger.error(err.message);
dfd.reject(err);
Expand Down Expand Up @@ -292,7 +301,8 @@ define('aura/ext/widgets', function() {
afterAppStart: function(app) {
// Auto start widgets when the app is loaded.
if (app.startOptions.widgets) {
app.core.start(app.startOptions.widgets);
// return a widget promise to handle async loading in Aura layer
return app.core.start(app.startOptions.widgets);
}
}

Expand Down