@nowarajs/singleton-manager
    Preparing search index...

    Class SingletonManager

    SingletonManager is a static class that manages the singletons in the application. When a class is registered, the SingletonManager creates a new instance of the class when it is requested.

    class ExampleSingleton {
    private static _count = 0;
    private readonly _id: number;

    public constructor() {
    ExampleSingleton._count += 1;
    this._id = ExampleSingleton._count;
    console.log(`ExampleSingleton created with ID: ${this._id}`);
    }

    public sayHello(): void {
    console.log(`Hello from instance ${this._id}!`);
    }
    }

    SingletonManager.register('ExampleSingleton', ExampleSingleton);

    SingletonManager.get<ExampleSingleton>('ExampleSingleton').sayHello(); // Output: ExampleSingleton created with ID: 1 /n Hello from instance 1!
    SingletonManager.get<ExampleSingleton>('ExampleSingleton').sayHello(); // Output: Hello from instance 1!
    Index

    Constructors

    Methods

    Constructors

    Methods

    • Gets the singleton instance of the class. If the class is not registered, it throws an error.

      Type Parameters

      • TClass

        The type of the class.

      Parameters

      • name: string

        The name of the class to get the singleton instance.

      Returns TClass

      The singleton instance of the class.

      (Error) If the class is not registered, it throws an error.

    • Checks if the class is registered in the SingletonManager.

      Parameters

      • name: string

        The name of the class to check if it is registered.

      Returns boolean

      True if the class is registered, otherwise false.

    • Registers a class constructor in the SingletonManager.

      Type Parameters

      • TClass extends object

        The type of the class.

      • TArgs extends unknown[]

        The tuple type of the constructor arguments.

      Parameters

      • name: string

        The name of the class.

      • constructor: new (...args: TArgs) => TClass

        The constructor of the class.

      • ...args: TArgs

        The arguments to pass to the constructor of the class.

      Returns void

      (Error) If the class constructor is already registered, it throws an error.

    • Unregisters a class from the SingletonManager.

      Parameters

      • name: string

        The name of the class to unregister. ²

      Returns void

      (Error) If the class constructor is not registered, it throws an error.