Initial import
This commit is contained in:
56
src/service/Canon.ts
Normal file
56
src/service/Canon.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import {Canonical} from "./Canonical";
|
||||
import {Singleton} from "@extollo/di";
|
||||
|
||||
/**
|
||||
* Error throw when a duplicate canonical key is registered.
|
||||
* @extends Error
|
||||
*/
|
||||
export class DuplicateResolverKeyError extends Error {
|
||||
constructor(key: string) {
|
||||
super(`There is already a canonical unit with the scope ${key} registered.`)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Error throw when a key that isn't registered with the service.
|
||||
* @extends Error
|
||||
*/
|
||||
export class NoSuchCanonicalResolverKeyError extends Error {
|
||||
constructor(key: string) {
|
||||
super(`There is no such canonical unit with the scope ${key} registered.`)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Service used to access various canonical resources.
|
||||
*/
|
||||
@Singleton()
|
||||
export class Canon {
|
||||
/**
|
||||
* The resources registered with this service. Map of canonical service name
|
||||
* to canonical service instance.
|
||||
* @type object
|
||||
*/
|
||||
protected resources: { [key: string]: Canonical<any> } = {}
|
||||
|
||||
/**
|
||||
* Get a canonical resource by its name key.
|
||||
* @param {string} key
|
||||
* @return Canonical
|
||||
*/
|
||||
resource<T>(key: string): Canonical<T> {
|
||||
if ( !this.resources[key] ) throw new NoSuchCanonicalResolverKeyError(key)
|
||||
return this.resources[key] as Canonical<T>
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a canonical resource.
|
||||
* @param {Canonical} unit
|
||||
*/
|
||||
registerCanonical(unit: Canonical<any>) {
|
||||
const key = unit.canonicalItems
|
||||
if ( this.resources[key] ) throw new DuplicateResolverKeyError(key)
|
||||
this.resources[key] = unit
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user