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.
daton/lib/src/unit/InstantiableCanonical.ts

28 lines
1.0 KiB

import {Canonical, CanonicalDefinition} from './Canonical.ts'
import Instantiable, {isInstantiable} from '../../../di/src/type/Instantiable.ts'
/**
* Error thrown when the item returned from a canonical definition file is not the expected item.
* @extends Error
*/
export class InvalidCanonicalExportError extends Error {
constructor(name: string) {
super(`Unable to import canonical item from "${name}". The default export of this file is invalid.`)
}
}
/**
* Base class for Canonical units which return instantiated versions of the classes
* defined in those files. The files should export default clases which are Instantiable.
* @extends Canonical
*/
export class InstantiableCanonical<T> extends Canonical<Instantiable<T>> {
public async init_canonical_item(def: CanonicalDefinition): Promise<Instantiable<T>> {
if ( isInstantiable(def.imported.default) ) {
return this.make(def.imported.default)
}
throw new InvalidCanonicalExportError(def.original_name)
}
}