microsoft / microsoft/tsyringe
Disposing container does not dispose singleton resolved from child container
Open
@Xapphire13 is already working on this.
Since Mar 22, 2024.
awaiting triage
- Dominant language
- TypeScript
- Stars
- 6k
- Forks
- 184
- Avg merge
- 3m
- Merged PRs (30d)
- 1
Description
Describe the bug
Disposing container does not dispose singleton resolved from child container.
See reproduction for details:
To Reproduce
import "reflect-metadata";
import {
container,
Disposable,
injectable,
Lifecycle,
scoped,
singleton,
} from "tsyringe";
@singleton()
@injectable()
class Foo implements Disposable {
constructor() {
console.log("making foo!");
}
dispose(): void | Promise<void> {
console.log("disposing foo!");
}
}
@scoped(Lifecycle.ContainerScoped)
@injectable()
class Bar {
constructor(private readonly foo: Foo) {
console.log("making bar!");
}
}
const childA = container.createChildContainer();
const childB = container.createChildContainer();
/**
* making foo!
* making bar!
* making bar!
*/
childA.resolve(Bar);
childB.resolve(Bar);
/**
* no output, container already has a Foo
*/
container.resolve(Foo);
/**
* no output, is not disposing of Foo...
*/
await container.dispose();
/**
* disposing foo!
*/
await childA.dispose();
Expected behavior
I expect singletons to always execute their dispose function even when calling dispose on a container which wasn't used when they where first resolved.
i.e. I expect the same behavior of first resolving the singleton from the root container and then resolving it from child containers:
import {
container,
Disposable,
injectable,
Lifecycle,
scoped,
singleton,
} from "tsyringe";
@singleton()
@injectable()
class Foo implements Disposable {
constructor() {
console.log("making foo!");
}
dispose(): void | Promise<void> {
console.log("disposing foo!");
}
}
@scoped(Lifecycle.ContainerScoped)
@injectable()
class Bar {
constructor(private readonly foo: Foo) {
console.log("making bar!");
}
}
/**
* making foo!
*/
container.resolve(Foo);
const childA = container.createChildContainer();
const childB = container.createChildContainer();
/**
* making bar!
* making bar!
*/
childA.resolve(Bar);
childB.resolve(Bar);
/**
* disposing foo!
*/
await container.dispose();
Version: 4.8.0
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.