You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
979 B

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
}
}