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 @postConstruct/onActivation + @preDestroy/onDeactivation #1086

Closed
wants to merge 41 commits into from

Conversation

parisholley
Copy link
Contributor

@parisholley parisholley commented May 23, 2019

For anyone wanting to play around with this, you can use the NPM release:

@parisholley/inversify-async:1.0.20

In continuation of #1074, allowing async hooks into dependency lifecycles.

Description

Global onActivation

container.onActivation('asyncIdentifier', (context, injectable) => {
  return Promise.resolve('newObject'); // returning a promise enforces use of getAsync(), even if future binds are not async
});
container.bind('asyncIdentifier').toConstantValue('foobar');

await container.getAsync('asyncIdentifier');

container.onActivation('syncIdentifier', (context, injectable) => {
  return 'newObject'; // no promise = use legacy API
});

container.get('syncIdentifier');

Binding onActivation

container.bind('asyncIdentifier').toConstantValue('foobar').onActivation((context, injectable) => {
  return Promise.resolve('newObject'); // returning a promise enforces use of getAsync()
});

await container.getAsync('asyncIdentifier');

container.bind('asyncIdentifier').toConstantValue('foobar').onActivation((context, injectable) => {
  return 'newObject'; // no promise = use legacy API
});

container.get('syncIdentifier');

Parent/Child onActivation

container.onActivation('asyncIdentifier', (context, injectable) => {
  return Promise.resolve('newObject'); // returning a promise enforces use of getAsync()
});

const child = container.createChild();
child.bind('asyncIdentifier').toConstantValue('foobar');

await container.getAsync('asyncIdentifier');

Module onActivation

container.bind('asyncIdentifier').toConstantValue('foobar');

const TestContainerModule = new ContainerModule((bind, unbind, isBound, rebind, onActivation) => {
  onActivation('asyncIdentifier', async (context, service) => {

 }));
});

Related Issue

#551
#1074
#471

Motivation and Context

For testing (or perhaps a custom request based framework where child containers are created for each web request to implement "Request Scope" similar to spring), it may be useful to have async construct/destroy methods. For example, a web request where we want to run all service calls in a transaction and commit on at the end (by unbinding the container), or in tests by rolling back database changes for more performant runs.

How Has This Been Tested?

Similar to test cases for #1074, ensure that errors are thrown when mixing sync and async APIs.

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.

…c/async, prevent onActivation from firing until lazy value is available
…e than once (even though it is a singleton). removed support for onActivation and context access in order to keep internal code simple. in theory, the async call can happen at any time (or in-side multiple contexts), so conceptually we should not support access the current context
…sync support for @PostConstruct, unbind/unbindAll async APIs, onDeactivation support
…apshotting, support traversing container tree for onActivations, fix onACtivation when depedency is Lazy, updated tests to use approriate equals handler
@parisholley
Copy link
Contributor Author

closing and continuing with #1132

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.

1 participant