Skip to content

Commit

Permalink
[refactor] Common function for Ember-App instance destruction
Browse files Browse the repository at this point in the history
  • Loading branch information
Arjan Singh committed Oct 29, 2016
1 parent ff877c7 commit d616761
Showing 1 changed file with 25 additions and 28 deletions.
53 changes: 25 additions & 28 deletions src/ember-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,24 @@ class EmberApp {
});
}

destroyAppInstance(result, timeout) {
const instance = result.instance;

if (!timeout) return () => instance.destroy();

// start a timer to destroy the appInstance forcefully in the given ms.
// This is a failure mechanism so that node process doesn't get wedged if the `visit` never completes.
const destructionTimer = setTimeout(function() {
instance.destroy();
result.error = new Error(`App instance was forcefully destroyed in ${timeout}ms`);
}, timeout);

return function() {
clearTimeout(destructionTimer);
instance.destroy();
};
}

/**
* 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 @@ -203,7 +221,7 @@ class EmberApp {
let res = options.response;
let html = options.html || this.html;
let disableShoebox = options.disableShoebox || false;
let destroyAppInstanceInMs = options.destroyAppInstanceInMs;
let destroyAppInstanceInMs = parseInt(options.destroyAppInstanceInMs, 10);

let shouldRender = (options.shouldRender !== undefined) ? options.shouldRender : true;
let bootOptions = buildBootOptions(shouldRender);
Expand All @@ -215,38 +233,26 @@ class EmberApp {

let doc = bootOptions.document;

let instance;

let result = new Result({
doc: doc,
html: html,
fastbootInfo: fastbootInfo
});

let destroyAppInstanceTimer;
if (parseInt(destroyAppInstanceInMs, 10) > 0) {
// start a timer to destroy the appInstance forcefully in the given ms.
// This is a failure mechanism so that node process doesn't get wedged if the `visit` never completes.
destroyAppInstanceTimer = setTimeout(function() {
if (instance && !result.instanceDestroyed) {
result.instanceDestroyed = true;
result.error = new Error('App instance was forcefully destroyed in ' + destroyAppInstanceInMs + 'ms');
instance.destroy();
}
}, destroyAppInstanceInMs);
}

return this.buildAppInstance()
.then(appInstance => {
instance = appInstance;
const instance = appInstance;

result.instance = instance;
result.destroyInstance = this.destroyAppInstance(result, destroyAppInstanceInMs);
registerFastBootInfo(fastbootInfo, instance);

return instance.boot(bootOptions);
})
.then(() => result.instanceBooted = true)
.then(() => instance.visit(path, bootOptions))
.then(() => waitForApp(instance))
.then(() => result.instance.visit(path, bootOptions))
.then(() => waitForApp(result.instance))
.then(() => {
if (!disableShoebox) {
// if shoebox is not disabled, then create the shoebox and send API data
Expand All @@ -255,16 +261,7 @@ class EmberApp {
})
.catch(error => result.error = error)
.then(() => result._finalize())
.finally(() => {
if (instance && !result.instanceDestroyed) {
result.instanceDestroyed = true;
instance.destroy();

if (destroyAppInstanceTimer) {
clearTimeout(destroyAppInstanceTimer);
}
}
});
.finally(() => result.destroyInstance());
}

/**
Expand Down

0 comments on commit d616761

Please sign in to comment.