This commit is contained in:
2025-10-29 00:15:48 -04:00
parent 3a7f5fe75d
commit 63dc90a8c8

View File

@@ -7,13 +7,12 @@ type Registration = {
/** /**
* Lifecycle types: * Lifecycle types:
* - 'singleton': A single shared instance cached for the lifetime of the manager * - 'singleton': A single shared instance cached for the lifetime of the manager
* - 'instance': A new instance created on each resolve (transient) * - 'transient': A new instance created on each resolve (transient)
* - 'service': A new instance created on each resolve (transient, alias for 'instance')
*/ */
type Lifecycle = 'instance' | 'service' | 'singleton'; type Lifecycle = 'transient' | 'singleton';
export class DependencyManager { export class DependencyManager {
private static readonly _supportedLifecycles: string[] = ['instance', 'service', 'singleton']; private static readonly _supportedLifecycles: Lifecycle[] = ['transient', 'singleton'];
private readonly _resolving: Set<string>; private readonly _resolving: Set<string>;
private readonly _registrations: Map<string, Registration>; private readonly _registrations: Map<string, Registration>;
private readonly _singletons: Map<string, unknown>; private readonly _singletons: Map<string, unknown>;
@@ -32,7 +31,7 @@ export class DependencyManager {
* @param lifecycle - Lifecycle of the instance ('singleton', 'instance', or 'service'). Default: 'singleton' * @param lifecycle - Lifecycle of the instance ('singleton', 'instance', or 'service'). Default: 'singleton'
* @throws Error if name is already registered or lifecycle is invalid * @throws Error if name is already registered or lifecycle is invalid
*/ */
public register(name: string, Class: new (...args: any[]) => any, dependencies: string[], lifecycle = 'singleton') { public register(name: string, Class: new (...args: any[]) => any, dependencies: string[], lifecycle: Lifecycle = 'singleton') {
if (this._registrations.has(name)) { if (this._registrations.has(name)) {
throw new Error(`Error: [${name}] is already registered`); throw new Error(`Error: [${name}] is already registered`);
} }