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

    @nowarajs/singleton-manager

    ๐ŸŽฏ Singleton Manager

    A powerful and type-safe singleton manager for TypeScript/JavaScript applications.

    Singleton Manager provides a centralized way to manage singleton instances in your application. It ensures that only one instance of each registered class exists throughout the application lifecycle, with full TypeScript support and type safety.

    • ๐Ÿ”’ Type-safe: Full TypeScript support with generics
    • ๐ŸŽฏ Centralized Management: Single registry for all your singletons
    • ๐Ÿ”ง Constructor Arguments: Support for classes with constructor parameters
    • ๐Ÿ›ก๏ธ Error Handling: Clear error messages for common mistakes
    • โšก Lightweight: Minimal overhead with maximum functionality
    • ๐Ÿงช Well Tested: Comprehensive test suite included
    bun add @nowarajs/singleton-manager
    
    import { SingletonManager } from '@nowarajs/singleton-manager';

    // Define your singleton class
    class DatabaseConnection {
    private _isConnected = false;

    public constructor() {
    console.log('Database connection created');
    this._isConnected = true;
    }

    public get isConnected(): boolean {
    return this._isConnected;
    }

    public query(sql: string): string[] {
    console.log(`Executing query: ${sql}`);
    return ['result1', 'result2'];
    }
    }

    // Register the singleton
    SingletonManager.register('DatabaseConnection', DatabaseConnection);

    // Get the singleton instance (same instance every time)
    const db1 = SingletonManager.get<DatabaseConnection>('DatabaseConnection');
    const db2 = SingletonManager.get<DatabaseConnection>('DatabaseConnection');

    console.log(db1 === db2); // true - same instance
    class ApiClient {
    private readonly _baseUrl: string;
    private readonly _apiKey: string;

    public constructor(baseUrl: string, apiKey: string) {
    this._baseUrl = baseUrl;
    this._apiKey = apiKey;
    }

    public get baseUrl(): string {
    return this._baseUrl;
    }
    }

    // Register with constructor arguments
    SingletonManager.register(
    'ApiClient',
    ApiClient,
    'https://api.example.com',
    'your-api-key'
    );

    // Use the singleton
    const client = SingletonManager.get<ApiClient>('ApiClient');
    console.log(client.baseUrl); // https://api.example.com
    // TypeScript provides full type safety
    const dbConnection = SingletonManager.get<DatabaseConnection>('DatabaseConnection');
    dbConnection.query('SELECT * FROM users'); // โœ… TypeScript knows this method exists

    // Attempting to call non-existent methods will cause TypeScript errors
    // dbConnection.nonExistentMethod(); // โŒ TypeScript error
    // Check if a singleton is registered
    if (SingletonManager.has('DatabaseConnection')) {
    const db = SingletonManager.get<DatabaseConnection>('DatabaseConnection');
    }

    // Unregister a singleton (removes the instance)
    SingletonManager.unregister('DatabaseConnection');

    // Re-register will create a new instance
    SingletonManager.register('DatabaseConnection', DatabaseConnection);

    You can find the complete API reference documentation for TypedEventEmitter at:

    Distributed under the MIT License. See LICENSE for more information.

    NowaraJS Team