This is a tiny library to create and manage singleton instances.
Table of Contents
npm install singa --save
The singa function returns an object of type Singa, with all input parameters, including name and factory, being entirely optional.
import { singa } from 'singa';
class Foo {
}
const singleton = singa({
name: 'singleton',
factory() {
return new Foo();
},
});
const instance = singleton.use();
declare type Singa<T> = {
/**
* Create or us existing singleton instance.
*/
use: () => T,
/**
* Set the singleton instance.
*
* @param instance
*/
set: (instance: T) => void,
/**
* Set factory fn for instance creation.
*
* @param factory
*/
setFactory: (factory: Factory<T>) => void,
/**
* Reset the singleton instance.
*/
reset: () => void,
/**
* Check if the singleton instance is set.
*/
has: () => boolean,
/**
* Check if a factory fn is set.
*/
hasFactory: () => boolean
};
Before starting to work on a pull request, it is important to review the guidelines for contributing and the code of conduct. These guidelines will help to ensure that contributions are made effectively and are accepted.
Made with 💚
Published under MIT License.