question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to handle circular dependecy

See original GitHub issue

So 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:closed
  • Created 4 years ago
  • Comments:5

github_iconTop GitHub Comments

4reactions
unlocomqxcommented, May 26, 2019

Check the new commits, you can install using the build2 branch npm i git+https://github.com/unlocomqx/tsyringe.git#build2

There is an example in the file /src/__tests__/lazy-inject.test.ts

export interface IChicken {
}

@injectable()
@registry([{
  token   : "IEgg",
  useToken: "EggWithInterface"
}])
export class ChickenWithInterface implements IChicken {

  @lazyInject("IEgg") public egg: IEgg;

  constructor() {
  }
}
export interface IEgg { }

@injectable()
@registry([{
  token: "IChicken",
  useToken: "ChickenWithInterface"
}])
export class EggWithInterface implements IEgg {

  @lazyInject("IChicken") public chicken: IChicken;

  constructor() {
  }
}

I will try to make a PR here but it’s become a bit messy tbh 😆

1reaction
unlocomqxcommented, May 24, 2019

@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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found