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

@furystack/inject

Package Overview
Dependencies
Maintainers
1
Versions
151
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@furystack/inject

Core FuryStack package

  • 4.0.41
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
18
decreased by-50%
Maintainers
1
Weekly downloads
 
Created
Source

@furystack/inject

dependency injection provider for FuryStack

Injector

Injectors act as containers, they are responsible for creating / retrieving service instances based on the provided Injectable metadata. You can create an injector with simply instantiating the class

const myInjector = new Injector()

You can organize your injector(s) in trees by creating child injectors. You can use the children and services with scoped lifetime for contextual services.

const childInjector = myInjector.createChild({ owner: 'myCustomContext' })

Injectable

Creating an Injectable service from a class

You can create an injectable service from a plain class when decorating with the @Injectable() decorator.

@Injectable({
  /** Injectable options */
})
export class MySercive {
  /** ...service implementation... */

  constructor(s1: OtherInjectableService, s2: AnotherInjectableService) {}
}

The constructor parameters (s1: OtherInjectableService and s2: AnotherInjectableService) should be also decorated and will be resolved recursively.

Lifetime

You can define a specific Lifetime for Injectable services on the decorator

@Injectable({
  lifetime: 'transient',
})
export class MySercive {
  /** ...service implementation... */
}

The lifetime can be

  • transient - A new instance will be created each time when you get an instance
  • scoped - A new instance will be created if it doesn't exist on the current scope. Can be useful for injectable services that can be used for contextual data.
  • singleton - A new instance will be created only if it doesn't exists on the root injector. It will act as a singleton in other cases.

Injectables can only depend on services with longer lifetime, e.g. a transient can depend on a singleton, but inversing it will throw an error

Retrieving your service from the injector

You can retrieve a service by calling

const service = myInjector.getInstance(MySercive)

Explicit instance setup

There are cases that you have to set a service instance explicitly. You can do that in the following way

class MyService {
  constructor(public readonly foo: string)
}

myInjector.setExplicitInstance(new MyService('bar'))

Keywords

FAQs

Package last updated on 20 Nov 2020

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