| 9 | import type { ServiceIdentifier } from './instantiation'; |
| 10 | |
| 11 | export class ServiceCollection { |
| 12 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 13 | private readonly _entries = new Map<ServiceIdentifier<any>, unknown>(); |
| 14 | |
| 15 | constructor( |
| 16 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 17 | ...entries: ReadonlyArray<readonly [ServiceIdentifier<any>, unknown]> |
| 18 | ) { |
| 19 | for (const [id, value] of entries) { |
| 20 | this._entries.set(id, value); |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Set an entry. Returns the previous value (or `undefined` if the id was |
| 26 | * not previously set). |
| 27 | */ |
| 28 | set<T>( |
| 29 | id: ServiceIdentifier<T>, |
| 30 | instanceOrDescriptor: T | SyncDescriptor<T>, |
| 31 | ): T | SyncDescriptor<T> | undefined { |
| 32 | const prev = this._entries.get(id); |
| 33 | this._entries.set(id, instanceOrDescriptor); |
| 34 | return prev as T | SyncDescriptor<T> | undefined; |
| 35 | } |
| 36 | |
| 37 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 38 | has(id: ServiceIdentifier<any>): boolean { |
| 39 | return this._entries.has(id); |
| 40 | } |
| 41 | |
| 42 | get<T>(id: ServiceIdentifier<T>): T | SyncDescriptor<T> | undefined { |
| 43 | return this._entries.get(id) as T | SyncDescriptor<T> | undefined; |
| 44 | } |
| 45 | |
| 46 | /** Iterate all entries. Order is insertion-order (Map semantics). */ |
| 47 | forEach( |
| 48 | callback: ( |
| 49 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 50 | id: ServiceIdentifier<any>, |
| 51 | value: unknown, |
| 52 | ) => void, |
| 53 | ): void { |
| 54 | this._entries.forEach((value, id) => callback(id, value)); |
| 55 | } |
| 56 | } |
nothing calls this directly
no outgoing calls
no test coverage detected