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

Use injected() function with AsyncFactory? #41

Open
JSchaenzle opened this issue Dec 12, 2023 · 0 comments
Open

Use injected() function with AsyncFactory? #41

JSchaenzle opened this issue Dec 12, 2023 · 0 comments

Comments

@JSchaenzle
Copy link

I have a singleton class that is being injected into other classes using the injected(...) syntax.
The class is called Exchange.
I need to add an async initializer to that class. (want to set up a websocket before the class is used)
I'm trying to do so by creating a Factory but I'm running into a roadblock. Can I still used injected() when using an async Factory?

I have tokens defined as follows:

    exchange: token<Exchange>('Exchange'),
    exchangeFactory: token<AsyncFactory<Exchange>>('Exchange Factory'),

and container bindings set up like this,

    .bind(TOKENS.exchange)
    .toInstance(ExchangeAlpaca)
    .inSingletonScope();

container
    .bind(TOKENS.exchangeFactory)
    .toFactory(async () => {
        let e = container.get(TOKENS.exchange);
        await e.init();
        return e;
    });
class SomeClass {
  constructor(private e: Exchange){}
}
injected(SomeClass, TOKENS.exchangeFactory.optional) // <- this doesn't work
injected(SomeClass, TOKENS.exchange.optional) // <- this works but factory doesn't get used.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant