Everyone here seems to say that cyclic dependencies are bad. This is correct in a way, and I try to avoid static cyclic dependencies in almost all costs. You can do this with inverse control, as described in this blog: http://blog.schauderhaft.de/2011/07/17/breaking-dependency-cylces/
But what you are describing is not required for static cyclic dependency, but at runtime. I'm not quite sure, but I think it is more or less impossible to avoid circular dependencies at runtime. But, of course, this should not lead to endless cycles. To fix those, I see two and a half options
Hack first
Make sure that each event triggered by another event has a link to the original event (or significant information about it, as an identifier). When you process an event, make sure that it does not come from you.
Pro: easy to implement; completely prevents recursion
The other half of the hack
If you work synchronously, you can set the firingEvent flag before and reset it after. Ignore events that occur during the installation of firingEvent .
Pro: even easier to implement; completely prevents recursion when working in a single thread
Semantic rich decision
I am convinced that the event that A fires on some external trigger, and the event that fires A, because the fire C are really two different events, or all three events are actually only one that can arise from another unknown source D. Or something like that. It is impossible to speak without information about what A, B and C are and what events they shoot. If you find the right events, the loop will disappear.
Pro: The design will be cleaner and contain more information.
Jens schauder
source share