首页 > 其他 > 详细

[XState] Delay XState Events and Transitions

时间:2020-01-21 10:14:23      阅读:77      评论:0      收藏:0      [点我收藏+]

The passing of time can be represented as an event in XState. Rather than requiring the user to send an event after an amount of time has passed, XState provides the after property.

The value of after is an object whose keys are the milliseconds that should pass before a transition is taken. Consider a stop light transitioning from yellow to red after three seconds:

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

const stopLightMachine = Machine(
  {
    id: "stopLight",
    initial: "red",
    context: {
      rushHourMultiplier: 1
    },
    states: {
      red: {
        after: { RED_TIMER: "yellow" }
      },
      yellow: {
        after: { YELLOW_TIMER: "green" }
      },
      green: {
        after: { GREEN_TIMER: "red" }
      }
    }
  },
  {
    delays: {
      RED_TIMER: context => context.rushHourMultiplier * 4000,
      YELLOW_TIMER: context => context.rushHourMultiplier * 1000,
      GREEN_TIMER: context => context.rushHourMultiplier * 3000
    }
  }
);

技术分享图片

[XState] Delay XState Events and Transitions

原文:https://www.cnblogs.com/Answer1215/p/12220245.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!