tm-singleton

    0.1.0 • Public • Published

    tm-singleton

    npm codecov

    Implements a Singleton design pattern with several extra control methods.

    Installation

    npm i tm-singleton

    Tests

    npm test

    Tests with coverage

    npm run test:coverage

    Options

    You can pass options object to the singleton factory function, as a second argument.

    const singleton = require('tm-singleton');
    
    const MyCounter = singletone(MySuperCounter, options);
    const counter = new MyCounter();

    Available options:

    • errorOnDuplicate Will throw an error if you try to create instance with new operator, available values: true/false, default is false.

    API.

    To create a new instance, just use a new operator.

    e.g

    const singleton = require('tm-singleton');
    
    const MyCounter = singletone(MySuperCounter);
    const counter = new MyCounter();

    To access an existing instance use INSTANCE getter. If instance hasn't been created it will return null.

    const singleton = require('tm-singleton');
    
    const MyCounter = singletone(MySuperCounter);
    
    ....
    
    const counter = MyCounter.INSTANCE;

    Events

    Singleton provides an event emitter. You can access it due eventEmitter getter.

    const singleton = require('tm-singleton');
    
    const MyCounter = singletone(MySuperCounter);
    
    ....
    
    MyCounter.eventEmitter.on('create', () => doSomething());

    Available events

    • create emits once after creating an instance.

    Example

    Create a module with a class which you want to make a singleton by wrapping it into a tm-singleton object:

    const singleton = require('tm-singleton');
    
    // Below is an example of the counter class
    // Which we are going to make a singleton.
    class MySuperCounter {
      constructor(start = 0) {
        this.value = start;
      }
    
      increase(amount = 1) {
        this.value += amount;
        return this.value;
      }
    }
    
    // Wrapping it into a singleton object.
    module.exports = singletone(MySuperCounter);

    Require it somewhere in the code and create an instance.

    const MySuperCounter = require('./mySuperCounter');
    
    const counter = new MySuperCounter(5);
    
    console.log(counter.increase(5)); // Will show 10;

    Require it another part of the code and create an instance.

    const MySuperCounter = require('./mySuperCounter');
    
    const counter = new MySuperCounter(); // or const counter = MySuperCounter.INSTANCE;
    
    console.log(counter.increase(5)); // Will show 15, since it is the same instance of your counter;

    Keywords

    Install

    npm i tm-singleton

    DownloadsWeekly Downloads

    1

    Version

    0.1.0

    License

    MIT

    Unpacked Size

    11.5 kB

    Total Files

    7

    Last publish

    Collaborators

    • romon2002