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

instanceCachingFactory not cleared on clearInstances #211

Open
vitorvanacor opened this issue Sep 26, 2022 · 0 comments
Open

instanceCachingFactory not cleared on clearInstances #211

vitorvanacor opened this issue Sep 26, 2022 · 0 comments
Assignees

Comments

@vitorvanacor
Copy link

vitorvanacor commented Sep 26, 2022

Describe the bug
Calling container.clearInstances() does not clear instances created by instanceCachingFactory. If this is not a bug, but by design, what is the correct way of achieving this behaviour?

To Reproduce

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

function main() {
  container.register('port', {
    useFactory: instanceCachingFactory(() => process.env.PORT),
  })
  process.env.PORT = '1234'
  assert(container.resolve('port') === '1234')
  process.env.PORT = '5678'
  // Still resolves to '1234' (OK)
  assert(container.resolve('port') === '1234')
  container.clearInstances()
  // Should run factory again and resolve to '5678'
  assert(container.resolve('port') === '5678') // Error! It is still resolving to '1234'
}

main()

Expected behavior
After clearInstances, the instanceCachingFactory should run again - at least this is what I expect since it is what happens with @singleton, registerSingleton and { lifecycle: Lifecycle.Singleton } and the README says that instanceCachingFactory is very similar to singleton.
johan13 has created a good alternative here: #140 (comment)
But shouldn't this be supported by the library?

Version: 4.6.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