Skip to content
This repository has been archived by the owner on Jan 17, 2019. It is now read-only.

Add support for async FactoryProvider bindings #6

Closed
marcus-sa opened this issue Aug 24, 2018 · 0 comments
Closed

Add support for async FactoryProvider bindings #6

marcus-sa opened this issue Aug 24, 2018 · 0 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@marcus-sa
Copy link
Owner

marcus-sa commented Aug 24, 2018

Due to the limitations with Inversify it is currently not possible to bind an async function, let it resolve upon injecting and then return the actual provider.
Currently it's done like this

async bindFactoryProvider(provider: FactoryProvider) {
  const deps = await this.getDependencies(provider.deps);

  if (provider.scope === SCOPES.TRANSIENT) {
    return this.module.providers
      .bind(provider.provide)
      .toDynamicValue(() => provider.useFactory(...deps));
  }

  this.module.providers
    .bind(provider.provide)
    .toProvider(() => provider.useFactory(...deps));
}

But it should be done like this

bindFactoryProvider(provider: FactoryProvider) {
  const resolveBinding = async () => {
    const deps = await this.getDependencies(provider.deps);
    return await provider.useFactory(...deps);
  };


  if (provider.scope === SCOPES.TRANSIENT) {
    return this.module.providers
      .bind(provider.provide)
      .toDynamicValue(() => resolveBinding());
  }

  this.module.providers
    .bind(provider.provide)
    .toProvider(() => resolveBinding());
}

That'll let me wait with resolving provider dependencies until the actual provider is being injected somewhere.
Either we'll have to fork Inversify or create a PR so this can be fixed.

@marcus-sa marcus-sa added enhancement New feature or request help wanted Extra attention is needed labels Aug 24, 2018
marcus-sa pushed a commit that referenced this issue Sep 27, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant