diff --git a/src/ts/@loafer/context/decorator/Configuration.ts b/src/ts/@loafer/context/decorator/Configuration.ts index 9d64ee4..d4bc3f2 100644 --- a/src/ts/@loafer/context/decorator/Configuration.ts +++ b/src/ts/@loafer/context/decorator/Configuration.ts @@ -3,11 +3,12 @@ import ConfigurationDefinition from '@loafer/context/definition/ConfigurationDef import { getConfigurationDefinition } from '@loafer/context/util/metadata'; import GetAppContext from '@loafer/context'; -import { DEFAULT_QUALIFIER, InjectableSterotype } from '@loafer/pouches/constants'; +import { InjectableSterotype } from '@loafer/pouches/constants'; import DefaultPouchFactory from '@loafer/pouches/factory/implement/DefaultPouchFactory'; import PouchDefinitionRegistry from '@loafer/pouches/factory/registry/PouchDefinitionRegistry'; +import { validateQualifier } from '@loafer/pouches/util/qualifier'; -const Configuration = (qualifier: string | symbol = DEFAULT_QUALIFIER ) => createDecorator('Configuration', { +const Configuration = (qualifier?: string | symbol ) => createDecorator('Configuration', { classDecorator: (target: TFunction): TFunction | void => { let configurationDefinition: ConfigurationDefinition = getConfigurationDefinition(target.prototype, false); if (undefined !== configurationDefinition) { @@ -16,7 +17,7 @@ const Configuration = (qualifier: string | symbol = DEFAULT_QUALIFIER ) => creat configurationDefinition = getConfigurationDefinition(target, true); configurationDefinition.Stereotype = InjectableSterotype.CONFIGURATION; - configurationDefinition.Qualifier = qualifier; + configurationDefinition.Qualifier = validateQualifier(target.prototype, qualifier); let pouchFactory: DefaultPouchFactory = GetAppContext().getPouchFactory(); let pouchDefinitionRegistry = pouchFactory; diff --git a/src/ts/@loafer/context/decorator/Injectable.ts b/src/ts/@loafer/context/decorator/Injectable.ts index 8e9dd8b..df8b8f5 100644 --- a/src/ts/@loafer/context/decorator/Injectable.ts +++ b/src/ts/@loafer/context/decorator/Injectable.ts @@ -3,19 +3,19 @@ import InjectableDefinition from '@loafer/context/definition/InjectableDefinitio import { getInjectableDefinition } from '@loafer/context/util/metadata'; import GetAppContext from '@loafer/context'; -import { DEFAULT_QUALIFIER } from '@loafer/pouches/constants'; +import { validateQualifier } from '@loafer/pouches/util/qualifier'; import DefaultPouchFactory from '@loafer/pouches/factory/implement/DefaultPouchFactory'; import PouchDefinitionRegistry from '@loafer/pouches/factory/registry/PouchDefinitionRegistry'; -const Injectable = (qualifier: string | symbol = DEFAULT_QUALIFIER ) => createDecorator('Injectable', { +const Injectable = (qualifier?: string | symbol ) => createDecorator('Injectable', { classDecorator: (target: TFunction): TFunction | void => { let injectableDefinition: InjectableDefinition = getInjectableDefinition(target.prototype, false); if (undefined !== injectableDefinition) { throw new Error('Cannot apply @Injectable decorator multiple times.'); } - injectableDefinition = getInjectableDefinition(target, true); - injectableDefinition.Qualifier = qualifier; + injectableDefinition = getInjectableDefinition(target.prototype, true); + injectableDefinition.Qualifier = validateQualifier(target.prototype, qualifier); let pouchFactory: DefaultPouchFactory = GetAppContext().getPouchFactory(); let pouchDefinitionRegistry = pouchFactory; diff --git a/src/ts/@loafer/context/decorator/Service.ts b/src/ts/@loafer/context/decorator/Service.ts index 182c5c8..216f151 100644 --- a/src/ts/@loafer/context/decorator/Service.ts +++ b/src/ts/@loafer/context/decorator/Service.ts @@ -3,11 +3,12 @@ import InjectableDefinition from '@loafer/context/definition/InjectableDefinitio import { getInjectableDefinition } from '@loafer/context/util/metadata'; import GetAppContext from '@loafer/context'; -import { DEFAULT_QUALIFIER, InjectableSterotype } from '@loafer/pouches/constants'; +import { InjectableSterotype } from '@loafer/pouches/constants'; +import { validateQualifier } from '@loafer/pouches/util/qualifier'; import DefaultPouchFactory from '@loafer/pouches/factory/implement/DefaultPouchFactory'; import PouchDefinitionRegistry from '@loafer/pouches/factory/registry/PouchDefinitionRegistry'; -const Service = (qualifier: string | symbol = DEFAULT_QUALIFIER ) => createDecorator('Service', { +const Service = (qualifier?: string | symbol ) => createDecorator('Service', { classDecorator: (target: TFunction): TFunction | void => { let injectableDefinition: InjectableDefinition = getInjectableDefinition(target.prototype, false); if (undefined !== injectableDefinition) { @@ -16,7 +17,7 @@ const Service = (qualifier: string | symbol = DEFAULT_QUALIFIER ) => createDecor injectableDefinition = getInjectableDefinition(target, true); injectableDefinition.Stereotype = InjectableSterotype.SERVICE; - injectableDefinition.Qualifier = qualifier; + injectableDefinition.Qualifier = validateQualifier(target.prototype, qualifier); let pouchFactory: DefaultPouchFactory = GetAppContext().getPouchFactory(); let pouchDefinitionRegistry = pouchFactory; diff --git a/src/ts/@loafer/pouches/constants/index.ts b/src/ts/@loafer/pouches/constants/index.ts index c4e0621..966e41b 100644 --- a/src/ts/@loafer/pouches/constants/index.ts +++ b/src/ts/@loafer/pouches/constants/index.ts @@ -1,5 +1,3 @@ export * from './reflect'; export * from './stereotype'; export * from './types'; - -export const DEFAULT_QUALIFIER = Symbol('__DEFAULT_QUALIFIER__'); diff --git a/src/ts/@loafer/pouches/definition/InjectDefinition.ts b/src/ts/@loafer/pouches/definition/InjectDefinition.ts index 3fef19c..f6caf12 100644 --- a/src/ts/@loafer/pouches/definition/InjectDefinition.ts +++ b/src/ts/@loafer/pouches/definition/InjectDefinition.ts @@ -1,5 +1,5 @@ -import * as METADATA from '@loafer/pouches/constants'; -import { DEFAULT_QUALIFIER, InjectSterotype } from '@loafer/pouches/constants'; +import { InjectSterotype } from '@loafer/pouches/constants'; +import { validateQualifier } from '@loafer/pouches/util/qualifier'; import InjectConfig from '@loafer/pouches/decorator/InjectConfig'; import * as CoreConstants from '@loafer/core/constants'; @@ -27,9 +27,7 @@ class InjectDefinition { * addInject */ public addInject(injectConfig: InjectConfig, propertyKey: string | symbol, parameterIndex?: number): void { - if (undefined === injectConfig.qualifier) { - injectConfig.qualifier = DEFAULT_QUALIFIER; - } + injectConfig.qualifier = validateQualifier(this.target, injectConfig.qualifier); if (undefined === injectConfig.required) { injectConfig.required = false; } diff --git a/src/ts/@loafer/pouches/factory/implement/AbstractPouchFactory.ts b/src/ts/@loafer/pouches/factory/implement/AbstractPouchFactory.ts index 136bd1d..56920cc 100644 --- a/src/ts/@loafer/pouches/factory/implement/AbstractPouchFactory.ts +++ b/src/ts/@loafer/pouches/factory/implement/AbstractPouchFactory.ts @@ -9,7 +9,7 @@ import DefaultSingletonPouchRegistry from '@loafer/pouches/factory/implement/Def abstract class AbstractPouchFactory extends DefaultSingletonPouchRegistry implements PouchDefinitionRegistry { - protected pouchDefinitionMap: Map>; + protected pouchDefinitionMap: Map>; public constructor() { super(); this.pouchDefinitionMap = new Map(); @@ -17,33 +17,35 @@ abstract class AbstractPouchFactory extends DefaultSingletonPouchRegistry implem public registerPouchDefinition(pouchDefinition: PouchDefinition): void { let type = pouchDefinition.Type; - let qualifier = pouchDefinition.Qualifier; + let qualifier = validateQualifier(type, pouchDefinition.Qualifier); if (this.hasPouchDefinition(type, qualifier)) { throw new Error(`Pouch Definition[${type.constructor.name}:${qualifier}] is exist already`); } - let map = this.pouchDefinitionMap.get(type); + let map = this.pouchDefinitionMap.get(qualifier); if (undefined === map) { map = new Map(); + this.pouchDefinitionMap.set(qualifier, map); } - map.set(qualifier, pouchDefinition); + map.set(type, pouchDefinition); } public removePouchDefinition(type: Object, qualifier?: string | symbol): void { - if (!this.hasPouchDefinition(type, qualifier)) { + const _qualifier = validateQualifier(type, qualifier); + if (!this.hasPouchDefinition(type, _qualifier)) { throw new Error(`Pouch Definition[${type.constructor.name}:${qualifier}] is not exist`); } - const _qualifier = validateQualifier(qualifier); - this.pouchDefinitionMap.get(type).delete(_qualifier); - if (0 === this.pouchDefinitionMap.get(type).size) { - this.pouchDefinitionMap.delete(type); + this.pouchDefinitionMap.get(_qualifier).delete(type); + if (0 === this.pouchDefinitionMap.get(_qualifier).size) { + this.pouchDefinitionMap.delete(_qualifier); } } public getPouchDefinition(type: Object, qualifier?: string | symbol): PouchDefinition { - if (!this.pouchDefinitionMap.has(type)) { + const _qualifier = validateQualifier(type, qualifier); + + if (!this.pouchDefinitionMap.has(_qualifier)) { return undefined; } - const _qualifier = validateQualifier(qualifier); - return this.pouchDefinitionMap.get(type).get(qualifier); + return this.pouchDefinitionMap.get(_qualifier).get(type); } public hasPouchDefinition(type: Object, qualifier?: string | symbol): boolean { return undefined === this.getPouchDefinition(type, qualifier) ? false : true; diff --git a/src/ts/@loafer/pouches/factory/implement/DefaultPouchFactory.ts b/src/ts/@loafer/pouches/factory/implement/DefaultPouchFactory.ts index fc33ef6..ce8cc5c 100644 --- a/src/ts/@loafer/pouches/factory/implement/DefaultPouchFactory.ts +++ b/src/ts/@loafer/pouches/factory/implement/DefaultPouchFactory.ts @@ -5,8 +5,10 @@ import { import { PouchScope, - DEFAULT_QUALIFIER, } from '@loafer/pouches/constants'; +import { + validateQualifier, +} from '@loafer/pouches/util/qualifier'; import PouchFactory from '@loafer/pouches/factory/PouchFactory'; import AbstractPouchFactory from '@loafer/pouches/factory/implement/AbstractPouchFactory'; @@ -19,7 +21,7 @@ class DefaultPouchFactory extends AbstractPouchFactory implements PouchFactory, } public getPouch(type: Construtorable, ...args: any[]): T { - let qualifier = DEFAULT_QUALIFIER; + let qualifier = validateQualifier(type.prototype, undefined); return this.getPouchByQualifier(type, qualifier, args); } diff --git a/src/ts/@loafer/pouches/factory/implement/DefaultSingletonPouchRegistry.ts b/src/ts/@loafer/pouches/factory/implement/DefaultSingletonPouchRegistry.ts index 1c48204..4fe399f 100644 --- a/src/ts/@loafer/pouches/factory/implement/DefaultSingletonPouchRegistry.ts +++ b/src/ts/@loafer/pouches/factory/implement/DefaultSingletonPouchRegistry.ts @@ -9,33 +9,44 @@ import { } from '@loafer/pouches/util/qualifier'; class DefaultSingletonPouchRegistry implements SingletonPouchRegistry { - protected singletonInstanceMap: Map>; + protected singletonInstanceMap: Map>; protected constructor() { } public registerSingleton(pouch: any, qualifier?: string | symbol): void { - let type = Object.getPrototypeOf(pouch); - const _qualifier = validateQualifier(qualifier); + let type: Object = Object.getPrototypeOf(pouch); + const _qualifier = validateQualifier(type, qualifier); - if (this.hasSingleton(type, qualifier)) { + if (this._hasSingleton(type, _qualifier)) { throw new Error(`Pouch Definition[${type.constructor.name}:${qualifier}] is exist already`); } - let map = this.singletonInstanceMap.get(type); + let map = this.singletonInstanceMap.get(_qualifier); if (undefined === map) { map = new Map(); + this.singletonInstanceMap.set(_qualifier, map); } - map.set(qualifier, pouch); + map.set(type, pouch); } - public getSingleton(type: Construtorable, qualifier?: string | symbol): T { - if (!this.singletonInstanceMap.has(type)) { + public getSingleton(target: Construtorable, qualifier?: string | symbol): T { + let type = target.prototype; + return this._getSingleton(type, qualifier); + } + public hasSingleton(target: Construtorable, qualifier?: string | symbol): boolean { + return this._hasSingleton(target.prototype, qualifier); + } + + private _getSingleton(type: Object, qualifier?: string | symbol): T { + const _qualifier = validateQualifier(type, qualifier); + + if (!this.singletonInstanceMap.has(_qualifier)) { return undefined; } - const _qualifier = validateQualifier(qualifier); - return this.singletonInstanceMap.get(type).get(qualifier); + return this.singletonInstanceMap.get(_qualifier).get(type); } - public hasSingleton(type: Construtorable, qualifier?: string | symbol): boolean { - return undefined === this.getSingleton(type, qualifier) ? false : true; + + private _hasSingleton(type: Object, qualifier?: string | symbol): boolean { + return undefined === this._getSingleton(type, qualifier) ? false : true; } } diff --git a/src/ts/@loafer/pouches/util/qualifier.ts b/src/ts/@loafer/pouches/util/qualifier.ts index be54a85..98a4beb 100644 --- a/src/ts/@loafer/pouches/util/qualifier.ts +++ b/src/ts/@loafer/pouches/util/qualifier.ts @@ -1,7 +1,3 @@ -import { - DEFAULT_QUALIFIER, -} from '@loafer/pouches/constants'; - -export const validateQualifier = (qualifier: string | symbol) => { - return undefined === qualifier ? DEFAULT_QUALIFIER : qualifier; +export const validateQualifier = (type: Object, qualifier: string | symbol) => { + return undefined === qualifier ? type.constructor.name : qualifier; }; diff --git a/src/ts/@overflow/commons/api/Service.ts b/src/ts/@overflow/commons/api/Service.ts index a276bed..f85872a 100644 --- a/src/ts/@overflow/commons/api/Service.ts +++ b/src/ts/@overflow/commons/api/Service.ts @@ -2,7 +2,7 @@ import Inject from '@loafer/pouches/decorator/Inject'; import WebSocketRPC from '../websocket/WebSocketRPC'; abstract class Service { - @Inject({required: false}) + @Inject({required: true}) private webSocketRPC: WebSocketRPC; private name: string; protected constructor(name: string) {