Skip to content

[reactive-element] addInitializer adds initializer to superclass #3373

Closed

Description

@christophe-g

Which package(s) are affected?

Lit Core (lit / lit-html / lit-element / reactive-element)

Description

While looking #3296 a bit more in depth, I realize that the culprit is not @lit-labs/context but ReactiveElement.addInitializer, which will add intializers to the superclass if it already contains its own initializers.

Documentation states:

Initializers are stored per-constructor. Adding an initializer to a subclass does not add it to a superclass. Since initializers are run in constructors, initializers will run in order of the class hierarchy, starting with superclasses and progressing to the instance's class.

This is not what happens as addInitializer only checks if this._initializer exists on the Class prototype chain. In case a superclass already has one, it will be added to it (the superclass). We should have a hasOwnProperty check there.

   /*
   * Initializers are stored per-constructor. Adding an initializer to a
   * subclass does not add it to a superclass. Since initializers are run in
   * constructors, initializers will run in order of the class hierarchy,
   * starting with superclasses and progressing to the instance's class.
   *
   * @nocollapse
   */
  static addInitializer(initializer: Initializer) {
    this._initializers ??= [];
    this._initializers.push(initializer);
  }

Reproduction

  • go to this playground
  • See that the base class has 2 initializers (while it shoul donly have one)

Workaround

none

Is this a regression?

No or unsure. This never worked, or I haven't tried before.

Affected versions

Failing in 2.4.0

Browser/OS/Node environment

NA

Activity

  1. changed the title [-]ReactiveElement.addInitializer adds initializer to superclass[/-] [+][reactive-element] addInitializer adds initializer to superclass[/+] on Oct 18, 2022
  2. moved this to 📋 Triaged in Lit Project Boardon Oct 18, 2022
  3. justinfagnani commented on Oct 18, 2022

    @justinfagnani
    Collaborator

    Thanks for investigating this and for the repro @christophe-g !

    We can prioritize a fix for this, or you could contribute it if you would like to. The biggest requirement would be a good test.

  4. sorvell commented on Oct 18, 2022

    @sorvell
    Member

    Just a quick note, this should be fixed in the finalize step where we handle similar behavior for properties and styles. The superclass initializers should be copied into a set of initializers owned by (and not shared with!) the superclass.

  5. christophe-g commented on Oct 18, 2022

    @christophe-g
    ContributorAuthor

    @justinfagnani - honestly, while I'd like to contribute, I am already pretty stretched over a couple of important deadlines on my end ; )

    The fix should be pretty straighforward and the lit team shall be in better position to efficiently tackle this (esp. on the good test requirement).

  6. Repository owner moved this from 📋 Triaged to ✅ Done in Lit Project Boardon Oct 23, 2022
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

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions