import { getNameOfServiceType } from "../helpers/getNameOfServiceType.js"; import type { IServiceCollection, IServiceConstructor, IServiceDescriptor, IServiceFactory, IServiceImplementation, IServiceImplementationResolver, SingleServiceImplementation, SingleServiceType, } from "../types/index.js"; import { IServiceProvider } from "./IServiceProvider.js"; import { ServiceLifetime } from "./ServiceLifetime.js"; import { ServiceRegistry } from "./ServiceRegistry.js"; import { fallback } from "../implementation-resolvers/fallback.js"; const EMPTY_DESCRIPTORS: IServiceDescriptor[] = []; export class ServiceCollection implements IServiceCollection { protected readonly collection: Map[]>; constructor(collection?: ServiceCollection) { this.collection = collection ? collection.copy() : new Map(); if (!this.collection.has(IServiceProvider)) { this.addTransient(IServiceProvider); } } *[Symbol.iterator](): Iterator, any, undefined> { for (const descriptors of this.collection.values()) { yield* descriptors; } } getDescriptors( service: SingleServiceType, ): readonly IServiceDescriptor[] { return (this.collection.get(service) ?? EMPTY_DESCRIPTORS) as IServiceDescriptor[]; } getDescriptor( service: SingleServiceType, ): IServiceDescriptor { const descriptors = this.collection.get(service); if (descriptors) { return descriptors[descriptors.length - 1] as IServiceDescriptor; } let name = getNameOfServiceType(service); const metadata = ServiceRegistry.get(service); if (metadata?.name) { name = metadata.name; } throw new Error(`No service of type "${name}" is registered`); } addScoped(service: SingleServiceImplementation): this; addScoped( service: SingleServiceType, factory: IServiceFactory, ): this; addScoped( service: SingleServiceType, constructor: IServiceConstructor, ): this; addScoped( service: SingleServiceType, resolver: IServiceImplementationResolver< T | SingleServiceImplementation >, ): this; addScoped( service: SingleServiceType, implementation: IServiceImplementation = service as SingleServiceImplementation, ): this { this.addService(ServiceLifetime.Scoped, service, implementation); return this; } addTransient(service: SingleServiceImplementation): this; addTransient( service: SingleServiceType, factory: IServiceFactory, ): this; addTransient( service: SingleServiceType, constructor: IServiceConstructor, ): this; addTransient( service: SingleServiceType, resolver: IServiceImplementationResolver< T | SingleServiceImplementation >, ): this; addTransient( service: SingleServiceType, implementation: IServiceImplementation = service as SingleServiceImplementation, ): this { this.addService(ServiceLifetime.Transient, service, implementation); return this; } addSingleton(service: SingleServiceImplementation): this; addSingleton( service: SingleServiceType, implementation: T, ): this; addSingleton( service: SingleServiceType, factory: IServiceFactory, ): this; addSingleton( service: SingleServiceType, constructor: IServiceConstructor, ): this; addSingleton( service: SingleServiceType, resolver: IServiceImplementationResolver< T | SingleServiceImplementation >, ): this; addSingleton( service: SingleServiceType, implementation: IServiceImplementation = service as SingleServiceImplementation, ): this { this.addService(ServiceLifetime.Singleton, service, implementation); return this; } protected copy(): Map[]> { const newMap = new Map[]>(); for (const [key, descriptors] of this.collection) { const len = descriptors.length; // Pre-allocate exact size to avoid dynamic growth / copying const all: IServiceDescriptor[] = new Array(len); for (let i = 0; i < len; i++) { const d = descriptors[i]!; // Manual property copy is faster than object spread for small fixed objects all[i] = { lifetime: d.lifetime, service: d.service, resolver: d.resolver, }; } newMap.set(key, all); } return newMap; } protected addService( lifetime: ServiceLifetime, service: SingleServiceType, implementation: IServiceImplementation, ): this { const descriptor: IServiceDescriptor = { lifetime, service, resolver: fallback(implementation), }; let descriptors = this.collection.get(service); if (descriptors) { descriptors.push(descriptor); } else { this.collection.set(service, [descriptor]); } return this; } }