Skip to content

Commit 3bb326b

Browse files
committed
closes #2
1 parent 8dac7bc commit 3bb326b

File tree

2 files changed

+43
-30
lines changed

2 files changed

+43
-30
lines changed

src/container.ts

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -83,48 +83,48 @@ export class Container {
8383
): InstanceOf<T> {
8484
this.resolutionContainer = resolutionContainer || new Container();
8585

86-
let cls = clsOrToken as TokenOrClass;
86+
try {
87+
if (this.resolutionSet.has(clsOrToken)) {
88+
throw new DependencyCycleError();
89+
}
8790

88-
let scope: ScopeHandler;
91+
let cls = clsOrToken as TokenOrClass;
8992

90-
let container: Container = this;
93+
let scope: ScopeHandler;
9194

92-
const descriptor = this.getTokenDescriptor(clsOrToken);
95+
let container: Container = this;
9396

94-
if (!descriptor) {
95-
if (clsOrToken instanceof Token) {
96-
throw new TokenNotRegisteredError();
97-
}
97+
const descriptor = this.getTokenDescriptor(clsOrToken);
9898

99-
cls = clsOrToken;
99+
this.resolutionSet.add(clsOrToken);
100100

101-
scope = cls.scope || Scopes.Transient();
101+
if (!descriptor) {
102+
if (clsOrToken instanceof Token) {
103+
throw new TokenNotRegisteredError();
104+
}
102105

103-
container = this;
104-
} else {
105-
if ("class" in descriptor) {
106-
cls = descriptor.class as ScopedClass;
106+
cls = clsOrToken;
107107

108-
scope = descriptor.scope || cls.scope || Scopes.Transient();
108+
scope = cls.scope || Scopes.Transient();
109109

110-
container = descriptor.container;
111-
} else if ("value" in descriptor) {
112-
return descriptor.value;
113-
} else if ("factory" in descriptor) {
114-
// @ts-ignore
115-
return descriptor.factory(container, ...args);
110+
container = this;
116111
} else {
117-
throw new Error("Invalid descriptor");
118-
}
119-
}
120-
121-
try {
122-
if (this.resolutionSet.has(clsOrToken)) {
123-
throw new DependencyCycleError();
112+
if ("class" in descriptor) {
113+
cls = descriptor.class as ScopedClass;
114+
115+
scope = descriptor.scope || cls.scope || Scopes.Transient();
116+
117+
container = descriptor.container;
118+
} else if ("value" in descriptor) {
119+
return descriptor.value;
120+
} else if ("factory" in descriptor) {
121+
// @ts-ignore
122+
return descriptor.factory(container, ...args);
123+
} else {
124+
throw new Error("Invalid descriptor");
125+
}
124126
}
125127

126-
this.resolutionSet.add(clsOrToken);
127-
128128
return scope(cls, args, container, this.resolutionContainer);
129129
} finally {
130130
this.resolutionSet.delete(clsOrToken);

test/dioma.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,6 +1521,19 @@ describe("Dioma", () => {
15211521

15221522
expect(value).toBeInstanceOf(TokenClass);
15231523
});
1524+
1525+
it("should throw error when there is a circular dependency", () => {
1526+
const token = new Token<string>();
1527+
1528+
container.register({
1529+
token,
1530+
factory: (container, value: string) => container.inject(token),
1531+
});
1532+
1533+
expect(() => container.inject(token, "test")).toThrowError(
1534+
DependencyCycleError
1535+
);
1536+
});
15241537
});
15251538
});
15261539
});

0 commit comments

Comments
 (0)