Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Singleton<T>

Asynchronous Singleton Generator.

The Singleton is an asynchronous singleton generator class who guarantees the lazy constructor to be called *"only one at time". The *"only one at time" would always be kepted, even in the race condition.

Create a Singleton instance with your custom lazy constructor and get the promised value through the Singleton.get() method. The Singleton.get() method would construct the return value following below logics:

  • At the first time: calls the lazy constructor and returns the value.
  • After the lazy construction: returns the pre-constructed value.
  • Race condition:
    • simultaneously call happens during the lazy construction.
    • guarantees the *"only one at time"* through a mutex.

If you want to reload the promised value, regardless of whether the lazy construction has been completed or not, call the Singleton.reload() method. It would call the lazy constructor forcibly, even if the lany construction has been completed in sometime.

author

Jeongho Nam - https://github.com/samchon

Type parameters

  • T

    Type of the promised value to be lazy-constructed.

Hierarchy

  • Singleton

Index

Constructors

Methods

Constructors

constructor

  • new Singleton(lazyConstructor: () => Promise<T>): Singleton
  • Initializer Constructor.

    Create a new Singleton instance with the lazy consturctor.

    Parameters

    • lazyConstructor: () => Promise<T>

      Lazy constructor function returning the promised value.

        • (): Promise<T>
        • Returns Promise<T>

    Returns Singleton

Methods

get

  • get(): Promise<T>
  • Get promised value.

    Singleton.get() method returns the lazy constructed value*. It guarantees the *lazy constructor to be called *"only one at time". It ensures the *"only one at time", even in the race condition.

    If the promised value is not constructed yet (call this method at the first time), the lazy constructor would be called and returns the promised value. Otherwise, the promised value has been already constructed by the lazy constructor (this method already had been called), returns the pre-generated value.

    Also, you don't need to worry anything even race condition has been occured, calling Singleton.get() simultaneously when the lazy construction is not completed but on going. The Singleton guarantees the lazy constructor to be called only one at time by using the unique-lock on a mutex.

    Returns Promise<T>

    The lazy constructed value.

reload

  • reload(): Promise<T>
  • Reload value.

    The Singleton.reload() method enforces to call the lazy constructor, regardless of whether the lazy construction has been completed or not. Therefore, even if the lazy construction has been completed in sometime, the Singleton.reload() will call the lazy constructor again.

    However, unlike Singleton.get(), Singleton.reload() does not ensure the safety in the race condition. Therefore, you've to be careful by yourself when using this Singleton.reload() method. Try not to call this method simultaneously.

    Returns Promise<T>

    Re-constructed value.

Generated using TypeDoc