[XState] Use an Interpreter to Instantiate a Machine

While it‘s powerful enough to have Machine.transition, it can get tedious constantly passing it a state and an event. It would be nice if we had a function that could take a machine, instantiate it, maintain the state of the machine, and give us the ability to send events to it.

This is where an interpreter comes in handy.

And interpreter takes the abstract machine and brings it to life. XState provides us a function, interpret, to do this. interpret returns to us a service and we can use that service to send events, subscribe to changes, and add callbacks to events such as onTransition

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

const lit = {
  // ‘on‘ keyword present events
  on: {
    TOGGLE: "unlit",
    BROKEN: "broken"
  }
};
const unlit = {
  on: {
    TOGGLE: "lit",
    BROKEN: "broken"
  }
};
const broken = {
  // you can leave it empty, the same as final state
  //type: "final"
};

const states = { lit, unlit, broken };

const lightBulb = Machine({
  id: "lightBulb",
  initial: "unlit",
  strict: true,
  states
});

const service = interpret(lightBulb)
  .onTransition(state => {
    // Side effect
    if (state.value === "broken") {
      console.log("Light bulb is borken");
      service.stop();
    }

    // check the state is changed or not
    if (state.changed) {
      console.log("changed to: ", state.value);
    }
  })
  .start(); // unlit when start

service.send("TOGGLE"); // lit after toggle

service.send("BROKEN"); // borken

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

时间: 2024-10-13 03:59:02

[XState] Use an Interpreter to Instantiate a Machine的相关文章

lua 环境探索

什么是环境? http://www.lua.org/manual/5.1/manual.html#2.9 Besides metatables, objects of types thread, function, and userdata have another table associated with them, called their environment. Like metatables, environments are regular tables and multiple

Lua 架构 The Lua Architecture

转载自:http://magicpanda.net/2010/10/lua%E6%9E%B6%E6%9E%84%E6%96%87%E6%A1%A3/ Lua架构文档(翻译) 十 102010 前段时间翻译了lua官方关于lua5架构设计的一份文档,现在分享给大家. 注意:所有版权都归lua官方所有,本人仅将其翻译为中文,以方便中文阅读者.翻译中出现任何错误导致的结果,本人不负任何责任. 如果有任何翻译错误,以及意见与建议,请email本人.邮件地址:[email protected]. 转载请注

Get start with "Lisp"

Get start with "Lisp" 在<黑客与漫画>中,Paul graham提到什么才是最好的编程语言,Lisp和C被明显的提及,对可谓是Lisp是推崇备至.最近准备看SICP,于是学一下极具逼格的Lisp. The basic, if primitive, way to get Lisp running is to install a command-line Lisp system. This works especially well if you have

Hypervisor scheduler

Techniques for configuring a?hypervisor?scheduler?to make use of cache topology of processors and physical memory distances between NUMA nodes when making scheduling decisions. In the same or other embodiments the?hypervisor scheduler?can be configur

关于解释器风格

这周我们了解了解释器风格 通过看书和查找资料对解释器相关的资料进行了整理 首先我们来了解一下什么是解释器: An interpreter is a program that executes another program (解释器是一个用来执行其他程序的程序). An interpreter implements a virtual machine, which may be different from the underlying hardware platform. (解释器针对不同的硬

各种高级语言虚拟机

RednaxelaFX 2010-01-06 关注各种高级语言虚拟机(high-level language virtual machine,HLL VM)的设计与实现,泛化至各种高级语言的运行时的设计与实现,也会涉及动态编译.GC.动态语言的编译等话题.总之大方向是高级语言的语义的实现方式就对了 >_< 我会开一些帖子作为资料堆积用,例如书籍.论文.教程.文章及相关链接收集.请不要以讨论方式回复这些资料堆积帖,以便于保持资料的“整洁”:不过可以另外开对应的讨论帖放讨论. 也欢迎大家开讨论帖对

python --001简介

python ····什么是python python是一门程序设计语言1.自然语言:人与人之间的交流(汉语,英语)2.机器语言:计算机能够读懂的语言(0,1--微码编程)3.程序设计语言:有固定的语法约束,由文字构成的文本文件(Java,c++,python)注:越接近自然语言的程序设计语言越高级,为高级程序设计语言,python是高级语言. ····Python的简史 python的创始人:Guido van Rossum喜欢马戏团:monty python (python 大蟒蛇)1989

[XState] Invoke Callbacks to Send and Receive Events from a Parent XState Machine

We can invoke a callback as a service when we enter a state in XState. This gives us the ability to trigger various functionality by responding to events sent to the service, and allows us to send events back to the parent machine. We do this by writ

[XState] Invoke Child XState Machines from a Parent Machine

Trying to structure the state logic of an application as a single machine can begin to become unwieldy when we have too many states. It is often better to divide our app into smaller machines and invoke those machines as necessary. When we enter a st