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

Allow toService to be bound to async service #1441

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

AGrzes
Copy link

@AGrzes AGrzes commented Feb 18, 2022

Description

toService method:

 public toService(service: string | symbol | interfaces.Newable<T> | interfaces.Abstract<T>): void {
    this.toDynamicValue(
      (context) => context.container.get<T>(service)
    );
  }

Was using get method that throws exception when it would return promise. It caused prevented resolution of service using getAsync method.
getAsync could not be used instead because it always return promise even when resolution returns simple value (async keyword on function have that effect)
Removal of async keyword would change getAsync result - it would work for clients using await because one can await simple value but it would break for clients calling then/catch because simple values does not have them.
New function getMaybeAsync was introduced instead to Container interface returning promise when resolved to promise and simple value when resolved to simple value.

If introduction of new interface method is not desired then I see another way to approach problem - by passing if resolution is sync/async through Context.

Related Issue

#1409

Motivation and Context

toService binding were using Container.get method internally so it was throwing exception when the binding target was promise even if the resolution was also asynchroneous (getAsync)

How Has This Been Tested?

Added new automated test case.
All old test cases are passing.

Types of changes

  • Updated docs / Refactor code / Added a tests case (non-breaking change)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • I have updated the changelog.

I will update changelog/documentation if general shape of PR is acceptable.

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

Successfully merging this pull request may close these issues.

None yet

2 participants