1.2.2 • Published 4 years ago

singleton-wrapper v1.2.2

Weekly downloads
2
License
ISC
Repository
github
Last release
4 years ago

singleton-wrapper

To enable JS programmers to implement the singleton pattern in their application at ease.

Suppose there is a class written that is used in multiple places and has an object instantiated in multiple places using the constructor.

If you want to have only one instance of the class in the entire application with minimal changes, then this package helps you without modifying class and the use of a constructor to instantiate the object.

steps to implement

install using command

npm i singleton-wrapper

once installation completed just import the Singleton class and export default the wrapper class as below.

import Singleton from 'singleton-wrapper';

export default class {
    constructor() {
        return new Singleton(MyClass);
    }
}
class MyClass {
    constuctor(){
    }
}

parameterized constructor

If you have parameterized constructor, then pass the parameter as an collected object, as below:

import Singleton from 'singleton-wrapper';

export default class {
    constructor(arg1, arg2, ...) {
        return new Singleton(MyClass, [arg1, arg2, ...]);
    }
}
class MyClass {
    constuctor(arg1, ag2, ....){
    }
}

It makes sure MyClass will have only one object in the application.

1.2.0

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago