You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Deprecate the usage of Ember.on for Component's lifecycle events. #174
The order in which doA and doB happens is unknown. This code might be the result of a refactoring. Or, a developer new to the code base not noticing that the event is being observed already. Subtle bugs can be easily introduced.
It is also easier to read and understand the behavior of a component if there is a single place where all the event dependent code is executed. Tracking down all the different pieces of code that are executed at different moments in the lifecycle is cumbersome and can be easily avoided by using overrides.
One argument that might come up against overrides is:
what if people forgets to call super? that can introduce bugs too!
It is easier to know that you always have to have that call inside the hook, there is determinism. Whereas keeping track of all the Ember.ons that might be cohabiting an object is not straightforward. When reading the code, you have to keep a mental map of all the pieces that might be executed by a hook; the cognitive load is high.
Reacted by Chad Hietala, Karthick C, Praveenkumar, Kavin B, Gastón Silva, Simon Ihmig, Esteban Lara, Aaron Sikes and bekzod
@givanse which packages are this? Ember.on, for framework hook methods, is widely used in applications in the wild so I'd rather the events that have hook method alternatives to be deprecated.
It was emberjs/ember.js/packages. (updated the snippet)
Ember.on can be removed by its users over time, just as views were removed. A deprecation warning can be put in place for as long as needed. Aside from wide spread usage what else can be said in favor of .on?
@givanse which packages are this? Ember.on, for framework hook methods, is widely used in applications in the wild so I'd rather the events that have hook method alternatives to be deprecated.
Yes, I would really like to get critical mass on-board with moving away from these. We burn many extra CPU cycles speculatively trigging a huge number of events during both ember-data models, and ember component lifecycles. Although we do also speculative call the method hooks, those atleast perform much better then the event listener equivalents.
In addition to the perf overhead, the ordering/extended traps these have over just method + super cause real pain, not to mention yet another thing to teach.
FWIW, I've ditched Ember.on a long time ago and I fully support this.
The transition to ES6 classes can be the perfect time, so if glimmer-components includes the changes in the ObjectModel as part of it, I'd push to also use that transition as an opportunity to remove this.
I'm apparently not in the majority here, but I like .on. Even though, as @mmun points out, you have to choose a key name, I find that far better than having to use this._super. Case and point, there is a bug in your sample code, @givanse:
Ember.Component.extend({didInsertElement: function(){this._super(...arguments);// You need to return here}});
Without returning the result of this._super, you aren't properly overriding that method. This isn't an issue with the .on style. As for the sequence in which multiple .ons fire, the trivial solution is to put imperative code in a single listener.
My opinions here aren't strong, but this thread was in need of a counter-argument.
For didInsertElement in particular, you don't, but generally speaking, the super may return something other than undefined. And even if the super doesn't return today, it could in the future. This is more prevalent in Ember Data.
As a user who up to this point has avoided overriding lifecycle hooks, I was unaware the use of Ember.on was that bad, I always consider it the safer approach as you didn't have to always remember to call this._super plus I found the super call kind of unergonomic, so I've always recommended people use Ember.on instead. This was mostly because I was bitten by this early on.
Is it worth pursing a more ergonomic alternative to overriding the methods and calling super?
@stefanpenner when you say the on hooks are slow, have you quantified this? I'm curious to know how much slower the on hooks are from the hook method alternative.
@DingoEatingFuzz Returning values from a lifecycle hook is something I've never seen done in an Ember application. However, it is possible to find code where the lifecycle hooks will return this (maybe old code in addons, libraries). The main reason for this is that lifecycle hooks are abstract interfaces and they used to be initialized by default with the value Ember.K which is nothing more than a function that returns this. The chaining capabilities of that function didn't really see wide spread usage. And, Ember.K was deprecated.
It seems like both of the current options have drawbacks and can be prone to bugs in different use cases. _super can cause issues if it is called at the wrong time, with the wrong arguments, or is forgotten. .on can cause issues if there are multiple occurrences of them and the order matters for code execution.
Is there an acceptable API which can avoid both of these issues? Perhaps a dedicated hook like extendDidInsertElement, but with a better name. By default it could maybe call _super with any supplied arguments before running the code inside?
Thanks everyone for the discussion! @givanse what do you feel about closing this issue given that Glimmer components are now the default implementation and they do not have lifecycle hooks?
Activity
stefanpenner commentedon Oct 26, 2016
and all the
onhooks are slowmmun commentedon Oct 26, 2016
Another point against the
Ember.onstyle is that you are forced to choose a key name (doA,doB) for each of the event handlers.givanse commentedon Oct 27, 2016
Since
onhooks are slow, should the scope be to completely deprecateEmber.on? Doesn't seem to be used that much:Not assuming that is trivial, but it doesn't seem to be all over the place either.
locks commentedon Oct 27, 2016
@givanse which packages are this?
Ember.on, for framework hook methods, is widely used in applications in the wild so I'd rather the events that have hook method alternatives to be deprecated.givanse commentedon Oct 27, 2016
It was
emberjs/ember.js/packages. (updated the snippet)Ember.oncan be removed by its users over time, just as views were removed. A deprecation warning can be put in place for as long as needed. Aside from wide spread usage what else can be said in favor of.on?locks commentedon Oct 27, 2016
@givanse it's how you listen to Ember events :P
stefanpenner commentedon Oct 27, 2016
Yes, I would really like to get critical mass on-board with moving away from these. We burn many extra CPU cycles speculatively trigging a huge number of events during both ember-data models, and ember component lifecycles. Although we do also speculative call the method hooks, those atleast perform much better then the event listener equivalents.
In addition to the perf overhead, the ordering/extended traps these have over just
method+supercause real pain, not to mention yet another thing to teach.cibernox commentedon Nov 18, 2016
FWIW, I've ditched
Ember.ona long time ago and I fully support this.The transition to ES6 classes can be the perfect time, so if glimmer-components includes the changes in the ObjectModel as part of it, I'd push to also use that transition as an opportunity to remove this.
DingoEatingFuzz commentedon Nov 18, 2016
I'm apparently not in the majority here, but I like
.on. Even though, as @mmun points out, you have to choose a key name, I find that far better than having to usethis._super. Case and point, there is a bug in your sample code, @givanse:Without returning the result of
this._super, you aren't properly overriding that method. This isn't an issue with the.onstyle. As for the sequence in which multiple.ons fire, the trivial solution is to put imperative code in a single listener.My opinions here aren't strong, but this thread was in need of a counter-argument.
DingoEatingFuzz commentedon Nov 18, 2016
For
didInsertElementin particular, you don't, but generally speaking, the super may return something other thanundefined. And even if the super doesn't return today, it could in the future. This is more prevalent in Ember Data.DingoEatingFuzz commentedon Nov 18, 2016
@locks How is that relevant to general commentary on method overriding?
locks commentedon Nov 18, 2016
Whoops, I saw my comments repeated and deleted one of them. They weren't repeated after all.
AKST commentedon Jan 24, 2017
As a user who up to this point has avoided overriding lifecycle hooks, I was unaware the use of
Ember.onwas that bad, I always consider it the safer approach as you didn't have to always remember to callthis._superplus I found the super call kind of unergonomic, so I've always recommended people useEmber.oninstead. This was mostly because I was bitten by this early on.Is it worth pursing a more ergonomic alternative to overriding the methods and calling super?
kkincade commentedon Aug 3, 2017
@stefanpenner when you say the on hooks are slow, have you quantified this? I'm curious to know how much slower the on hooks are from the hook method alternative.
givanse commentedon Sep 28, 2017
@DingoEatingFuzz Returning values from a lifecycle hook is something I've never seen done in an Ember application. However, it is possible to find code where the lifecycle hooks will return
this(maybe old code in addons, libraries). The main reason for this is that lifecycle hooks are abstract interfaces and they used to be initialized by default with the valueEmber.Kwhich is nothing more than a function that returnsthis. The chaining capabilities of that function didn't really see wide spread usage. And,Ember.Kwas deprecated.rmwxiong commentedon Sep 28, 2017
It seems like both of the current options have drawbacks and can be prone to bugs in different use cases.
_supercan cause issues if it is called at the wrong time, with the wrong arguments, or is forgotten..oncan cause issues if there are multiple occurrences of them and the order matters for code execution.Is there an acceptable API which can avoid both of these issues? Perhaps a dedicated hook like
extendDidInsertElement, but with a better name. By default it could maybe call _super with any supplied arguments before running the code inside?locks commentedon Nov 25, 2020
Thanks everyone for the discussion!
@givanse what do you feel about closing this issue given that Glimmer components are now the default implementation and they do not have lifecycle hooks?
givanse commentedon Nov 25, 2020
Very happy to close! Thanks.