stacked
stacked copied to clipboard
[feature]: Add `InitializableLazySingleton`
Is your feature request related to a problem? Please describe.
If I have a singleton for the locator that has async initialization but also should be lazy (for example because of a required successful login), then I can't use stacked properly for now.
So let's consider this scenario:
- App starts up and general services get setup directly
- User logs in, gets an auth token and then is navigated to the user-specific part of the app
- User specific services (like for example one that fetches his personal messages) will need to be initialized, but async as they call an API for initial data
The services in step three are not LazySingleton
s because they are async and not InitializableSingleton
because they are lazy.
Describe the solution you would like
Add the combination of LazySingleton
and InitializableSingleton
for stacked locator.
Additional Context
No response
Hi @sebastianbuechler , how are you?
The thing is that you have to create the instance of the service to execute its init method. When you finish executing the init method, it is registered in the service locator. If the service is not instantiated, how would you execute the init method?
Hi @ferrarafer, doing pretty good, thanks for asking. How about you?
If I'm not mistaken get_it
already supports this with the registryLazySingletonAsync
method: https://pub.dev/documentation/get_it/latest/get_it/GetIt/registerLazySingletonAsync.html