Comparing version 0.0.31 to 0.0.32
@@ -17,8 +17,8 @@ import { containerMark } from './constants.js'; | ||
inject(firstArg, ...args) { | ||
let container = this; | ||
let targetContainer = this; | ||
const lastContainer = Container.transitPath.at(-1); | ||
let tag = Tag.search(firstArg); | ||
if (!tag) { | ||
if (container.config.fallbackTag) { | ||
tag = Tag.create(container.config.fallbackTag(firstArg)); | ||
if (targetContainer.config.fallbackTag) { | ||
tag = Tag.create(targetContainer.config.fallbackTag(firstArg)); | ||
} | ||
@@ -30,16 +30,27 @@ else { | ||
let transitPathIndex; | ||
if (tag.scope === 'container') { | ||
const parentContainer = lastContainer ?? this; | ||
container = parentContainer.extend(); | ||
transitPathIndex = Container.transitPath.push(container) - 1; | ||
switch (tag.scope) { | ||
case 'container': { | ||
const parentContainer = lastContainer ?? this; | ||
targetContainer = parentContainer.extend(); | ||
transitPathIndex = Container.transitPath.push(targetContainer) - 1; | ||
break; | ||
} | ||
case 'singleton': { | ||
// eslint-disable-next-line @typescript-eslint/no-use-before-define | ||
targetContainer = container; | ||
break; | ||
} | ||
case 'transient': { | ||
if (lastContainer) { | ||
targetContainer = lastContainer; | ||
} | ||
break; | ||
} | ||
} | ||
if (tag.scope === 'transient' && lastContainer) { | ||
container = lastContainer; | ||
} | ||
let injection; | ||
if (container.inheritInjections.has(tag)) { | ||
injection = container.inheritInjections.get(tag); | ||
if (targetContainer.inheritInjections.has(tag)) { | ||
injection = targetContainer.inheritInjections.get(tag); | ||
} | ||
else if (container.injections.has(tag)) { | ||
injection = container.injections.get(tag); | ||
else if (targetContainer.injections.has(tag)) { | ||
injection = targetContainer.injections.get(tag); | ||
} | ||
@@ -52,3 +63,3 @@ else { | ||
if (inheritInjection) { | ||
container.inheritInjections.set(tag, inheritInjection); | ||
targetContainer.inheritInjections.set(tag, inheritInjection); | ||
injection = inheritInjection; | ||
@@ -58,4 +69,4 @@ } | ||
injection = tag.createValue(args); | ||
container.injections.set(tag, injection); | ||
tag.containersInUse.add(container); | ||
targetContainer.injections.set(tag, injection); | ||
tag.containersInUse.add(targetContainer); | ||
} | ||
@@ -65,3 +76,3 @@ } | ||
Object.defineProperty(injection, containerMark, { | ||
value: container, | ||
value: targetContainer, | ||
configurable: true, | ||
@@ -68,0 +79,0 @@ writable: false, |
@@ -18,3 +18,7 @@ /* eslint-disable sonarjs/constructor-for-side-effects */ | ||
const container = new ContainerMock(); | ||
class Singleton { | ||
} | ||
new TagMock({ token: Singleton, scope: 'singleton' }); | ||
class Aborter extends LinkedAbortController { | ||
singleton = container.inject(Singleton); | ||
} | ||
@@ -24,2 +28,3 @@ new TagMock({ token: Aborter, scope: 'container' }); | ||
value; | ||
singleton = container.inject(Singleton); | ||
constructor(value) { | ||
@@ -31,2 +36,3 @@ this.value = value; | ||
class A { | ||
singleton = container.inject(Singleton); | ||
aborter = container.inject(Aborter); | ||
@@ -37,2 +43,3 @@ transient = container.inject(Transient, 'dep-a'); | ||
class B { | ||
singleton = container.inject(Singleton); | ||
aborter = container.inject(Aborter); | ||
@@ -44,2 +51,3 @@ a = container.inject(A); | ||
class C { | ||
singleton = container.inject(Singleton); | ||
aborter = container.inject(Aborter); | ||
@@ -57,2 +65,8 @@ a = container.inject(A); | ||
expect(c.transient).toBeInstanceOf(Transient); | ||
expect(c.singleton).toBeInstanceOf(Singleton); | ||
expect(c.a.singleton).toBeInstanceOf(Singleton); | ||
expect(c.b.singleton).toBeInstanceOf(Singleton); | ||
expect(c.b.a.singleton).toBeInstanceOf(Singleton); | ||
expect(c.aborter.singleton).toBeInstanceOf(Singleton); | ||
expect(c.transient.singleton).toBeInstanceOf(Singleton); | ||
expect(c.a.aborter).toBeInstanceOf(Aborter); | ||
@@ -71,6 +85,11 @@ expect(c.a.transient).toBeInstanceOf(Transient); | ||
expect(c.b.a.aborter).toBe(c.aborter); | ||
expect(c.b.a.transient.singleton).toBe(c.singleton); | ||
}); | ||
it('complex test (destroy)', () => { | ||
const container = new ContainerMock(); | ||
class Singleton { | ||
} | ||
const tagSingleton = new TagMock({ token: Singleton, scope: 'singleton' }); | ||
class Aborter extends LinkedAbortController { | ||
singleton = container.inject(Singleton); | ||
} | ||
@@ -80,2 +99,3 @@ const tagAborter = new TagMock({ token: Aborter, scope: 'container' }); | ||
value; | ||
singleton = container.inject(Singleton); | ||
constructor(value) { | ||
@@ -90,2 +110,3 @@ this.value = value; | ||
class A { | ||
singleton = container.inject(Singleton); | ||
aborter = container.inject(Aborter); | ||
@@ -96,2 +117,3 @@ transient = container.inject(Transient, 'dep-a'); | ||
class B { | ||
singleton = container.inject(Singleton); | ||
aborter = container.inject(Aborter); | ||
@@ -103,2 +125,3 @@ a = container.inject(A); | ||
class C { | ||
singleton = container.inject(Singleton); | ||
aborter = container.inject(Aborter); | ||
@@ -125,2 +148,3 @@ a = container.inject(A); | ||
expect(cContainer.isEmpty).toBe(true); | ||
expect(tagSingleton.containersInUse.size).toBe(1); | ||
expect(tagAborter.containersInUse.size).toBe(0); | ||
@@ -131,3 +155,5 @@ expect(tagTransient.containersInUse.size).toBe(0); | ||
expect(tagC.containersInUse.size).toBe(0); | ||
// expect([...tagSingleton.containersInUse.values()][0]).toBe(container); | ||
console.info('fff', container.parent); | ||
}); | ||
}); |
{ | ||
"name": "mobidic", | ||
"version": "0.0.31", | ||
"version": "0.0.32", | ||
"keywords": [], | ||
@@ -5,0 +5,0 @@ "author": "js2me", |
Sorry, the diff of this file is not supported yet
27084
507