Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

di-scoped

Package Overview
Dependencies
Maintainers
0
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

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.

  • 1.0.8
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
377
Maintainers
0
Weekly downloads
 
Created
Source

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, 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

Usage

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<typeof ScopedService>;

export default ScopedService;

The Context Scoped services for NodeJS

  1. The constructor arguments are moved to the context creation scope
// TypeScript

import { 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');
  1. The instance reference is similar independent to the context
// TypeScript

let 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

Keywords

FAQs

Package last updated on 21 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc