import {Service} from '../../../di/src/decorator/Service.ts' export type CanonicalResolver = (key: string) => any export class DuplicateResolverKeyError extends Error { constructor(key: string) { super(`There is already a canonical resource with the scope ${key} registered.`) } } @Service() export class Canon { protected resources: { [key: string]: any } = {} get(key: string): any { const key_parts = key.split('::') let desc_value = this.resources key_parts.forEach(part => { if ( typeof desc_value === 'function' ) { desc_value = desc_value(part) } else { desc_value = desc_value[part] } }) return desc_value } register_resource(scope: string, resolver: CanonicalResolver) { if ( this.resources[scope] ) { throw new DuplicateResolverKeyError(scope) } this.resources[scope] = resolver } }