Skip to content
This repository has been archived by the owner on Apr 24, 2020. It is now read-only.

Latest commit

 

History

History
43 lines (32 loc) · 892 Bytes

FEATURE-SINGLETON.md

File metadata and controls

43 lines (32 loc) · 892 Bytes

Singleton

About

This is a decorator @Singleton() to make you class single. Compatible with most browsers, even old ones

Usage

import { Singleton } from '@taipescripeto/basic';

@Singleton()
class SomeBusinessService {

  someBusinessRule(a: number, b: number): boolean {
    return a < b;
  }
}

//  this return true
new SomeBusinessService() === new SomeBusinessService();

If you like an old school singleton implementation, you can do that:

import { Singleton } from '@taipescripeto/basic';

@Singleton()
class SomeBusinessService {

  static getInstance() {
    return new SomeBusinessService();
  }

  private constructor() { }

  someBusinessRule(a: number, b: number): boolean {
    return a < b;
  }
}

//  this return true
SomeBusinessService.getInstance() === SomeBusinessService.getInstance();