https://github.com/aigoncharov/singleton
Singleton decorator. No constructor monkeypatching. Zero dependencies. Built with TypeScript.
https://github.com/aigoncharov/singleton
decorator singleton typescript
Last synced: 11 months ago
JSON representation
Singleton decorator. No constructor monkeypatching. Zero dependencies. Built with TypeScript.
- Host: GitHub
- URL: https://github.com/aigoncharov/singleton
- Owner: aigoncharov
- License: mit
- Created: 2019-03-30T10:24:14.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-03-30T11:33:54.000Z (almost 7 years ago)
- Last Synced: 2024-10-12T02:53:41.762Z (over 1 year ago)
- Topics: decorator, singleton, typescript
- Language: TypeScript
- Homepage:
- Size: 62.5 KB
- Stars: 27
- Watchers: 1
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# singleton [](https://travis-ci.org/keenondrums/singleton) [](https://coveralls.io/github/keenondrums/singleton?branch=master) [](https://twitter.com/intent/tweet?text=Singleton.%20No%20constructor%20monkeypatching.%20Zero%20dependencies.%20Built%20with%20TypeScript.&url=https://github.com/keenondrums/singleton&hashtags=javascript,typescript,singleton,decorator)
Singleton decorator. No constructor monkeypatching. Zero dependencies. Built with TypeScript.
- [Installation](#installation)
- [Quick start](#quick-start)
- [Usage without decorators](#usage-without-decorators)
- [Inheritance](#inheritance)
- [In depth](#in-depth)
## Installation
1. Run
```sh
npm i @keenondrums/singleton
```
1. **(Optional)** Enable decorators
1. If you use TypeScript set in you tsconfig.json
```json
{
"compilerOptions": {
"experimentalDecorators": true
}
}
```
1. If you use JavaScript configure your babel to support decorators and class properties
## Quick start
```ts
import { singleton } from '@keenondrums/singleton'
@singleton
class Test {}
new Test() === new Test() // returns `true`
```
## Usage without decorators
```ts
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.
```ts
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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) and a [construct trap](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/handler/construct) to override class' creation logic.
Your singleton instance is always available as a static property of a class by key `SINGLETON_KEY`.
```ts
import { singleton, SINGLETON_KEY } from '@keenondrums/singleton'
@singleton
class Test {}
const instance = new Test()
Test[SINGLETON_KEY] === instance // returns `true`
```