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

feat(dispose): dispose child injectors #29

Merged
merged 2 commits into from Aug 12, 2020
Merged

Conversation

nicojs
Copy link
Owner

@nicojs nicojs commented Aug 12, 2020

This changes the way dispose works. Instead of an Injector disposing it's own provided dependencies and then call dispose on the parent, it will dispose its child Injector's (that were created using provideXXX) and then it's own dependencies. The parent Injector will no longer be disposed of.

This allows you to dispose from the rootInjector down:

class Foo {
  dispose() {
    console.log('Foo disposed');
  }
}
const rootInjector = createInjector();
const fooProvider = rootInjector.provideClass('foo', Foo);
fooProvider.resolve('foo');
await rootInjector.dispose(); // => "Foo disposed"
rootInjector.provideClass('bar', Bar); // Error: Injector already disposed

Because now an Injector has to keep track of its child injectors, the rootInjector would no longer be stateless. That's why it is removed altogether. Developers are expected to create their own (root) injector using createInjector().

BREAKING CHANGE: rootInjector is removed in favor of createInjector.

This:

import { rootInjector } from 'typed-inject';

Becomes:

import { createInjector } from 'typed-inject';
const rootInjector = createInjector();

Injector's created from createInjector are no longer stateless. They
keep track of their child injectors.

BREAKING CHANGE: dispose no longer disposes parent injector, disposes
the child injectors instead. See readme for more details.

Fixes #24

This changes the way `dispose` works. Instead of an `Injector` disposing it's own provided dependencies and then call `dispose` on the parent, it will dipose it's child `Injector`'s (that were created using `provideXXX`) and then it's own dependencies. The parent `Injector` will _no longer_ be disposed.

This allows you to dispose from the rootInjector down:

```ts
class Foo {
  dispose() {
    console.log('Foo disposed');
  }
}
const rootInjector = createInjector();
const fooProvider = rootInjector.provideClass('foo', Foo);
fooProvider.resolve('foo');
await rootInjector.dispose(); // => "Foo disposed"
rootInjector.provideClass('bar', Bar); // Error: Injector already disposed
```

Because now an `Injector` has to keep track of it's child injectors, the `rootInjector` would no longer be stateless. That's why it is removed all together. Developers are expected to create their own rootInjector using `createInjector()`.

BREAKING CHANGE: `rootInjector` is removed in favor of `createInjector`.

This:

```
import { rootInjector } from 'typed-inject';
```

Becomes:

```
import { createInjector } from 'typed-inject';
const rootInjector = createInjector();
```

Injector's created from `createInjector` are no longer stateless. They
keep track of their child injectors.

BREAKING CHANGE: `dispose` no longer disposes parent injector, disposes
the child injectors instead. See readme for more details.
@nicojs nicojs merged commit 3f0f3f5 into master Aug 12, 2020
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.

Add a way to dispose all child injectors
1 participant