singleton-injection
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

Singleton Injection

A simple way to manage singletons

This provides a way to create Singleton Container to store Singleton instances

Usage

// container.ts
import { SingletonContainer } from "singleton-injection";

// Define your singletons
const singletonMap = {
    shape: () => new Circle() as Shape,
    otherShape: () => new Square() as Shape
};

// Create Singleton container
export const container = new SingletonContainer(singletonMap);

// app.ts
// Get instance in your app with
import { container } from "./container";

// now you can get same instance of shape anywhere in your code
const shape = container.resolve("shape");
// you will get TS suggestions for parameter of `resolve`, so no need to worry about spelling mistakes

// Destroy container instances
container.destroy();
// after destroy, container.resolve will create and return new instances

Mocking in Tests

container.mock({
    shape: () => new MockedSquare() as Shape
});
// With above all `container.resolve("shape")` calls will return this mocked shape
// Note: mocked instances are not singletons, it will create instance everytime

// To restore mock, you can use same method with empty object, this will restore the original container
container.mock({});

Package Sidebar

Install

npm i singleton-injection

Weekly Downloads

1

Version

2.0.0

License

MIT

Unpacked Size

14.4 kB

Total Files

13

Last publish

Collaborators

  • rohilaharsh