statelyai / xstate

Actor-based state management & orchestration for complex app logic.
https://stately.ai/docs
MIT License
27.14k stars 1.25k forks source link

Detecting Final State without starting #328

Closed timoxley closed 5 years ago

timoxley commented 5 years ago

Feature request

Originally discussed here: https://spectrum.chat/statecharts/general/detecting-state-machine-is-done~56dda5bc-8031-4840-96d7-f0458cca12d6

I would like to be able to filter my persisted state machines to only those which have not reached a final state. i.e. no point starting a machine which has already reached its final state. Currently there doesn't appear to be a good way to do this without starting the machine.

Perhaps it could be a method on the State prototype e.g.:

const state = State.create(data)
if (state.isFinal()) {
  interpret(machine).start(state)
}

However this would prevent filtering db records without instantiating State for each of them. A serialisable flag would be preferable e.g.:

if (data.isFinal) {
  interpret(machine).start(data)
}

// queryable from the db level:
const datas = await db.query({ isFinal: false })
datas.forEach(data => interpret(machine).start(data))
davidkpiano commented 5 years ago

Bikeshedding:

state.done; // true or false

// might be confusing with type: 'final' for individual state nodes
state.final; // true or false
timoxley commented 5 years ago

state.done 👍

I guess this also matches up with onDone