microsoft / microsoft/tsyringe

container.clearInstances() should not clear ValueProviders

Open
#121 4 comments 4 reactions 1 assignee View on GitHub

@Xapphire13 is already working on this.

Since Aug 15, 2020.

Dominant language
TypeScript
Stars
6k
Forks
184
Avg merge
3m
Merged PRs (30d)
1

Description

Describe the bug

One can register dependencies as ValueProvider via:

container.register(TOKEN, {useValue: myImpl});

Unfortunately container.clearInstances() unregisters dependencies registered via ValueProvider, which makes it impossible to resolve TOKEN after calling container.clearInstances().

To Reproduce

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

const DEP_TOKEN = Symbol("DEP");

const myDepImpl = {
  doStuff() {
    console.log("foo");
  },
} as const;

container.register(DEP_TOKEN, { useValue: myDepImpl });

container.resolve<typeof myDepImpl>(DEP_TOKEN).doStuff(); // prints "foo"

container.clearInstances();

// errors with: Error: Attempted to resolve unregistered dependency token: "Symbol(DEP)"
container.resolve<typeof myDepImpl>(DEP_TOKEN).doStuff();

Expected behavior
I would expect container.clearInstances() to only clear instances that were created via a ClassProvider or FactoryProvider but not via ValueProvider as it makes it otherwise impossible / impractical to use .clearInstances() in conjunction with ValueProviders.

Version:

4.3.0

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.