Socket
Book a DemoInstallSign in
Socket

@cocopina/singleton

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@cocopina/singleton

A truly global, isomorphoic, JavaScript singleton class

latest
Source
npmnpm
Version
1.2.1
Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

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.

Keywords

singleton

FAQs

Package last updated on 14 Nov 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts