https://github.com/tripolskypetr/di-scoped
A context-based instantiation system using async_hooks, ensuring each class instance is scoped to a unique execution context, with automatic creation and reuse of different instances using the same reference across multiple contexts.
https://github.com/tripolskypetr/di-scoped
aspnet-core dependency-injection nodejs scoped
Last synced: 7 months ago
JSON representation
A context-based instantiation system using async_hooks, ensuring each class instance is scoped to a unique execution context, with automatic creation and reuse of different instances using the same reference across multiple contexts.
- Host: GitHub
- URL: https://github.com/tripolskypetr/di-scoped
- Owner: tripolskypetr
- License: mit
- Created: 2024-11-14T13:45:46.000Z (11 months ago)
- Default Branch: master
- Last Pushed: 2025-03-09T19:26:30.000Z (7 months ago)
- Last Synced: 2025-03-09T20:25:15.443Z (7 months ago)
- Topics: aspnet-core, dependency-injection, nodejs, scoped
- Language: TypeScript
- Homepage: https://github.com/react-declarative/react-declarative
- Size: 65.4 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# di-scoped
> A context-based instantiation system using `async_hooks`, ensuring each class instance is scoped to a unique execution context, with automatic creation and reuse of different instances using the same reference across multiple contexts.
This works the same like [Scoped ASP.Net Core services](https://henriquesd.medium.com/dependency-injection-and-service-lifetimes-in-net-core-ab9189349420), aka `These services are created once per HTTP request and are tied to the lifetime of the request (i.e., the HttpContext).`
Made for usage with [di-kit package](https://npmjs.com/package/di-kit)
## Usage
```tsx
import { scoped } from 'di-scoped';export const ScopedService = scoped(class {
constructor(public jwt: string) { }
setJwt = (jwt: string) => {
this.jwt = jwt;
}getJwt = () => {
return this.jwt;
};});
export type TScopedService = InstanceType;
export default ScopedService;
```
## The Context Scoped services for NodeJS
1. **The constructor arguments are moved to the context creation scope**
```tsx
// TypeScriptimport { scoped } from 'di-scoped';
const TestClass = scoped(class {
constructor(private name: string) {
}test() {
console.log(`Hello, ${this.name}`);
}
});TestClass.runInContext(() => {
new TestClass().test(); // Hello, Peter
}, "Peter")
new TestClass().test(); // ScopeContextError('di-scoped ContextReferer not running in context');
```2. **The instance reference is similar independent to the context**
```tsx
// TypeScriptlet instanceRef1;
let instanceRef2;TestClass.runInContext(() => {
instanceRef1 = new TestClass()
instanceRef1.test() // Hello, Peter}, "Peter")
TestClass.runInContext(() => {
instanceRef2 = new TestClass()
instanceRef2.test() // Hello, not Peter}, "not Peter")
if (TestClass === TestClass) {
console.log("Ok! This is the same class")
}if (instanceRef1 === instanceRef2) {
console.log("OMG! This is the same instance")
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
}
```## See also
If you looking for integrated DI container for scoped services instantiation, take a look on [di-kit npm package](https://www.npmjs.com/package/di-kit)