import {Service} from '../../../di/src/decorator/Service.ts' import {Canonical} from './Canonical.ts' export class DuplicateResolverKeyError extends Error { constructor(key: string) { super(`There is already a canonical unit with the scope ${key} registered.`) } } export class NoSuchCanonicalResolverKeyError extends Error { constructor(key: string) { super(`There is no such canonical unit with the scope ${key} registered.`) } } @Service() export class Canon { protected resources: { [key: string]: Canonical } = {} resource(key: string): Canonical { if ( !this.resources[key] ) throw new NoSuchCanonicalResolverKeyError(key) return this.resources[key] as Canonical } register_canonical(unit: Canonical) { const key = unit.canonical_items if ( this.resources[key] ) throw new DuplicateResolverKeyError(key) this.resources[key] = unit } }