[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 writing a "callback handler" and setting it as the src of our invoked service. A callback handler is a function that receives the current context and the event object that triggered the invocation. This function returns another function that receives two functions as arguments. A callback function to send events to the parent machine, and an onEvent function for the handler to respond to events sent to the handler.

The way events are sent to the callback handler is by utilizing the options argument of the send action creator. We identify where we send events to using the to property, and setting the value to the id of our service.

const handlerEchoCallback = (context, event) => {
  return (callback, onReceive) => {
    onReceive(e => {
      if (e.type === ‘FOO‘) {
       callback(‘ECHO‘); // call the ‘ECHO‘ action
      }
    })
  }
}

const callbackMachine = Machine({
  id: ‘callbackMachine‘,
  initial: ‘listening‘,
  states: {
    listening: {
      invoke: {
        src: handlerEchoCallback,
        id: ‘handlerEchoCallback‘
      },
      on: {
        SPEAK: {
          actions: send(‘FOO‘, {
            to: ‘handlerEchoCallback‘
          })
        },
        ECHO: {
          actions: (context, event) => {
            console.log(‘echo‘, context, event);
          }
        }
      }
    }
  }
})

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

时间: 2024-10-01 07:03:54

[XState] Invoke Callbacks to Send and Receive Events from a Parent XState Machine的相关文章

[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

Send or receive files via Xshell

1. install lrzsz $ sudo apt-get install lrzsz 2. If you want to send file from your pc to pi, just drag it to the Xshell window. Or, input $ sudo rz then select the file you want to send. 3. If you want to receive file from pi, input $ sudo sz file_y

C# Socket服务端和客户端互相send和receive

服务端 1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using System.Net.Sockets; 5 using System.Net; 6 using System.Threading; 7  8 namespace Controller 9 {10     public static class SocketServer11     {12         private stat

和Keyle一起学StrangeIoc – Extensions

Strange: the IoC framework for Unity Extensions You may have heard that Strange is a Dependency Injection framework. I'm a little uncomfortable with that description. Sure, Strange offers DI and it's a great use, but the core of the framework - as I'

临时

This work was partially performed when the first author was a visitor at NCSU, supported by a fellowship from the University of Pisa and MURST, Italy. This work was supported in part by NSF grant CCR-9320992 Design of a Toolset for Dynamic Analysis o

Introduction to Distributed Algorithms

Gerard Tel @ Cambridge University Press 1994, 2000 ref: Distributed Algorithms for Message-Passing Systems 1 Introduction: Distributed Systems 1.1 What is a Distributed System? 1.1.1 Motivation The characteristics of a distributed system (1) Informat

[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

一种构造WEB服务器端recv和send接口阻塞现象的方法

send阻塞 socket recv send接口阻塞,会导致服务器端不在响应客户端任何请求,所以一般情况, 会将socket设置为非阻塞状态, 但是有些场景,例如ssl_accept就需要使用阻塞的socket,否则握手极容易失败, 但是一直阻塞,容易导致服务器端DOS现象. 下面是阻塞的解释 http://blog.csdn.net/xiaofei0859/article/details/6037814 比如你调用send函数发送一定的Byte,在系统内部send做的工作其实只是把数据传输(

接口处理篇 accept bind connect atan2 htons inet_addr inet_aton inet_ntoa listen ntohl recv send sendto socket

accept(接受socket连线) 相关函数 socket,bind,listen,connect 表头文件 #include<sys/types.h> #include<sys/socket.h> 定义函数 int accept(int s,struct sockaddr * addr,int * addrlen); 函数说明 accept()用来接受参数s的socket连线.参数s的socket必需先经bind().listen()函数处理过,当有连线进来时 accept()