New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

tiny-sl

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tiny-sl

ServiceLocator is a simple dependency injection container for TypeScript.

latest
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

Service Locator

ServiceLocator is a simple dependency injection container for TypeScript.

Installation

You can install ServiceLocator via npm:

npm install tiny-sl

Usage

import ServiceLocator from "@types/service-locator";

class MyService {
  doSomething() {
    console.log("doing something...");
  }

  dispose() {
    console.log("disposing...");
  }
}

let sl = ServiceLocator.instance;

sl.registerSingleton(MyService, new MyService());

let myService = sl.get(MyService);
myService.doSomething(); // doing something...

sl.unregister(MyService); // disposing...
// (or)
// sl.reset(); // disposing...

API

ServiceLocator

instance: ServiceLocator

Returns the instance of the ServiceLocator.

get<T>(type: new () => T): T

Get the instance of a registered service.

  • type: The type of the service.
  • Returns: The instance of the service.
  • Throws: Error if the service registration is not found.

registerSingleton<T>(type: new () => T, service: T): void

Register a singleton service.

  • type: The type of the service.
  • service: The instance of the service.
  • Throws: Error if the service is already registered.

registerLazySingleton<T>(type: new () => T, factory: () => T): void

Register a lazy singleton service.

  • type: The type of the service.
  • factory: The factory function to create the service instance.
  • Throws: Error if the service is already registered.

registerFactory<T>(type: new () => T, factory: () => T): void

Register a factory service.

  • type: The type of the service.
  • factory: The factory function to create the service instance.
  • Throws: Error if the service is already registered.

unregister<T>(type: new () => T): void

Unregister a service.

  • type: The type of the service.
  • Throws: Error if the service registration is not found.

reset(params?: { dispose: boolean }): void

Reset the ServiceLocator.

  • params.dispose: Whether to dispose services before resetting. Default is true.

resetLazySingleton<T>(type: new () => T): void

Reset a lazy singleton service.

  • type: The type of the service.
  • Throws: Error if the service registration is not found.

License

This package is licensed under the MIT License.

FAQs

Package last updated on 17 Mar 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