Function Singleton

  • Class decorator factory. Enforces a transparent Singleton pattern on the decorated class. When calling the new operator on a decorated class for the first time, an instance of the decorated class is created using the supplied arguments, if any. This instance will remain the Singleton instance of the decorated class indefinitely. When calling the new operator on a decorated class already instantiated, the Singleton pattern is enforced and the previously constructed instance is returned. Instead, if provided, the apply callback is fired with the Singleton instance and the new invocation parameters.

    Type Parameters

    • T extends (new (...args) => any)

      The type of the decorated constructor.

    Parameters

    Returns ((constructor) => T)

    A class constructor decorator.

      • (constructor): T
      • Parameters

        • constructor: T

        Returns T

    Example

    Singleton class:

    import { Singleton } from '@sgrud/core';

    ⁠@Singleton()
    export class Service {}

    new Service() === new Service(); // true

Generated using TypeDoc