Skip to content

[RFC] Drop lifecyle from Directive config #3924

Closed

Description

@vicb

The current way of subscribing to LC hooks is kind of messy:

   @Directive({
      lifecycle: [LifecycleEvent.OnChanges, ...]
   })

In Dart:

If the lifecycle key exists, we use it as the source of truth:

  • The lifecycle configuration gets generated by the transformer from the implemented interfaces, that is fine,
  • For non-transformed code, the lifecycle value has priority over implemented interfaces. It's counter-intuitive and might lead to a different behavior vs transformed code.

In JS:

The lifecycle value has priority over a structural type checking. Once again this could cause unexpected behavior if the lifecycle value is no updated when LC hooks are added or removed.

* Proposal*:

Remove the lifecycle config option from Directive:

  • In Dart, you would only have to implement the desired interfaces,
  • In JS, you would have to name methods as LC hooks for them to pass the structural type check,
  • In TS, the preferred way would be to implement the interfaces as well but the JS way would work equally well.
// Dart & TS (preferred)
class MyDirective implements OnChanges {
  onChanges() {
  }
}

// ES6 & TS (non-preferred)
class MyDirective {
  onChanges() {
  }
}

The only pb I can imagine is related to #2898 where @yjbanov wants to remove interfaces from the generated metadata in Dart. What about having a white list of interfaces we want to include there ?

I'd like to collect some thoughts before doing the changes, WDYT @mhevery @yjbanov @vsavkin @jakemac53 @kegluneq ?

The effort would probably be around .5 to 1 day

Activity

  1. mhevery commented on Aug 31, 2015

    @mhevery
    Contributor

    I think this is great, and I would love to see this happen. It is not clear to me if we can do this due to way Dart Transformers work.

  2. yjbanov commented on Sep 1, 2015

    @yjbanov
    Contributor

    I don't see a conflict with #2898. We either use interfaces in a runtime check is OnChange (in dev mode) or we discover them in the transformer and generate lifecycle call sites (and let dart2js erase the interfaces later).

    Not sure how this would work in TS, as tsc erases all information about interfaces.

  3. vicb commented on Sep 1, 2015

    @vicb
    ContributorAuthor

    As of today, it seems that the compiler calls DirectiveBinding.createFromBinding that in turn check what interfaces are implemented. At some later point we should probably retrieved the metadata generated by the transformer instead. When this is implemented there will be no need for the reflector to have information about implemented interfaces (at least for the LC hooks).

    TS (as JS) would use structural type check (implements OnHook is optional but would be documented as the best practice).

  4. mhevery commented on Sep 1, 2015

    @mhevery
    Contributor

    @vicb can this be refactored somehow so that we could drop LifecycleEvent? I really would like to get this breaking change in ASAP.

  5. added a commit that references this issue on Sep 2, 2015
    611cab4
  6. angular-automatic-lock-bot commented on Sep 7, 2019

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  7. locked and limited conversation to collaborators on Sep 7, 2019
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