import type { MemoryProviderAdapter, MemoryProviderAdapterFactoryContext } from './adapter.ts' export interface MemoryAdapterFactory { readonly id: Id create(context: Context): Adapter } export class MemoryAdapterFactoryRegistry { private readonly factories = new Map>() constructor(factories: readonly MemoryAdapterFactory[] = []) { for (const factory of factories) this.register(factory) } register(factory: MemoryAdapterFactory): () => void { if (this.factories.has(factory.id)) throw new Error(`memory adapter factory is already registered: ${factory.id}`) this.factories.set(factory.id, factory) let active = true return () => { if (!active) return active = false if (this.factories.get(factory.id) === factory) this.factories.delete(factory.id) } } create(context: Context): Map { const adapters = new Map() for (const factory of this.factories.values()) { const adapter = factory.create(context) if (adapter.id !== factory.id) throw new Error(`memory adapter factory ${factory.id} returned ${adapter.id}`) if (adapters.has(adapter.id)) throw new Error(`memory adapter is already created: ${adapter.id}`) adapters.set(adapter.id, adapter) } return adapters } ids(): Id[] { return [...this.factories.keys()] } } export class MemoryProviderAdapterRegistry extends MemoryAdapterFactoryRegistry {}