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.

32 lines
958 B

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<any> } = {}
resource<T>(key: string): Canonical<T> {
if ( !this.resources[key] ) throw new NoSuchCanonicalResolverKeyError(key)
return this.resources[key] as Canonical<T>
}
register_canonical(unit: Canonical<any>) {
const key = unit.canonical_items
if ( this.resources[key] ) throw new DuplicateResolverKeyError(key)
this.resources[key] = unit
}
}