Implementing TypeScript, I've been working on setting up a concept called a "trigger" : an object that includes both a checker function (which returns a Boolean) and a behavior function capable of performing various tasks. My aim is to execute the checker function of the trigger every time a specific variable's value changes. If the checker returns true, then the behavior function should also run.
Naturally, I placed the call to the trigger's checker function in a setter :
set health(value: number) {
this._health = value;
runTriggers();
}
The objective of a trigger's behavior function is to perform actions, including modifying values of variables. However, a problem arises when a trigger is invoked in the health setter as shown above, and the behavior function of the trigger alters the health variable (leading to another invocation of the setter), causing an endless recursive loop.
How can I prevent this infinite recursion while still allowing the trigger's behavior function to freely modify variables?