sonntam / node-red-contrib-xstate-machine

A xstate-based state machine implementation using state-machine-cat visualization for node red.
MIT License
22 stars 8 forks source link

persisting xstate #101

Open farukfakcin opened 9 months ago

farukfakcin commented 9 months ago

Hi there,

I am using "node-red-contrib-xstate-machine" plugin for a while now. It is very enjoyable, thanks for your effort.

My flow will be used on our production line and the state of the stations on the line is managed by the plugin.

Now I have a need to to presist the xstate nodes, as the flow might be restarted from time to time.

Is there a way to persist the state of the node like mentioned in https://xstate.js.org/docs/guides/states.html#persisting-state ?

Thanks in advance, Ferhat

sonntam commented 9 months ago

Hi, thanks for bringing it up. This would indeed be a useful feature.

Thanks for providing a link to xstate docs. There are some caveats with saving and restoring state as stated in these docs but it should be no problem incorporating it into my node-red wrapper.

I think a sensible implementation would use node red‘s storage functionality. I‘ll have to look into it.

sonntam commented 9 months ago

Some care has to be taken in order to make it work correctly within subflows as well (i.e. multiple instances of the same node).

farukfakcin commented 9 months ago

Thank you for taking the feature into your plan. I wish I could help.

thanks in advance

farukfakcin commented 8 months ago

Hi, any proceedings?

sonntam commented 8 months ago

I did do some conceptual and proof-of-concept work. The feature will definately be coming. My lack of free time makes progress super slow, unfortunately.

Thank you for your patience!

farukfakcin commented 8 months ago

another solution to my problem is having an input function with a "restore" topic and a payload of required initial state. i.e. I focused on restoring rather than persisting the state.

The following changes on restartMachine

if( msg.topic === "restore" ) restartMachine( node , msg.payload);

needs a change on method signature

function restartMachine(node, initialState = undefined)

will start service with initialState

if(initialState !== undefined) service.start(initialState);

This function will be called with a "restore" topic from other flow nodes. They need access to the internal xstate status object to use as initialState laters. The node should attach the internal xstate status object to the status change payload.

let payload = { state: state.value, changed: state.changed, done: state.done, activities: state.activities, actions: state.actions, event: state.event, context: state.context, stateObj: state };

That solved my need. I may contribute to your code, thanks.