Code Monkey home page Code Monkey logo

singleton's Introduction

singleton Build Status Coverage Status Tweet

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

Installation

  1. Run

    npm i @keenondrums/singleton
  2. (Optional) Enable decorators

    1. If you use TypeScript set in you tsconfig.json

      {
        "compilerOptions": {
          "experimentalDecorators": true
        }
      }
    2. 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`

singleton's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

singleton's Issues

Problem with subclasses

A problem with this implementation of singleton for sub-classes is that it depends on order of instantiation objects - if you first instantiate parent class and then the sub-class they share the same singleton object of parent class. For instance, taking the example from your README:

new Parent() === new ChildSingleton()

returns true, which is the problem, because I would expect that new ChildSingleton() is a different object that new Parent().

But:

new ChildSingleton() === new Parent()

returns false as expected.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.