UNPKG

@nowarajs/singleton-manager

Version:

Singleton Manager is a package that provides a simple way to manage singletons in your application. It allows you to create, retrieve, and manage singletons with ease and type safety.

146 lines (105 loc) โ€ข 4.13 kB
# ๐ŸŽฏ NowaraJS - Singleton Manager ## ๐Ÿ“Œ Table of Contents - [๐ŸŽฏ Singleton Manager](#-singleton-manager) - [๐Ÿ“Œ Table of Contents](#-table-of-contents) - [๐Ÿ“ Description](#-description) - [โœจ Features](#-features) - [๐Ÿ”ง Installation](#-installation) - [โš™๏ธ Usage](#-usage) - [Basic Usage](#basic-usage) - [With Constructor Parameters](#with-constructor-parameters) - [Type Safety](#type-safety) - [API Methods](#api-methods) - [๐Ÿ“š API Reference](#-api-reference) - [โš–๏ธ License](#-license) - [๐Ÿ“ง Contact](#-contact) ## ๐Ÿ“ Description > 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. ## โœจ Features - ๐Ÿ”’ **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 ## ๐Ÿ”ง Installation ```bash bun add @nowarajs/singleton-manager @nowarajs/error ``` ## โš™๏ธ Usage ### Basic Usage ```typescript 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 ``` ### With Constructor Parameters ```typescript 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 ``` ### Type Safety ```typescript // 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 ``` ### API Methods ```typescript // 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); ``` ## ๐Ÿ“š API Reference You can find the complete API reference documentation for `SingletonManager` at: - [Reference Documentation](https://nowarajs.github.io/singleton-manager/) ## โš–๏ธ License Distributed under the MIT License. See [LICENSE](./LICENSE) for more information. ## ๐Ÿ“ง Contact - GitHub: [NowaraJS](https://github.com/NowaraJS) - Package: [@nowarajs/singleton-manager](https://www.npmjs.com/package/@nowarajs/singleton-manager)