码迷,mamicode.com
首页 > 其他好文 > 详细

[XState] Multiple Simultaneous States with Parallel States

时间:2020-01-20 09:39:21      阅读:70      评论:0      收藏:0      [点我收藏+]

标签:move   off   currently   stat   can   whether   height   pre   mic   

Can you walk and talk at the same time? If so, you‘ve experienced what it‘s like to be in two states at the same time. Hopefully, those two states have no influence on the other. Whether or not you talk, you can walk, and vice versa. States that occur concurrently and have no affect on the other are known as "parallel states".

Parallel states happen simultaneously. The machine is in all of the parallel states at the same time. To create a parallel state node, we set the type to ‘parallel‘ and then remove the initial state. There‘s no need for an initial property when you‘re in all the child states at the same time.

 

const { Machine } = require("xstate");

const spaceHeaterMachine = Machine({
  id: "spaceHeater",
  initial: "poweredOff",
  states: {
    poweredOff: {
      on: { TOGGLE_POWER: "poweredOn" }
    },
    poweredOn: {
      on: { TOGGLE_POWER: "poweredOff" },
      type: "parallel", // mark parallel
      states: {
        heated: {
          initial: "lowHeat",
          states: {
            lowHeat: {
              on: { TOGGLE_HEAT: "highHeat" }
            },
            highHeat: {
              on: { TOGGLE_HEAT: "lowHeat" }
            }
          }
        },
        oscillation: {
          initial: "disabled",
          states: {
            disabled: {
              on: { TOGGLE_OSC: "enabled" }
            },
            enabled: {
              on: { TOGGLE_OSC: "disabled" }
            }
          }
        }
      }
    }
  }
});

技术图片

[XState] Multiple Simultaneous States with Parallel States

标签:move   off   currently   stat   can   whether   height   pre   mic   

原文地址:https://www.cnblogs.com/Answer1215/p/12216235.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!