Skip to content

Commit b20e775

Browse files
committed
refactor how timed app instance destroy works
1 parent 5a8fe13 commit b20e775

File tree

2 files changed

+25
-25
lines changed

2 files changed

+25
-25
lines changed

src/ember-app.js

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -226,17 +226,31 @@ class EmberApp {
226226
* @param {Object} result
227227
* @return {Promise<instance>} instance
228228
*/
229-
visitRoute(path, fastbootInfo, bootOptions, result) {
229+
visitRoute(path, fastbootInfo, bootOptions, result, destroyAppInstanceInMs) {
230230
return this.buildAppInstance()
231231
.then(instance => {
232232
result.instance = instance;
233-
result._startDestroyTimer();
234-
registerFastBootInfo(fastbootInfo, instance);
235-
return instance.boot(bootOptions);
236-
})
237-
.then(() => result.instanceBooted = true)
238-
.then(() => result.instance.visit(path, bootOptions))
239-
.then(() => fastbootInfo.deferredPromise);
233+
return new Promise(function(resolve, reject) {
234+
let timer;
235+
if (destroyAppInstanceInMs > 0) {
236+
// start a timer to destroy the appInstance forcefully in the given ms.
237+
// This is a failure mechanism so that node process doesn't get wedged if the `visit` never completes.
238+
timer = setTimeout(()=> {
239+
if (result._destroyAppInstance()) {
240+
reject(new Error('App instance was forcefully destroyed in ' + destroyAppInstanceInMs + 'ms'));
241+
}
242+
}, destroyAppInstanceInMs);
243+
}
244+
245+
registerFastBootInfo(fastbootInfo, instance);
246+
instance.boot(bootOptions)
247+
.then(() => result.instanceBooted = true)
248+
.then(() => result.instance.visit(path, bootOptions))
249+
.then(() => fastbootInfo.deferredPromise)
250+
.then(() => clearTimeout(timer))
251+
.then(resolve, reject);
252+
});
253+
});
240254
}
241255

242256
/**
@@ -277,9 +291,9 @@ class EmberApp {
277291

278292
let doc = bootOptions.document;
279293

280-
let result = new Result({ doc, html, fastbootInfo, destroyAppInstanceInMs });
294+
let result = new Result({ doc, html, fastbootInfo });
281295

282-
return this.visitRoute(path, fastbootInfo, bootOptions, result)
296+
return this.visitRoute(path, fastbootInfo, bootOptions, result, destroyAppInstanceInMs)
283297
.then(() => {
284298
if (!disableShoebox) {
285299
// if shoebox is not disabled, then create the shoebox and send API data

src/result.js

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ class Result {
1515
this._doc = options.doc;
1616
this._html = options.html;
1717
this._fastbootInfo = options.fastbootInfo;
18-
this._destroyAppInstanceInMs = options.destroyAppInstanceInMs;
19-
this._destroyAppInstanceTimer = undefined;
2018
}
2119

2220
/**
@@ -69,6 +67,7 @@ class Result {
6967
* to this Result instance.
7068
*/
7169
_finalize() {
70+
if (this._instanceDestroyed) { return this; }
7271
if (this.finalized) {
7372
throw new Error("Results cannot be finalized more than once");
7473
}
@@ -99,23 +98,10 @@ class Result {
9998
}
10099
}
101100

102-
_startDestroyTimer() {
103-
if (this._destroyAppInstanceInMs > 0 && !this._instanceDestroyed) {
104-
// start a timer to destroy the appInstance forcefully in the given ms.
105-
// This is a failure mechanism so that node process doesn't get wedged if the `visit` never completes.
106-
this._destroyAppInstanceTimer = setTimeout(()=> {
107-
if (this._destroyAppInstance()) {
108-
this.error = new Error('App instance was forcefully destroyed in ' + this._destroyAppInstanceInMs + 'ms');
109-
}
110-
}, this._destroyAppInstanceInMs);
111-
}
112-
}
113-
114101
_destroyAppInstance() {
115102
if (this.instance && !this._instanceDestroyed) {
116103
this._instanceDestroyed = true;
117104
this.instance.destroy();
118-
clearTimeout(this._destroyAppInstanceTimer);
119105
return true;
120106
}
121107
return false;

0 commit comments

Comments
 (0)