New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

@keenondrums/singleton

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@keenondrums/singleton

Singleton. No constructor monkeypatching. Zero dependencies. Built with TypeScript.

latest
Source
npmnpm
Version
1.0.0
Version published
Weekly downloads
234
50.97%
Maintainers
1
Weekly downloads
 
Created
Source
  • singleton

singleton

Singleton. No constructor monkeypatching. Zero dependencies. Built with TypeScript.

Installation

  • Run

    npm i @keenondrums/singleton
    
  • (Optional) Enable decorators

    • If you use TypeScript set in you tsconfig.json

      {
        "compilerOptions": {
          "experimentalDecorators": true
        }
      }
      
    • If you use JavaScript configure your babel to support decorators and class properties

Quick start

import { singleton } from '@keenondrums/singleton'

@singleton
class Test {}

new Test() === new Test() // returns `true`

Usage without decorators

import { singleton } from '@keenondrums/singleton'

class Test {}
const TestSingleton = singleton(Test)

new TestSingleton() === new TestSingleton() // returns `true`

Inheritance

Any child of your singleton will not be a singleton.

import { singleton } from '@keenondrums/singleton'

@singleton
class Parent {}

class Child extends Parent {}

new Child() === new Child() // returns `false`

// If you want to make `Child` a singleton as well, apply `singleton` decorator directly to it
@singleton
class ChildSingleton extends Parent {}

new ChildSingleton() === new ChildSingleton() // returns `true`

In depth

singleton decorator wraps your class with a Proxy and a construct trap to override class' creation logic.

Your singleton instance is always available as a static property of a class by key SINGLETON_KEY.

import { singleton, SINGLETON_KEY } from '@keenondrums/singleton'

@singleton
class Test {}

const instance = new Test()
Test[SINGLETON_KEY] === instance // returns `true`

Keywords

singleton

FAQs

Package last updated on 30 Mar 2019

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