@travetto/di
Advanced tools
Comparing version 5.0.14 to 5.0.15
@@ -0,4 +1,5 @@ | ||
import type { } from './src/global'; | ||
export * from './src/decorator'; | ||
export * from './src/error'; | ||
export * from './src/registry'; | ||
export * from './src/types'; | ||
export * from './src/types'; |
{ | ||
"name": "@travetto/di", | ||
"version": "5.0.14", | ||
"version": "5.0.15", | ||
"description": "Dependency registration/management and injection support.", | ||
@@ -30,6 +30,6 @@ "keywords": [ | ||
"dependencies": { | ||
"@travetto/registry": "^5.0.14" | ||
"@travetto/registry": "^5.0.15" | ||
}, | ||
"peerDependencies": { | ||
"@travetto/transformer": "^5.0.11" | ||
"@travetto/transformer": "^5.0.12" | ||
}, | ||
@@ -36,0 +36,0 @@ "peerDependenciesMeta": { |
@@ -17,3 +17,3 @@ import { | ||
const PrimaryCandidateⲐ = Symbol.for('@travetto/di:primary'); | ||
const PrimaryCandidateSymbol = Symbol.for('@travetto/di:primary'); | ||
@@ -59,4 +59,4 @@ const hasPostConstruct = hasFunction<{ postConstruct: () => Promise<unknown> }>('postConstruct'); | ||
// If primary found | ||
if (qualifiers.has(PrimaryCandidateⲐ)) { | ||
qualifier = PrimaryCandidateⲐ; | ||
if (qualifiers.has(PrimaryCandidateSymbol)) { | ||
qualifier = PrimaryCandidateSymbol; | ||
} else { | ||
@@ -224,4 +224,4 @@ // If there is only one default symbol | ||
this.instancePromises.get(classId)!.delete(qualifier); | ||
this.classToTarget.get(cls.Ⲑid)!.delete(qualifier); | ||
console.debug('On uninstall', { id: cls.Ⲑid, qualifier: qualifier.toString(), classId }); | ||
this.classToTarget.get(classId)!.delete(qualifier); | ||
console.debug('On uninstall', { id: classId, qualifier: qualifier.toString(), classId }); | ||
} | ||
@@ -292,4 +292,3 @@ | ||
getCandidateTypes<T, U = T>(target: Class<U>): InjectableConfig<T>[] { | ||
const targetId = target.Ⲑid; | ||
const qualifiers = this.targetToClass.get(targetId)!; | ||
const qualifiers = this.targetToClass.get(target.Ⲑid)!; | ||
const uniqueQualifiers = qualifiers ? Array.from(new Set(qualifiers.values())) : []; | ||
@@ -382,13 +381,15 @@ return castTo(uniqueQualifiers.map(id => this.get(id))); | ||
// Create mock cls for DI purposes | ||
const cls = asFull<Class>({ Ⲑid: config.id }); | ||
const fnClass = class { static Ⲑid = config.id; }; | ||
finalConfig.class = cls; | ||
finalConfig.class = fnClass; | ||
this.registerClass(cls, finalConfig); | ||
this.registerClass(fnClass, finalConfig); | ||
if (!this.factories.has(config.src.Ⲑid)) { | ||
this.factories.set(config.src.Ⲑid, new Map()); | ||
const srcClassId = config.src.Ⲑid; | ||
if (!this.factories.has(srcClassId)) { | ||
this.factories.set(srcClassId, new Map()); | ||
} | ||
this.factories.get(config.src.Ⲑid)!.set(cls, asFull(finalConfig)); | ||
this.factories.get(srcClassId)!.set(fnClass, asFull(finalConfig)); | ||
} | ||
@@ -401,6 +402,7 @@ | ||
super.onInstall(cls, e); | ||
const classId = cls.Ⲑid; | ||
// Install factories separate from classes | ||
if (this.factories.has(cls.Ⲑid)) { | ||
for (const fact of this.factories.get(cls.Ⲑid)!.keys()) { | ||
if (this.factories.has(classId)) { | ||
for (const fact of this.factories.get(classId)!.keys()) { | ||
this.onInstall(fact, e); | ||
@@ -465,25 +467,26 @@ } | ||
const targetId = config.target.Ⲑid; | ||
const targetClassId = config.target.Ⲑid; | ||
if (!this.targetToClass.has(targetId)) { | ||
this.targetToClass.set(targetId, new Map()); | ||
if (!this.targetToClass.has(targetClassId)) { | ||
this.targetToClass.set(targetClassId, new Map()); | ||
} | ||
if (config.qualifier === Symbol.for(cls.Ⲑid)) { | ||
if (config.qualifier === Symbol.for(classId)) { | ||
this.defaultSymbols.add(config.qualifier); | ||
} | ||
this.targetToClass.get(targetId)!.set(config.qualifier, classId); | ||
this.classToTarget.get(classId)!.set(config.qualifier, targetId); | ||
this.targetToClass.get(targetClassId)!.set(config.qualifier, classId); | ||
this.classToTarget.get(classId)!.set(config.qualifier, targetClassId); | ||
// If aliased | ||
for (const el of config.interfaces) { | ||
if (!this.targetToClass.has(el.Ⲑid)) { | ||
this.targetToClass.set(el.Ⲑid, new Map()); | ||
const elClassId = el.Ⲑid; | ||
if (!this.targetToClass.has(elClassId)) { | ||
this.targetToClass.set(elClassId, new Map()); | ||
} | ||
this.targetToClass.get(el.Ⲑid)!.set(config.qualifier, classId); | ||
this.classToTarget.get(classId)!.set(Symbol.for(el.Ⲑid), el.Ⲑid); | ||
this.targetToClass.get(elClassId)!.set(config.qualifier, classId); | ||
this.classToTarget.get(classId)!.set(Symbol.for(elClassId), elClassId); | ||
if (config.primary && (classId === targetId || config.factory)) { | ||
this.targetToClass.get(el.Ⲑid)!.set(PrimaryCandidateⲐ, classId); | ||
if (config.primary && (classId === targetClassId || config.factory)) { | ||
this.targetToClass.get(elClassId)!.set(PrimaryCandidateSymbol, classId); | ||
} | ||
@@ -493,3 +496,3 @@ } | ||
// If targeting self (default @Injectable behavior) | ||
if ((classId === targetId || config.factory) && (parentConfig || describeFunction(parentClass)?.abstract)) { | ||
if ((classId === targetClassId || config.factory) && (parentConfig || describeFunction(parentClass)?.abstract)) { | ||
const parentId = parentClass.Ⲑid; | ||
@@ -502,3 +505,3 @@ | ||
if (config.primary) { | ||
this.targetToClass.get(parentId)!.set(PrimaryCandidateⲐ, classId); | ||
this.targetToClass.get(parentId)!.set(PrimaryCandidateSymbol, classId); | ||
} | ||
@@ -514,6 +517,6 @@ | ||
} | ||
this.targetToClass.get(classId)!.set(PrimaryCandidateⲐ, classId); | ||
this.targetToClass.get(classId)!.set(PrimaryCandidateSymbol, classId); | ||
if (config.factory) { | ||
this.targetToClass.get(targetId)!.set(PrimaryCandidateⲐ, classId); | ||
this.targetToClass.get(targetClassId)!.set(PrimaryCandidateSymbol, classId); | ||
} | ||
@@ -524,6 +527,7 @@ | ||
const [primaryInterface] = config.interfaces; | ||
if (!this.targetToClass.has(primaryInterface.Ⲑid)) { | ||
this.targetToClass.set(primaryInterface.Ⲑid, new Map()); | ||
const primaryClassId = primaryInterface.Ⲑid; | ||
if (!this.targetToClass.has(primaryClassId)) { | ||
this.targetToClass.set(primaryClassId, new Map()); | ||
} | ||
this.targetToClass.get(primaryInterface.Ⲑid)!.set(PrimaryCandidateⲐ, classId); | ||
this.targetToClass.get(primaryClassId)!.set(PrimaryCandidateSymbol, classId); | ||
} | ||
@@ -541,3 +545,3 @@ } | ||
if (!this.classToTarget.has(cls.Ⲑid)) { | ||
if (!this.classToTarget.has(classId)) { | ||
return; | ||
@@ -544,0 +548,0 @@ } |
@@ -6,2 +6,3 @@ import ts from 'typescript'; | ||
} from '@travetto/transformer'; | ||
import { ForeignType } from '@travetto/transformer/src/resolver/types'; | ||
@@ -15,2 +16,8 @@ const INJECTABLE_MOD = '@travetto/di/src/decorator'; | ||
static getForeignTarget(state: TransformerState, ret: ForeignType): ts.Expression { | ||
return state.fromLiteral({ | ||
Ⲑid: `${ret.source.split('node_modules/')[1]}+${ret.name}` | ||
}); | ||
} | ||
/** | ||
@@ -44,3 +51,3 @@ * Handle a specific declaration param/property | ||
} else if (type.key === 'foreign') { | ||
payload.target = state.getForeignTarget(state, type); | ||
payload.target = this.getForeignTarget(state, type); | ||
} else { | ||
@@ -170,3 +177,3 @@ const file = param.getSourceFile().fileName; | ||
} else if (ret.key === 'foreign') { | ||
config.target = state.getForeignTarget(state, ret); | ||
config.target = this.getForeignTarget(state, ret); | ||
} | ||
@@ -173,0 +180,0 @@ |
47565
13
946
Updated@travetto/registry@^5.0.15