stacked icon indicating copy to clipboard operation
stacked copied to clipboard

[feature]: Add `InitializableLazySingleton`

Open sebastianbuechler opened this issue 11 months ago • 2 comments

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:

  1. App starts up and general services get setup directly
  2. User logs in, gets an auth token and then is navigated to the user-specific part of the app
  3. 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 LazySingletons 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

sebastianbuechler avatar Jul 25 '23 20:07 sebastianbuechler

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?

ferrarafer avatar Aug 08 '23 04:08 ferrarafer

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

sebastianbuechler avatar Aug 14 '23 09:08 sebastianbuechler