@cocopina/singleton

1.2.1 • Public • Published

Singleton

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.

Readme

Keywords

Package Sidebar

Install

npm i @cocopina/singleton

Weekly Downloads

1

Version

1.2.1

License

ISC

Unpacked Size

33 kB

Total Files

22

Last publish

Collaborators

  • cocopina