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.

26 lines
867 B

import { container } from '../global.ts'
import { isInstantiable } from '../type/Instantiable.ts'
import { Injectable } from './Injection.ts'
const injectable = Injectable()
/**
* Class decorator that marks a class as injectable and singleton in the dependency container.
* When this is applied, the Injectable decorator is automatically applied, and the class is
* marked so that, when it is injected into classes, a singleton instance is used.
* @param {string} [name] - optionally, refer to the dependency by a name, rather than by class type
* @constructor
*/
const Service = (name?: string): ClassDecorator => {
return (target) => {
if ( isInstantiable(target) ) {
if ( name ) container.register_named(name, target)
else container.register(target)
injectable(target)
}
}
}
export { Service }