How to handle circular dependecy
See original GitHub issueSo let’s say I have first class TestA that has dependency to class TestB
// TestA.ts
import { inject, injectable } from "tsyringe";
import { ITestB } from "./TestB";
export interface ITestA {
helloA(): void;
waveToB(): void;
}
@injectable()
export class TestA implements ITestA {
constructor(@inject("ITestB") private testB: ITestB) { }
/**
* helloA
*/
public helloA(): void {
console.log("Hello from class A");
}
public waveToB(): void {
console.log("waving to B");
this.testB.helloB();
}
}
and then I have second class TestB that has dependency to TestA:
// TestB.ts
import { inject, injectable } from "tsyringe";
import { ITestA } from "./TestA";
export interface ITestB {
helloB(): void;
}
@injectable()
export class TestB implements ITestB {
constructor(@inject("ITestA") private testA: ITestA) { }
/**
* helloB
*/
public helloB(): void {
console.log("Hello from class B");
}
public waveToA(): void {
console.log("waving to A");
this.testA.helloA();
}
}
And finally main:
import { container } from "tsyringe";
import { TestA } from "./lib/TestA";
import { TestB } from "./lib/TestB";
const testA = container.resolve(TestA);
testA.helloA();
testA.waveToB();
const testB = container.resolve(TestB);
testB.helloB();
Which upon running results in “Maximum call stack size exceeded” and crashes. Is there any solution to this without restructuring the code? (sometimes it is not that simple). I’ve seen probable solution by @unlocomqx : https://github.com/microsoft/tsyringe/issues/20 but I’m unable to compile it due to errors.
Issue Analytics
- State:
- Created 4 years ago
- Comments:5
Top Results From Across the Web
How to Eliminate Circular Dependencies from Your JavaScript ...
In my experience, the best way to deal with circular dependencies is to avoid them altogether. Circular dependencies are usually an ...
Read more >How to solve circular dependency?
Try identify your objects going backwards asking what (object) do you need for previous one to work - for example: File(filename).write(Report); ...
Read more >Circular Dependencies in Spring
A simple way to break the cycle is by telling Spring to initialize one of the beans lazily. So, instead of fully initializing...
Read more >How to fix nasty circular dependency issues once and for ...
First, this is ugly and doesn't scale. In a large code base, this will result in moving imports randomly around until stuff just...
Read more >Circular dependency
In software engineering, a circular dependency is a relation between two or more modules which either directly or indirectly depend on each other...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Check the new commits, you can install using the
build2
branchnpm i git+https://github.com/unlocomqx/tsyringe.git#build2
There is an example in the file
/src/__tests__/lazy-inject.test.ts
I will try to make a PR here but it’s become a bit messy tbh 😆
@elerocks check the
lazy
branch in my fork, I added the “lazyInject” decorator to solve the circular dep issue https://github.com/unlocomqx/tsyringe/tree/lazy You’ll need to remove the injection from the constructor and write it as a property. Example:@lazyInject("DesignManager") protected designManager: DesignManager;
I didn’t test it with interfaces though, but you could install with npm like this if you’d like to test it
npm i git+https://github.com/unlocomqx/tsyringe.git#build