Repository navigation
refactor(tsyringe): remove dependency - #1
Open
jeremycare wants to merge 5 commits into
Open
jeremycare wants to merge 5 commits into
jeremycare wants to merge 5 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Replace tsyringe with a Map-based registry
Summary
The container only used a small, non-decorator slice of tsyringe:
isRegistered,register,resolve,reset, plusLifecycle.SingletonandinstanceCachingFactory. All of that reduces to "look up by token, instantiate once, cache". This PR replaces it with a singleMapkeyed on theTokeninstance itself, and drops both runtime dependencies.The package now has zero runtime dependencies.
What changed
container.ts: a module-levelMap<Token, Entry>withhas,set,resolve, andclear. Every provider kind (useValue,useClass,useFactory) resolves lazily on firstinjectand is cached afterwards, matching the previous singleton and caching-factory behavior.Token.ts: no longer allocates aSymbol. The token object is the registry key, so identity and uniqueness come from the instance. Anamefield replaces the symbol description for error messages, and a phantom__typefield keepsToken<A>andToken<B>non-interchangeable at the type level.register.ts,inject.ts,reset.ts: thin wrappers over the new module. Public signatures and JSDoc are unchanged.index.ts: removes thereflect-metadataside-effect import.package.json: removestsyringeandreflect-metadata, drops thetsyringekeyword, updates the description.tsconfig.json: removesexperimentalDecoratorsandemitDecoratorMetadata, which nothing used.README.md: no longer describes the package as a Tsyringe wrapper.di-container.test.ts: adds coverage for the behavior previously delegated to tsyringe (see Testing).Why
ClassProvidertype isnew () => T, so constructor injection was never possible through this API. The README already recommendsinject()as a property initializer instead of constructors. Decorator metadata andreflect-metadatawere dead weight.reflect-metadataglobal polyfill side effect.Breaking changes
Token.symbolis removed. UseToken.namefor a readable label.reflect-metadata. Consumers who relied on this package to polyfillReflect.metadatafor their own decorators must import it themselves.Attempted to resolve unregistered token <name>.instead of tsyringe's wording.This warrants a major version bump.
Testing
tsc --noEmit: no errorsjest: 15 passed, 0 failed (8 existing, 7 new)useClassanduseFactoryproviders resolve exactly once and return the same instanceinjectinjectand yield to explicit registrationsToken<A>is not assignable toToken<B>, enforced via@ts-expect-errorunder ts-jest