1.2.1 • Published 6 years ago

@cocopina/singleton v1.2.1

Weekly downloads
1
License
ISC
Repository
github
Last release
6 years ago

Singleton npm.io

A truly global, isomorphoic, extendable JavaScript singleton class.

Table of Contents

Installation and Usage

Install the package:

npm i @cocopina/singleton

Extending the Singleton class

You can then extend your class to implement the Singleton class:

import Singleton from '@cocopina/singleton';

class MyClass extends Singleton {}

const a = MyClass.getInstance();
const b = MyClass.getInstance();

console.log(a === b); // true

Using the asSingleton decorator

You can alternatively use the asSingleton decorator like so:

import { asSingleton } from '@cocopina/singleton';

class MyClass {}

const MySingletonClass = asSingleton(MyClass);

const a = MySingletonClass.getInstance();
const b = MySingletonClass.getInstance();

console.log(a === b); // true

API

getInstance [Function]

Gets the singleton's instance.

If none exists, will create a new one and store it on the global scope.

@return {Object}

instance [Object]

Gets the singleton's instance, an alias for getInstance().

clear [Function]

Clears the singleton's instance from the global scope.

1.2.1

6 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.0.1

6 years ago