You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This seemed like a simple question but I couldn't find the answer anywhere, so apologies if it's already posted somewhere.
Anyway, I'm trying to reset a history state back to its initial value after some condition is reached. Is there any action/method I could call to do this?
What I'm trying to do
Basically I've got this mildly complicated reconnecting websocket. A caller may "close" the connection, but internally we have to drain a buffer of messages, and send a couple cleanup messages to the server before actually closing the connection. This process itself can be disrupted (read: connection lost), so that was the impetus for using a history state to return to the connected.closing state. However, when the machine actually does reach the closed state I want to reset this history state, or else subsequent attempts to "re-open" will transition to this connected.readyToClose state, which is really not what we want.
createMachine({id: "test",initial: "idle",context: {shouldClose: false,},states: {idle: {on: {REQUEST_OPEN: {target: "#test.connecting",},},},connecting: {on: {CONNECTION_OPENED: {target: "#test.connected.hist",},},},connected: {initial: "sending",states: {sending: {on: {REQUEST_CLOSE: {target: "#test.connected.closing",},SOCKET_CLOSED: {target: "#test.connecting",},},},closing: {on: {BUFFER_EMPTY: {target: "#test.connected.readyToClose",},SOCKET_CLOSED: {target: "#test.connecting",},},},readyToClose: {on: {CLOSE: {target: "#test.closed",},},},hist: {type: "history",history: "shallow",},},},closed: {entry: [({ context, self, system })=>{// I want to just reset the connected.hist history state here}],},},});
Hopefully there's a simple way to just reset this history state back to it's initial target. Otherwise I COULD take the following approaches:
Store something like isClosing: true in the context, and use a guard to transition instead of the history node. I'll probably end up doing this, but it felt gross (the connected child states are fairly locked down at this point, but if that changes, there's a chance this logic will have to change as well) .
Mark closed as a "final" state and stop the actor, then create a new actor. This could work, but then I'd have to make sure everything gets re-instantiated and re-bound as necessary. Seems like overkill.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
This seemed like a simple question but I couldn't find the answer anywhere, so apologies if it's already posted somewhere.
Anyway, I'm trying to reset a history state back to its initial value after some condition is reached. Is there any action/method I could call to do this?
What I'm trying to do
Basically I've got this mildly complicated reconnecting websocket. A caller may "close" the connection, but internally we have to drain a buffer of messages, and send a couple cleanup messages to the server before actually closing the connection. This process itself can be disrupted (read: connection lost), so that was the impetus for using a history state to return to the
connected.closing
state. However, when the machine actually does reach theclosed
state I want to reset this history state, or else subsequent attempts to "re-open" will transition to thisconnected.readyToClose
state, which is really not what we want.Hopefully there's a simple way to just reset this history state back to it's initial target. Otherwise I COULD take the following approaches:
isClosing: true
in the context, and use a guard to transition instead of the history node. I'll probably end up doing this, but it felt gross (the connected child states are fairly locked down at this point, but if that changes, there's a chance this logic will have to change as well) .closed
as a "final" state and stop the actor, then create a new actor. This could work, but then I'd have to make sure everything gets re-instantiated and re-bound as necessary. Seems like overkill.Beta Was this translation helpful? Give feedback.
All reactions