2020-06-17 10:34:01 +00:00
|
|
|
import { interfaces, Container } from "inversify"
|
2019-09-07 04:23:15 +00:00
|
|
|
|
2020-06-17 10:34:01 +00:00
|
|
|
let _container: Container
|
2019-09-07 04:23:15 +00:00
|
|
|
|
2020-06-17 10:34:01 +00:00
|
|
|
const ContainerInstance = Symbol.for("@ccms/ioc:Container")
|
|
|
|
const INJECTION = Symbol.for("INJECTION")
|
2019-09-07 04:23:15 +00:00
|
|
|
|
|
|
|
function _proxyGetter(
|
|
|
|
proto: any,
|
|
|
|
key: string,
|
|
|
|
resolve: () => any,
|
|
|
|
doCache: boolean
|
|
|
|
) {
|
2019-09-27 10:39:40 +00:00
|
|
|
function getter(this: object) {
|
2019-09-07 04:23:15 +00:00
|
|
|
if (doCache && !Reflect.hasMetadata(INJECTION, this, key)) {
|
2020-06-17 10:34:01 +00:00
|
|
|
Reflect.defineMetadata(INJECTION, resolve(), this, key)
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
if (Reflect.hasMetadata(INJECTION, this, key)) {
|
2020-06-17 10:34:01 +00:00
|
|
|
return Reflect.getMetadata(INJECTION, this, key)
|
2019-09-07 04:23:15 +00:00
|
|
|
} else {
|
2020-06-17 10:34:01 +00:00
|
|
|
return resolve()
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-27 10:39:40 +00:00
|
|
|
function setter(this: object, newVal: any) {
|
2020-06-17 10:34:01 +00:00
|
|
|
Reflect.defineMetadata(INJECTION, newVal, this, key)
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Object.defineProperty(proto, key, {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
get: getter,
|
|
|
|
set: setter
|
2020-06-17 10:34:01 +00:00
|
|
|
})
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function initContainer(container: Container) {
|
2020-06-17 10:34:01 +00:00
|
|
|
Reflect.defineMetadata(ContainerInstance, container, Reflect)
|
|
|
|
return _container = container
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getContainer(): Container {
|
2020-06-17 10:34:01 +00:00
|
|
|
return _container || initContainer(new Container())
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function makePropertyInjectDecorator(doCache: boolean) {
|
2020-06-17 10:34:01 +00:00
|
|
|
return function (serviceIdentifier: interfaces.ServiceIdentifier<any>) {
|
|
|
|
return function (proto: any, key: string): void {
|
2019-09-07 04:23:15 +00:00
|
|
|
let resolve = () => {
|
2020-06-17 10:34:01 +00:00
|
|
|
return getContainer().get(serviceIdentifier)
|
|
|
|
}
|
|
|
|
_proxyGetter(proto, key, resolve, doCache)
|
|
|
|
}
|
|
|
|
}
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function makePropertyInjectNamedDecorator(doCache: boolean) {
|
2020-06-17 10:34:01 +00:00
|
|
|
return function (serviceIdentifier: interfaces.ServiceIdentifier<any>, named: string) {
|
|
|
|
return function (proto: any, key: string): void {
|
2019-09-07 04:23:15 +00:00
|
|
|
let resolve = () => {
|
2020-06-17 10:34:01 +00:00
|
|
|
return getContainer().getNamed(serviceIdentifier, named)
|
|
|
|
}
|
|
|
|
_proxyGetter(proto, key, resolve, doCache)
|
|
|
|
}
|
|
|
|
}
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function makePropertyInjectTaggedDecorator(doCache: boolean) {
|
2020-06-17 10:34:01 +00:00
|
|
|
return function (serviceIdentifier: interfaces.ServiceIdentifier<any>, key: string, value: any) {
|
|
|
|
return function (proto: any, propertyName: string): void {
|
2019-09-07 04:23:15 +00:00
|
|
|
let resolve = () => {
|
2020-06-17 10:34:01 +00:00
|
|
|
return getContainer().getTagged(serviceIdentifier, key, value)
|
|
|
|
}
|
|
|
|
_proxyGetter(proto, propertyName, resolve, doCache)
|
|
|
|
}
|
|
|
|
}
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function makePropertyMultiInjectDecorator(doCache: boolean) {
|
2020-06-17 10:34:01 +00:00
|
|
|
return function (serviceIdentifier: interfaces.ServiceIdentifier<any>) {
|
|
|
|
return function (proto: any, key: string): void {
|
2019-09-07 04:23:15 +00:00
|
|
|
let resolve = () => {
|
2020-06-17 10:34:01 +00:00
|
|
|
return getContainer().getAll(serviceIdentifier)
|
|
|
|
}
|
|
|
|
_proxyGetter(proto, key, resolve, doCache)
|
|
|
|
}
|
|
|
|
}
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 10:34:01 +00:00
|
|
|
let doCache = true
|
2019-09-07 04:23:15 +00:00
|
|
|
|
|
|
|
let lazyInject = makePropertyInjectDecorator(doCache)
|
|
|
|
let lazyInjectNamed = makePropertyInjectNamedDecorator(doCache)
|
|
|
|
let lazyInjectTagged = makePropertyInjectTaggedDecorator(doCache)
|
|
|
|
let lazyMultiInject = makePropertyMultiInjectDecorator(doCache)
|
|
|
|
|
|
|
|
export {
|
|
|
|
initContainer,
|
|
|
|
getContainer,
|
2020-01-17 03:10:30 +00:00
|
|
|
ContainerInstance,
|
2019-09-07 04:23:15 +00:00
|
|
|
lazyInject,
|
|
|
|
lazyInjectNamed,
|
|
|
|
lazyInjectTagged,
|
|
|
|
lazyMultiInject
|
2020-06-17 10:34:01 +00:00
|
|
|
}
|