question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

feat: support for asynchronous factory functions

See original GitHub issue

Hello!

Consider a case where you have an asynchronous factory function: for example you need to make a request or read some file from the filesystem before creating an instance of the required class.

Right now, TypeDI, if supplied with the async factory function, will add the promise object to the container instead of resolving it beforehand.

I think it’s a valid use case, but in order to implement it, we will probably need to change all container’s call signatures to async ones, which is a very drastic change in my opinion. However, this enables some very interesting functionality.

The alternative would be to just resolve those factory functions manually during the app initialization and register instances with the container, which could be non-optimal, because we don’t know if an instance some class will be actually used during program execution (sacrificing lazy-loading).

What do you think?


The manual approach looks like this:

  const dataServiceFactory = Container.get(DataServiceFactory);
  const dataService = await dataServiceFactory.create();

  Container.set({
    type: DataService,
    value: dataService
  });

Which is somewhat cumbersome and non-automatic.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:35
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

13reactions
StevenLangbroekcommented, Jan 20, 2020

Hey folks! I was wondering if this is under consideration? I think this is a very common usecase (e.g., database connections, oauth providers that open & maintain a connection), and they all need to be lifted outside of your IoC in order to be feasible.

8reactions
kgkgcommented, Dec 3, 2021

Maybe instead of changing get it would be much more user-friendly to add resolve method which will await for async factories to finish and always returns Promise?

Something like

const service = await Container.resolve(MyService);
Read more comments on GitHub >

github_iconTop Results From Across the Web

11 Asynchronous Programming 3.2.7 - The Grails Framework
The Promises class uses a grails.async.PromiseFactory instance to create Promise instances. The default implementation uses Project Reactor and is called org.
Read more >
Async Programming - Patterns for Asynchronous MVVM ...
The Lazy<T> layer provides thread-safe lazy initialization, ensuring the factory method is only executed once; the Task<T> layer provides asynchronous support, ...
Read more >
JavaScript Question: What is a Factory Function? - YouTube
We will also address the value of closure with factory functions. Would you like to help keep this channel going?
Read more >
Table of Contents - Micronaut Documentation
Beans defined with the @Factory annotation can now produce beans from public or package ... Async; @Singleton public class SampleEventListener { private ...
Read more >
Supporting Asynchronous Collaboration for Interactive ...
The first such system is sense.us, a web site supporting asynchronous collaboration ... observation and hypothesis, and the complementary roles of social ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found