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.

45 lines (42 loc) 1.32 kB
// @bun // source/singletonManager.ts import { BaseError } from "@nowarajs/error"; // source/enums/singletonManagerErrorKeys.ts var SINGLETON_MANAGER_ERROR_KEYS = { CLASS_CONSTRUCTOR_ALREADY_REGISTERED: "singletonManager.error.class_constructor_already_registered", CLASS_CONSTRUCTOR_NOT_REGISTERED: "singletonManager.error.class_constructor_not_registered" }; // source/singletonManager.ts class SingletonManager { static _registry = new Map; static register(name, constructor, ...args) { if (this._registry.has(name)) throw new BaseError({ message: SINGLETON_MANAGER_ERROR_KEYS.CLASS_CONSTRUCTOR_ALREADY_REGISTERED, cause: { name } }); this._registry.set(name, new constructor(...args)); } static unregister(name) { if (!this._registry.has(name)) throw new BaseError({ message: SINGLETON_MANAGER_ERROR_KEYS.CLASS_CONSTRUCTOR_NOT_REGISTERED, cause: { name } }); this._registry.delete(name); } static get(name) { if (!this._registry.has(name)) throw new BaseError({ message: SINGLETON_MANAGER_ERROR_KEYS.CLASS_CONSTRUCTOR_NOT_REGISTERED, cause: { name } }); return this._registry.get(name); } static has(name) { return this._registry.has(name); } } export { SingletonManager };