Skip to content

Commit

Permalink
refactor how timed app instance destroy works
Browse files Browse the repository at this point in the history
  • Loading branch information
bekzod committed Jun 5, 2017
1 parent 5a8fe13 commit b20e775
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
34 changes: 24 additions & 10 deletions src/ember-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,17 +226,31 @@ class EmberApp {
* @param {Object} result
* @return {Promise<instance>} instance
*/
visitRoute(path, fastbootInfo, bootOptions, result) {
visitRoute(path, fastbootInfo, bootOptions, result, destroyAppInstanceInMs) {
return this.buildAppInstance()
.then(instance => {
result.instance = instance;
result._startDestroyTimer();
registerFastBootInfo(fastbootInfo, instance);
return instance.boot(bootOptions);
})
.then(() => result.instanceBooted = true)
.then(() => result.instance.visit(path, bootOptions))
.then(() => fastbootInfo.deferredPromise);
return new Promise(function(resolve, reject) {
let timer;
if (destroyAppInstanceInMs > 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.
timer = setTimeout(()=> {
if (result._destroyAppInstance()) {
reject(new Error('App instance was forcefully destroyed in ' + destroyAppInstanceInMs + 'ms'));
}
}, destroyAppInstanceInMs);
}

registerFastBootInfo(fastbootInfo, instance);
instance.boot(bootOptions)
.then(() => result.instanceBooted = true)
.then(() => result.instance.visit(path, bootOptions))
.then(() => fastbootInfo.deferredPromise)
.then(() => clearTimeout(timer))
.then(resolve, reject);
});
});
}

/**
Expand Down Expand Up @@ -277,9 +291,9 @@ class EmberApp {

let doc = bootOptions.document;

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

return this.visitRoute(path, fastbootInfo, bootOptions, result)
return this.visitRoute(path, fastbootInfo, bootOptions, result, destroyAppInstanceInMs)
.then(() => {
if (!disableShoebox) {
// if shoebox is not disabled, then create the shoebox and send API data
Expand Down
16 changes: 1 addition & 15 deletions src/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ class Result {
this._doc = options.doc;
this._html = options.html;
this._fastbootInfo = options.fastbootInfo;
this._destroyAppInstanceInMs = options.destroyAppInstanceInMs;
this._destroyAppInstanceTimer = undefined;
}

/**
Expand Down Expand Up @@ -69,6 +67,7 @@ class Result {
* to this Result instance.
*/
_finalize() {
if (this._instanceDestroyed) { return this; }
if (this.finalized) {
throw new Error("Results cannot be finalized more than once");
}
Expand Down Expand Up @@ -99,23 +98,10 @@ class Result {
}
}

_startDestroyTimer() {
if (this._destroyAppInstanceInMs > 0 && !this._instanceDestroyed) {
// 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.
this._destroyAppInstanceTimer = setTimeout(()=> {
if (this._destroyAppInstance()) {
this.error = new Error('App instance was forcefully destroyed in ' + this._destroyAppInstanceInMs + 'ms');
}
}, this._destroyAppInstanceInMs);
}
}

_destroyAppInstance() {
if (this.instance && !this._instanceDestroyed) {
this._instanceDestroyed = true;
this.instance.destroy();
clearTimeout(this._destroyAppInstanceTimer);
return true;
}
return false;
Expand Down

0 comments on commit b20e775

Please sign in to comment.