I have a state machine that, as a side effect of going into a certain state, sends a message to a remote server. In some situations, however, I don't want the state machine to send that message, even if it ends up in that specific state. The state machine itself doesn't know when such a situation occurs because that depends on external variables. How can I influence the execution of that side effect? Do I need to make the state machine become aware of those external variables? Or should the sending of the message not be a direct side effect of the state machine?
To give some more context: my state machine represents a login procedure where one of the states is 'authorized'. When the state machine goes into or leaves the 'authorized' state, a message needs to be sent to a remote server. I have multiple of these state machines running in parallel, each tracking the login state of different users.
If an event applies to only a single state machine, it is ok that the state machine sends the message to the server.
handle_event_x() {
state_machine.handle_event_x(); // ok if implicitly send message
}
If an event applies to multiple state machines, each of them implicitly sends the message.
handle_event_x() {
state_machine_1.handle_event_x(); // implicitly sends message
state_machine_2.handle_event_x(); // implicitly sends message
state_machine_3.handle_event_x(); // implicitly sends message
...
}
The message infrastructure, however, supports and encourages combining multiple messages. In order to do that, I need to somehow block the state machine from sending that message and send one for all state machines combined.
handle_event_x() {
state_machine_1.handle_event_x(); // block sending message
state_machine_2.handle_event_x(); // block sending message
state_machine_3.handle_event_x(); // block sending message
...
send_combined_message();
}
How can I do this if the state machine itself isn't aware of whether it is handled alone or in a group? Should the message sending not be part of the state machine action in the first place?