我已经玩过部分内容@xstate/graph
,这个例子与我想要的一样接近:
import { Machine } from "xstate"
import { getShortestPaths } from '@xstate/graph'
export const feedbackMachine = Machine({
id: 'feedback',
initial: 'question',
states: {
question: {
on: {
CLICK_GOOD: 'thanks',
CLICK_BAD: 'form',
CLOSE: 'closed',
ESC: 'closed',
REMOVE_ME: 'nope'
}
},
form: {
on: {
SUBMIT: 'thanks',
CLOSE: 'closed',
ESC: 'closed'
}
},
nope: {
on: {
}
},
thanks: {
on: {
CLOSE: 'closed',
ESC: 'closed'
}
},
closed: {
type: 'final'
}
}
})
const showPaths = () => {
const paths = getShortestPaths(feedbackMachine)
const states = Object.keys(paths).filter(k => paths[k].weight > 0)
console.log(states)
// [""thanks"", ""form"", ""closed"", ""nope""]
}
showPaths()
从问题状态中的可能转换中删除REMOVE_ME: 'nope'
后,您应该看到结果为:[""thanks"", ""form"", ""closed""]
showPaths
我还看到 getEdges 最近已被删除,@xstate/graph
所以我暂时不要使用它。