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

Disposing container does not dispose singleton resolved from child container #237

Open
eduardvercaemer opened this issue Mar 22, 2024 · 0 comments
Assignees

Comments

@eduardvercaemer
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants