The right question is part of the answer. My question concerns a special case when widgets are not only representations of data, but rather input devices for the general part of information.
The event manager, as suggested by Matteo Millore, has a different application. This is useful in cases where the flow of information is more linear: on the one hand, one or more objects that can trigger an event and other objects that listen to these events.
In my case, not only event management should be centralized, but more importantly, as well as logic management. This logic is characterized by several actuators acting on the same data source, which can easily cause loops. In my case, this data source: where is the focus, and when the editor should be activated / deactivated.
The solution to preventing loops is to use an internal state variable and carefully develop a mapping that translates each state + combination of events into an action state + new. A basic implementation might look like this:
switch (eventdescription) { case 'click_in_txt': switch (state) { case 'inactive': activate(); state = 'active'; break; case 'plugin_has_focus'; close_plugin(); state = 'active' break; default: console.log('undefined situation ' + state + ' / ' + eventdescription); } ... }
This approach still requires trial and error, but itβs easy to understand what situation is causing the error, and then you can change the behavior only for this situation. In addition, the console.log () function shows where you missed some combinations of events that might cause unexpected behavior.
source share