Skip to content

Commit

Permalink
closes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
zheksoon committed Apr 12, 2024
1 parent 8dac7bc commit 3bb326b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 30 deletions.
60 changes: 30 additions & 30 deletions src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,48 +83,48 @@ export class Container {
): InstanceOf<T> {
this.resolutionContainer = resolutionContainer || new Container();

let cls = clsOrToken as TokenOrClass;
try {
if (this.resolutionSet.has(clsOrToken)) {
throw new DependencyCycleError();
}

let scope: ScopeHandler;
let cls = clsOrToken as TokenOrClass;

let container: Container = this;
let scope: ScopeHandler;

const descriptor = this.getTokenDescriptor(clsOrToken);
let container: Container = this;

if (!descriptor) {
if (clsOrToken instanceof Token) {
throw new TokenNotRegisteredError();
}
const descriptor = this.getTokenDescriptor(clsOrToken);

cls = clsOrToken;
this.resolutionSet.add(clsOrToken);

scope = cls.scope || Scopes.Transient();
if (!descriptor) {
if (clsOrToken instanceof Token) {
throw new TokenNotRegisteredError();
}

container = this;
} else {
if ("class" in descriptor) {
cls = descriptor.class as ScopedClass;
cls = clsOrToken;

scope = descriptor.scope || cls.scope || Scopes.Transient();
scope = cls.scope || Scopes.Transient();

container = descriptor.container;
} else if ("value" in descriptor) {
return descriptor.value;
} else if ("factory" in descriptor) {
// @ts-ignore
return descriptor.factory(container, ...args);
container = this;
} else {
throw new Error("Invalid descriptor");
}
}

try {
if (this.resolutionSet.has(clsOrToken)) {
throw new DependencyCycleError();
if ("class" in descriptor) {
cls = descriptor.class as ScopedClass;

scope = descriptor.scope || cls.scope || Scopes.Transient();

container = descriptor.container;
} else if ("value" in descriptor) {
return descriptor.value;
} else if ("factory" in descriptor) {
// @ts-ignore
return descriptor.factory(container, ...args);
} else {
throw new Error("Invalid descriptor");
}
}

this.resolutionSet.add(clsOrToken);

return scope(cls, args, container, this.resolutionContainer);
} finally {
this.resolutionSet.delete(clsOrToken);
Expand Down
13 changes: 13 additions & 0 deletions test/dioma.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1521,6 +1521,19 @@ describe("Dioma", () => {

expect(value).toBeInstanceOf(TokenClass);
});

it("should throw error when there is a circular dependency", () => {
const token = new Token<string>();

container.register({
token,
factory: (container, value: string) => container.inject(token),
});

expect(() => container.inject(token, "test")).toThrowError(
DependencyCycleError
);
});
});
});
});
Expand Down

0 comments on commit 3bb326b

Please sign in to comment.