Check Circular Dependencies of Classes

I’d like to create React objects which reference one another (the text in one depends on the others). The user can control dependencies and could potentially create a circular dependency like this one:

class A{
  getB(){
    var b=new B
    return b.getA()
  }
}

class B{
  getA(){
    var a=new A
    return a.getB()
  }
}

var a=new A
a.getB()

As is, it gives the error:
Uncaught RangeError: Maximum call stack size exceeded.

I do want it to alert the user that there is a circular dependency. However, I would like it do this without it looping until it hits the max (I imagine this could lead to speed issues or similar).

Is it possible to check for circular dependencies?

It sounds like you want them to share a piece of state, and if that is the case you should have a parent component hold the state, and hand it down as props

To clarify, for the real thing, the text in one component would be computed based on the text in others (think of something like Excel). These dependencies can also be altered. These component would be part of a table, so the table could be the parent component. However, I’m not sure how using that approach of handing down props would allow me to check for circular dependencies.

I’m assuming you mean dependencies as in they rely on the content of what is in another field, is that correct?

Yes, that’s what I meant. To give a very simple example (there are more complex relations), if one element is 1+4, another element may just evaluate that one, so return 5.

Could you give me an example on how they might create a circular reference because it sounds like they are able to write code of some sort in these fields and if they are how are you evaluating the code?

The site is meant to define and solve systems of equations. For example:

Eqns1: a+b=3,a=2b           a+b=3,a=2b
Eqns2: solve Eqns1          a=2,b=1

A circular reference might look like this:

Eqns1: Eqns2 
Eqns2: solve Eqns1  

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.