Skip to content

Disposing container doesn't dispose instances created/registered with useFactory and useValue providers #274

@stalniy

Description

@stalniy

Describe the bug

Disposing container doesn't dispose instances created/registered with useFactory and useValue providers.

To Reproduce

import "reflect-metadata";
import { container } from 'tsyringe';

const APP_INIT = Symbol('APP_INIT')

container.register(APP_INIT, {
  useFactory: () => ({
     dispose: () => console.log('disposed from useFactory')
  })
});

container.register(APP_INIT, {
  useValue: {
     dispose: () => console.log('disposed from useValue')
  }
});

container.register(APP_INIT, {
  useClass: class {
    dispose() {
      console.log('disposed from useClass')
    }
  }
});

console.log(container.resolveAll(APP_INIT))
Promise.resolve(container.dispose()).then(() => console.log('container disposed'))

the output is:

[
  { dispose: [Function: dispose] },
  { dispose: [Function: dispose] },
  useClass {}
]
disposed from useClass
container disposed

Expected behavior

Documentation says

All instances created by the container that implement the Disposable interface will automatically be disposed of when the container is disposed.

So, I expect that all services that have .dispose method are disposed. At the bare minimum there should be a possibility to dispose factories and values. We use factories to provide APP_INITIALIZERs that can connect to db or remote socket and need to close this connections on shutdown

Version: 4.10.0

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions