@dmitriyzverev/locator
v1.0.0
Published
_Simple implementation of Service Locator pattern with singletons and lazy loading_
Downloads
3
Readme
@dmitriyzverev/locator
Simple implementation of Service Locator pattern with singletons and lazy loading
Usage
Install dependencies:
npm i -SE @dmitriyzverev/locator
Create locator and define your services inside:
import {createLocator} from '@dmitriyzverev/locator'; const locator = createLocator({ services: { get user() { return { name: 'Bob', }; }, get helloMessage() { return `Hello, ${locator.user.name}!`; }, }, });
Now you can use any service defined in locator:
console.log(locator.helloMessage); // Hello, Bob!
By default, each service is a singleton. It will be created only once, and only if requested. If you need to create a new service instance for each service request, disable the service singleton in the
singletons
property:import {createLocator} from '@dmitriyzverev/locator'; const locator = createLocator({ services: { get user() { return { name: 'Bob', }; }, }, singletons: { user: false, }, }); console.log(locator.user === locator.user); // false