Skip to content

Commit

Permalink
Merge pull request ember-fastboot#89 from kratiahuja/refactor
Browse files Browse the repository at this point in the history
Refactor parts of ember-app and minor cleanups
  • Loading branch information
rwjblue committed Oct 30, 2016
2 parents 6c61d27 + 7f5aff4 commit 946bb88
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 17 deletions.
80 changes: 64 additions & 16 deletions src/ember-app.js
Expand Up @@ -123,13 +123,10 @@ class EmberApp {
/**
* @private
*
* Initializes the sandbox by evaluating the Ember app's JavaScript
* code, then retrieves the application factory from the sandbox and creates a new
* `Ember.Application`.
* Loads the app and vendor files in the sandbox (Node vm).
*
* @returns {Ember.Application} the Ember application from the sandbox
*/
retrieveSandboxedApp() {
*/
loadAppFiles() {
let sandbox = this.sandbox;
let appFilePath = this.appFilePath;
let vendorFilePath = this.vendorFilePath;
Expand All @@ -146,6 +143,17 @@ class EmberApp {

sandbox.eval(appFile, appFilePath);
debug("app file evaluated");
}

/**
* @private
*
* Create the ember application in the sandbox.
*
*/
createEmberApp() {
let sandbox = this.sandbox;
let appFilePath = this.appFilePath;

// Retrieve the application factory from within the sandbox
let AppFactory = sandbox.run(function(ctx) {
Expand All @@ -161,6 +169,21 @@ class EmberApp {
return AppFactory['default']();
}

/**
* @private
*
* Initializes the sandbox by evaluating the Ember app's JavaScript
* code, then retrieves the application factory from the sandbox and creates a new
* `Ember.Application`.
*
* @returns {Ember.Application} the Ember application from the sandbox
*/
retrieveSandboxedApp() {
this.loadAppFiles();

return this.createEmberApp();
}

/**
* Destroys the app and its sandbox.
*/
Expand All @@ -186,6 +209,39 @@ class EmberApp {
});
}

/**
* @private
*
* Main funtion that creates the app instance for every `visit` request, boots
* the app instance and then visits the given route and destroys the app instance
* when the route is finished its render cycle.
*
* @param {string} path the URL path to render, like `/photos/1`
* @param {Object} fastbootInfo An object holding per request info
* @param {Object} bootOptions An object containing the boot options that are used by
* by ember to decide whether it needs to do rendering or not.
* @param {Object} result
* @return {Promise<instance>} instance
*/
visitRoute(path, fastbootInfo, bootOptions, result) {
let instance;

return this.buildAppInstance()
.then(appInstance => {
instance = appInstance;
result.instance = instance;
registerFastBootInfo(fastbootInfo, instance);

return instance.boot(bootOptions);
})
.then(() => result.instanceBooted = true)
.then(() => instance.visit(path, bootOptions))
.then(() => waitForApp(instance))
.then(() => {
return instance;
});
}

/**
* Creates a new application instance and renders the instance at a specific
* URL, returning a promise that resolves to a {@link Result}. The `Result`
Expand Down Expand Up @@ -225,8 +281,6 @@ class EmberApp {

let doc = bootOptions.document;

let instance;

let result = new Result({
doc: doc,
html: html,
Expand All @@ -246,17 +300,11 @@ class EmberApp {
}, destroyAppInstanceInMs);
}

return this.buildAppInstance()
let instance;
return this.visitRoute(path, fastbootInfo, bootOptions, result)
.then(appInstance => {
instance = appInstance;
result.instance = instance;
registerFastBootInfo(fastbootInfo, instance);

return instance.boot(bootOptions);
})
.then(() => result.instanceBooted = true)
.then(() => instance.visit(path, bootOptions))
.then(() => waitForApp(instance))
.then(() => {
if (!disableShoebox) {
// if shoebox is not disabled, then create the shoebox and send API data
Expand Down
1 change: 0 additions & 1 deletion test/fastboot-shoebox-test.js
Expand Up @@ -66,5 +66,4 @@ describe("FastBootShoebox", function() {
expect(html).to.not.include('<script type="fastboot/shoebox" id="shoebox-key5">{"otherUnicodeChars":"\\u0026\\u0026\\u003e\\u003e\\u003c\\u003c\\u2028\\u2028\\u2029\\u2029"}</script>');
});
});

});

0 comments on commit 946bb88

Please sign in to comment.