一个状态的多个“何时”条件

Multiple “when”-conditions for a State

假设我有以下状态:

State
{
    name: "red_state"
    when: color === "red"
},
State
{
    name: "blue_state"
    when: color === "blue"
},
State
{
    name: "square_state"
    when: shape === "square"
},
State
{
    name: "circle_state"
    when: shape === "circle"
},
State
{
   name: "blue_circle_state"
   when: color === "blue" && shape === "circle"
}

永远不会触发“blue_circle_state”,这是为什么?一个State有多个条件有没有更好的办法?

when中为State设置多个条件没有错,这里的问题是blue_statecircle_stateblue_circle_state所有评估同时为真。 documentation about the when property of a State 表示:

If multiple states in a group have when clauses that evaluate to true at the same time, the first matching state will be applied.

所以当颜色为蓝色且形状为圆形时,这里只有blue_state会被激活

根据您的情况,在开始时移动更具体的 blue_circle_state 将解决您的问题,但您仍然不能同时激活多个状态。

如果你想拥有更高级的状态机,你可以看看The Declarative State Machine Framework