This is my situation in pseudocode:
function onRequest() {
receiveConnectionRequest();
response = function onConnect() {
playersConnected++;
if (playersConnected == 4) {
sendAllPlayersTheirCards()
}
return OK;
}();
sendResponse(response);
}
When players 1-3 connect, they get added to the list of players and OK is returned to them, at which point they will set stuff up on their side. When player 4 connects, however, before the response to his request is sent, all players are sent their cards. Because player 4 has not received a response to his request yet, he hasn't initialised correctly yet and errors upon receiving his cards.
What I'd like to have is this:
function onRequest() {
receiveConnectionRequest();
response = function onConnect() {
playersConnected++;
if (playersConnected == 4) {
plan(sendAllPlayersTheirCards())
}
return OK;
}();
sendResponse(response);
executePlanned() // now cards are sent
}
Is there a general pattern for that? The onConnect function is in a different class and should not be aware of the implementation details of onRequest.
I'm specifically trying to implement this in Java, but generic solutions are welcome.