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

container.hasInstances() or some other way to tell if instances exist without creating new ones #127

Closed
mikl opened this issue Oct 13, 2020 · 4 comments · Fixed by #183 · May be fixed by #132
Closed

container.hasInstances() or some other way to tell if instances exist without creating new ones #127

mikl opened this issue Oct 13, 2020 · 4 comments · Fixed by #183 · May be fixed by #132
Assignees

Comments

@mikl
Copy link
Contributor

mikl commented Oct 13, 2020

I’ve been struggling to find a way to gracefully shut down my Node.js application after adopting TSyringe, since there appears to be no way to get existing instances without creating a new one.

Description

For example, I have a database client defined as

@singleton()
export class CassandraClient {
  // Implementation irrelevant
}

and a shutdown function (using the global container) looking like this:

  if (container.isRegistered(CassandraClient)) {
    for (const client of container.resolveAll(CassandraClient)) {
       client.shutdown())
    }
  }

…if no instances of the (singleton) CassandraClient exists, TSyringe instantiates a new one. Since I’m trying to shut down, that is counter-productive, and leads to problems trying to shut something down that hasn’t really started yet.

Solutions

I don’t really have a preference here, but I imagine one of these could solve the problem:

  1. A container.hasInstances() check that only returns true if a token has been instantiated, not just registered.
  2. A variant of (or parameter to) container.resolveAll() that only returns existing instances, instead of creating new ones.
@Xapphire13
Copy link
Collaborator

Xapphire13 commented Oct 15, 2020

Could this be solved by checking to see if the client has been started before calling .shutdown()?

@mikl
Copy link
Contributor Author

mikl commented Oct 15, 2020

@Xapphire13 just instantiating the class starts it. I even tried subclassing the container to try to get at the existing instances without creating new ones, but since everything has private visibility, that’s not an option either :(

Another option would be to make some of the internal methods protected instead, so it would be possible to customise the container.

@Xapphire13
Copy link
Collaborator

@mikl, would you problem be solved by adding a .dispose() method to the container? This method would dispose all instances that implement:

interface Disposable {
  dispose(): void;
}

and render the container unusable

@mikl
Copy link
Contributor Author

mikl commented Oct 18, 2020

@Xapphire13 yes, that would be a great solution, imho.

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