Aller au contenu

Utility

Gestion des Singletons : Enregistrement, Récupération et Désenregistrement

import { SingletonManager } from '@basalt-lab/basalt-helper';

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();
SingletonManager.get<ExampleSingleton>('ExampleSingleton').sayHello();
SingletonManager.unregister('ExampleSingleton');
const { SingletonManager } = require('@basalt-lab/basalt-helper');

class ExampleSingleton {
    static _count = 0;
    _id;

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

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

SingletonManager.register('ExampleSingleton', ExampleSingleton);
SingletonManager.get('ExampleSingleton').sayHello();
SingletonManager.get('ExampleSingleton').sayHello();
SingletonManager.unregister('ExampleSingleton');
node example.jsExampleSingleton created with ID: 1
Hello from instance 1!
Hello from instance 1!

Gestion des Singletons : Enregistrement, Récupération et Désenregistrement (avec des arguments)

import { SingletonManager } from '@basalt-lab/basalt-helper';

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

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

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

SingletonManager.register('ExampleSingleton', ExampleSingleton, 'John Doe');
SingletonManager.get<ExampleSingleton>('ExampleSingleton').sayHello();
SingletonManager.get<ExampleSingleton>('ExampleSingleton').sayHello();
SingletonManager.unregister('ExampleSingleton');
const { SingletonManager } = require('@basalt-lab/basalt-helper');

class ExampleSingleton {
    static _count = 0;
    _id;

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

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

SingletonManager.register('ExampleSingleton', ExampleSingleton, 'John Doe');
SingletonManager.get('ExampleSingleton').sayHello();
SingletonManager.get('ExampleSingleton').sayHello();
SingletonManager.unregister('ExampleSingleton');
node example.jsExampleSingleton created with ID: 1 and name: John Doe
Hello from instance 1!
Hello from instance 1!