updates
This commit is contained in:
@@ -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`);
|
||||||
}
|
}
|
||||||
@@ -62,7 +61,7 @@ export class DependencyManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const registration = this._registrations.get(name);
|
const registration = this._registrations.get(name);
|
||||||
const {Class, dependencies, lifecycle} = registration!;
|
const { Class, dependencies, lifecycle } = registration!;
|
||||||
|
|
||||||
if (lifecycle === 'singleton' && this._singletons.has(name)) {
|
if (lifecycle === 'singleton' && this._singletons.has(name)) {
|
||||||
console.debug(`↻ Using cached singleton [${name}]`);
|
console.debug(`↻ Using cached singleton [${name}]`);
|
||||||
|
|||||||
Reference in New Issue
Block a user