-
Notifications
You must be signed in to change notification settings - Fork 69
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
[refactor] Common function for Ember-App instance destruction #93
Conversation
d616761
to
74bf773
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After a quick read this seems good, tests added for hanging processes still pass so it seems to work.
@kratiahuja - Can you review / confirm also?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general this looks much better to me than what I had done.
|
||
return function() { | ||
clearTimeout(destructionTimer); | ||
instance.destroy(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to make sure we call instance.destroy()
only once. Can we make sure if the instance is destroyed with a timer doesn't call instance.destroy()
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ohoo sorry this will work. I had only added tests for this. You can drop this.
@@ -176,6 +176,24 @@ class EmberApp { | |||
}); | |||
} | |||
|
|||
destroyAppInstance(result, timeout) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add some comments to this function just like other functions have. Makes it easier to generate an API at a later point.
74bf773
to
04ba304
Compare
@kratiahuja I thought about what you said and I created a slightly more complicated
Interestingly, destroying the application instance in a timeout still does not stop the application instance from executing. Because of that my new test intermittently throws this error:
It happens with both your original code and my refactored code. It is caught in the promise chain's @kratiahuja and @rwjblue, thoughts? |
@arjansingh Does your slow app have I had thought about this and that error is actually expected. The reason being (if I understand the semantics of run loop correctly), if something is scheduled on the run loop, it depends on when it runs. It will run (when it comes to the front of the queue) irrespective of what the state of your instance is when it runs. In general, the idea that I had of using the Another usecase of something similar happening (without
afterModel: function() {
Ember.run.schedule('afterRender', function() {
Ember.run.later(this, function() {
// run something after 500 ms when scheduled in the `afterRender` queue
}, 500);
});
If you have an I would prefer we slowly add these fail safe mechanisms in fastboot itself to make this library more robust. As a long term thing, I would like to also help evolve the fastboot userguide to educate the users on the best practices for server side rendering of their ember apps. More like "how should i write my ember app such that it is performant on server side as well?" I would like @rwjblue to chime in his thoughts too 😄 |
The code and code it is refactoring seem to be unaware that the run loop is used in destroy and that it is async. |
yes that is because we don't run the destroy in the context of the sandbox. We actually should do that. |
Whenever you schedule things for later like this you should always keep track of the returned afterModel: function() {
Ember.run.schedule('afterRender',() => {
if (this._isDestroying) { return; }
this._cancelId = Ember.run.later(() => {
// run something after 500 ms when scheduled in the `afterRender` queue
}, 500);
});
},
willDestroy() {
Ember.run.cancel(this._cancelId);
} The code above is definitely more annoying to write, but is more correct than the initial snippet. |
Agreed. |
Writing the above reply made me remember that it isn't possible to cancel things that are scheduled by |
@rwjblue Agreed we should always cancel the scheduled task. But a lot of times apps don't do that causing node process to probably crash on the server side. This is the reason I would like the library to be more robust to such hard failures. On the client side, you only have one instance in the browser so it may not hit that hard. After speaking with Kris offline last night, he suggested an idea that the instance destroy in Node should probably allow you to automatically cancel things scheduled by that instance in the run loop. It's not a blocker for this PR but something we probably should evaluate in the long run. |
@rwjblue just saw you published ember-lifeline which answers my long term question. Thank you! |
Opened an issue to track destroying of app instance and other tasks in the sandbox context: #94 |
So the take aways are:
Do we still want my refactor? We should at least keep the new test case. |
@arjansingh I think we should still have the refactor since it is much neater and cleaner API. Regarding the take aways:
I will let @rwjblue make the final call. |
04ba304
to
52a22e6
Compare
@rwjblue Is there anything preventing this from merging? In general, I like @arjansingh refactor in this PR |
Not needed. Closing. |
@rwjblue @kratiahuja
This was the suggested refactor I had in mind for the timeout work. I think adding a helper function makes the logic more compact, and eliminates a lot of the state checks that we needed to do when the code was inline.