Skip to content
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

Async construction? #59

Open
benjamingr opened this issue Aug 2, 2022 · 5 comments
Open

Async construction? #59

benjamingr opened this issue Aug 2, 2022 · 5 comments
Labels
enhancement New feature or request

Comments

@benjamingr
Copy link

Hey, I might need to download a bundle containing a service when resolving it so I need a way to .get asynchronously and have async factories (like in InversifyJS for example).

Alternatively if plugins could return a Promise I guess that would also work for this use case.

@benjamingr benjamingr added the enhancement New feature or request label Aug 2, 2022
@hbroer
Copy link
Contributor

hbroer commented Aug 16, 2022

Hey, the dependency/service can be a Promise itself, doesn't that help?

const TYPE = {
    Dependency: token<Promise<Dependency>>("dependency"),
};

container.bind<Dependency>(TYPE.Dependency)).toFactory(async () => {
  const dependency = new Dependency();
  await dependency.someAsyncFunction();
  return dependency;
});

@benjamingr
Copy link
Author

How would that work with recursive dependencies?

@hbroer
Copy link
Contributor

hbroer commented Aug 16, 2022

Recursion should work as normal if you don't access the constructor in a constructor, or access a function in a factory which calls a function of the depending service. But that's always a problem.

async function one() { await two(); }
async function two() { await one(); }

one();

I never had a usecase where I needed a async service. I use Promise a lot but not on that level. Only where needed in methods or getter.

@benjamingr
Copy link
Author

I never had a usecase where I needed a async service. I use Promise a lot but not on that level. Only where needed in methods or getter.

Basically the use case I have is mixing DI with code-splitting and dependencies may live in a different not-yet-loaded chunk

@hbroer
Copy link
Contributor

hbroer commented Aug 29, 2022

I never had a usecase where I needed a async service. I use Promise a lot but not on that level. Only where needed in methods or getter.

Basically the use case I have is mixing DI with code-splitting and dependencies may live in a different not-yet-loaded chunk

This should work without making the service a promise. I had a project in the past wich used async-router (with Preact). The key was to load a seperate bootstrap for that module which binds and loads the service. The project also had some global services which where loaded on the global bootstrap.

I think about this way: The view can only be used if everything is ready, all services are loaded and the view is ready to receive data. This is one chunk to load and the fastest part of the delivered data to the user (static asset cache of the webserver or even the browser cache), the then to load dynamic data is what takes a while and you can display meanwhile animated placeholders within the layout of that module. You can make it more complicated but IMO this is the fastest, still very userfriendly and most efficient way. You don't need promises everywhere, just in key places.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants