-
-
Notifications
You must be signed in to change notification settings - Fork 60
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
Implementation for scoped components #416
Comments
It's because you can have a scoped inject in a different module than the component so it can't know ahead of time every thing to create a property for. The map allows them to be created dynamically |
Oh, I see I'm adding an example from the tests to be sure I see that the generated code is directly using the scoped LazyMap so it makes sense, it moves the creation closer to the usage (typically not in ExternalParent nor ExternalChild class, even if they define the bind) To complete a bit the overall question, I wanted to define a scope of long-living services for my app and I thought it would be nice to avoid recreating it (typically for caches). @Component
abstract class NestedExternalScopedComponent(@Component val parent: ExternalChildComponent) {
abstract val foo: IExternalFoo // is defined in the parent: external child component
}
/* generated */
public class InjectNestedExternalScopedComponent(
parent: ExternalChildComponent,
) : NestedExternalScopedComponent(parent) {
override val foo: IExternalFoo
get() = with(parent.parent) {
(this as ScopedComponent)._scoped.get("me.tatarka.inject.test.module.ScopedExternalFoo") {
ScopedExternalFoo()
}.bind
}
}
// and the parent.parent is
public class InjectExternalParentComponent() : ExternalParentComponent(), ScopedComponent {
override val _scoped: LazyMap = LazyMap()
}
|
I checked the integration tests and I saw some benchmark |
Yeah the benchmark was to see if there was a meaningful performance difference from using a map vs lazy prop, the result was there was not.
Not sure what you mean by this? |
I just have in mind the most basic class possible class Example {
val a: A = A() // instead of lazy { A() }
} |
That has different semantics though, being lazy is important for the expected behavior. |
I agree, but still I think it can be interesting to measure |
I was looking at the generated code today (the following example comes from the tests).
And I didn't completely get why it uses a LazyMap for ScopedComponent wheras I guessed a Lazy property was enough.
So I'm just interested to see the main idea, thanks!
The text was updated successfully, but these errors were encountered: