Skip to content

A truly global, isomorphoic, JavaScript singleton class

Notifications You must be signed in to change notification settings

sahariko/singleton

Repository files navigation

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.

About

A truly global, isomorphoic, JavaScript singleton class

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published