import type { IResolvedServiceImplementationCreator, IServiceDescriptor, IServiceInstanceInfo, } from "../types/index.js"; import { getNameOfDescriptor } from "../helpers/getNameOfDescriptor.js"; const NOT_INITIALIZED = Symbol("NOT_INITIALIZED"); export class ServiceInstanceInfo implements IServiceInstanceInfo { get instance(): T { if (!this.initialized) { throw new Error( `Service "${getNameOfDescriptor(this.descriptor)}" is not initialized (circular dependency)`, ); } return this._instance as T; } get initialized(): boolean { return this._instance !== NOT_INITIALIZED; } disposed: boolean; dependents: IServiceInstanceInfo[]; private _instance: T | typeof NOT_INITIALIZED; constructor(public descriptor: IServiceDescriptor) { this.disposed = false; this.dependents = []; this._instance = NOT_INITIALIZED; } next() { return { done: true as const, value: this._instance, }; } [Symbol.iterator]() { return this; } initialize( creator: IResolvedServiceImplementationCreator, dependencies: any[], ): T { if (this._instance === NOT_INITIALIZED) { this._instance = creator(dependencies); } return this._instance; } addDependent(dependent: IServiceInstanceInfo): void { this.dependents.push(dependent); } disposeSync(): void { if (this.disposed || !this.initialized) { return; } const instance = this._instance as any; const disposeMethod = instance?.[Symbol.dispose] ?? instance?.dispose; if (typeof disposeMethod === "function") { for (const dependent of this.dependents) { dependent.disposeSync(); } Reflect.apply(disposeMethod, instance, []); this.disposed = true; } } async disposeAsync(): Promise { if (this.disposed || !this.initialized) { return; } const instance = this._instance as any; const disposeMethod = instance?.[Symbol.asyncDispose] ?? instance?.dispose; if (typeof disposeMethod === "function") { this.disposed = true; try { await Promise.all( [...this.dependents].map((dependent) => dependent.disposeAsync()), ); await Reflect.apply(disposeMethod, instance, []); } catch (e) { this.disposed = false; throw e; } } else { this.disposeSync(); } } }