2021-03-03 00:57:41 +00:00
|
|
|
/**
|
|
|
|
* Error thrown when the item returned from a canonical definition file is not the expected item.
|
|
|
|
* @extends Error
|
|
|
|
*/
|
2021-06-03 03:36:25 +00:00
|
|
|
import {Canonical, CanonicalDefinition} from './Canonical'
|
2021-07-25 14:15:01 +00:00
|
|
|
import {isInstantiable} from '../di'
|
2021-03-03 00:57:41 +00:00
|
|
|
|
2021-03-25 13:50:13 +00:00
|
|
|
/**
|
|
|
|
* Error thrown when the export of a canonical file is determined to be invalid.
|
|
|
|
*/
|
2021-03-03 00:57:41 +00:00
|
|
|
export class InvalidCanonicalExportError extends Error {
|
|
|
|
constructor(name: string) {
|
|
|
|
super(`Unable to import canonical item from "${name}". The default export of this file is invalid.`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 13:50:13 +00:00
|
|
|
/**
|
|
|
|
* Variant of the Canonical unit whose files export classes which are instantiated using the global container.
|
|
|
|
*/
|
2021-07-25 14:15:01 +00:00
|
|
|
export class CanonicalInstantiable<T> extends Canonical<T> {
|
|
|
|
public async initCanonicalItem(definition: CanonicalDefinition): Promise<T> {
|
2021-03-03 00:57:41 +00:00
|
|
|
if ( isInstantiable(definition.imported.default) ) {
|
|
|
|
return this.app().make(definition.imported.default)
|
|
|
|
}
|
|
|
|
|
2021-03-08 17:08:56 +00:00
|
|
|
if ( isInstantiable(definition.imported[definition.canonicalName.split(':').reverse()[0]]) ) {
|
|
|
|
return this.app().make(definition.imported[definition.canonicalName.split(':').reverse()[0]])
|
2021-03-06 17:44:32 +00:00
|
|
|
}
|
|
|
|
|
2021-03-03 00:57:41 +00:00
|
|
|
throw new InvalidCanonicalExportError(definition.originalName)
|
|
|
|
}
|
2021-03-25 13:50:13 +00:00
|
|
|
}
|