Skip to content

Deprecate the usage of Ember.on for Component's lifecycle events. #174

Closed

Description

@givanse

Request

Deprecate this:

Ember.Component.extend({
  someRandomName: Ember.on('didInsertElement', function() {
  })
});

In favor of:

Ember.Component.extend({
  didInsertElement: function() {
    this._super(...arguments);
  }
});

Reasoning

Ember.Component.extend({
  doA: Ember.on('willInsertElement', function() { },
  doB: Ember.on('willInsertElement', function() { }
})

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.

Activity

  1. stefanpenner commented on Oct 26, 2016

    @stefanpenner
    Member

    and all the on hooks are slow

  2. mmun commented on Oct 26, 2016

    @mmun
    Member

    Another point against the Ember.on style is that you are forced to choose a key name (doA, doB) for each of the event handlers.

  3. givanse commented on Oct 27, 2016

    @givanse
    Author

    Since on hooks are slow, should the scope be to completely deprecate Ember.on? Doesn't seem to be used that much:

    cd emberjs/ember.js
    grep -R -e '\.on(' packages/ | wc -l
    47
    # a couple less than that, the regex is not perfect
    

    Not assuming that is trivial, but it doesn't seem to be all over the place either.

  4. locks commented on Oct 27, 2016

    @locks
    Contributor

    @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.

  5. givanse commented on Oct 27, 2016

    @givanse
    Author

    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?

  6. locks commented on Oct 27, 2016

    @locks
    Contributor

    @givanse it's how you listen to Ember events :P

  7. stefanpenner commented on Oct 27, 2016

    @stefanpenner
    Member

    @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.

  8. cibernox commented on Nov 18, 2016

    @cibernox
    Contributor

    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.

  9. DingoEatingFuzz commented on Nov 18, 2016

    @DingoEatingFuzz

    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.

  10. DingoEatingFuzz commented on Nov 18, 2016

    @DingoEatingFuzz

    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.

  11. DingoEatingFuzz commented on Nov 18, 2016

    @DingoEatingFuzz

    @locks How is that relevant to general commentary on method overriding?

  12. locks commented on Nov 18, 2016

    @locks
    Contributor

    Whoops, I saw my comments repeated and deleted one of them. They weren't repeated after all.

  13. AKST commented on Jan 24, 2017

    @AKST

    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?

  14. kkincade commented on Aug 3, 2017

    @kkincade

    @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.

  15. givanse commented on Sep 28, 2017

    @givanse
    Author

    @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.

  16. rmwxiong commented on Sep 28, 2017

    @rmwxiong

    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?

  17. locks commented on Nov 25, 2020

    @locks
    Contributor

    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?

  18. givanse commented on Nov 25, 2020

    @givanse
    Author

    Very happy to close! Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions