-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
@tracked: lazy evaluation #18118
Comments
@buschtoens this should work in all cases for framework classes, all base classes should be assigning the owner in the root constructor, before any subclasses assign their fields. Are you manually assigning the owner to a class? |
Yes, in this case the class was not created through the container, but instantiated manually. Should we declare manual instantiation an anti-pattern and instead require the creation through the container? |
If you need access to the container, I would tend to say yes. The way most container classes work now is essentially: class GlimmerComponent {
constructor(owner) {
setOwner(this, owner);
}
} This is how all subclasses are able to immediately access the owner and injections. You could setup your own classes to do something like this, or you could avoid accessing injections until after you assign the owner in general. Unfortunately, we cannot safely give the owner to a class in any other way, so you do have to do all access after |
Cool. I can live with that. 👍 Maybe we should add a note about this somewhere in the guides? I can imagine that more people will start instantiating native utility classes themselves, now that it's officially supported. |
While investigating #18075, I noticed a minor issue, that persists even after the change to
WeakMap
in #18091, because of the lazy evaluationAssume
Foo
gets created by a container lookup or get manually calledsetOwner
on.Intuitively I would expect the above code to work. And it does.
If
this.someProp
is only accessed later in the template or ininit
, everything works fine.However, if you already access
this.someProp
in theconstructor
, you get aTypeError
, becausethis.someService
isundefined
.While I understand why, I still think this is inconsistent. If we decide to keep lazy evaluation of the initializers, we need to document it well. Would the new static decorators proposal support lazy evaluation in the first place?
The text was updated successfully, but these errors were encountered: