[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 state in a machine, we can invoke a child machine. This machine can send and receive events from the parent. When the child machine reaches a final state, the onDone transition is taken by the parent‘s current state node.

To invoke a child machine, we use the invoke property and set the src property to a machine. We can forward events in the parent machine to the child machine by setting the property autoForwards to true. We can also send events to the child machine through setting the second argument of the send() function to { to: ‘child-machine-id‘ }.

The child machine can send events to the parent machine by using the sendParent function. In this way, parent and child machines can communicate.

const childMachine = Machine({
  id: ‘child‘,
  initial: ‘step1‘,
  states: {
    step1: {
      on: { STEP: ‘step2‘ },
    },
    step2: {
      on: { STEP: ‘step3‘ },
    },
    step3: {
      type: ‘final‘,
    }
  }
})

const parentMachine = Machine({
  id: ‘parent‘,
  initial: ‘idle‘,
  states: {
    idle: {
      on: { ACTIVATE: ‘active‘ },
    },
    active: {
      invoke: {
        id: ‘child‘,
        src: childMachine,
        onDone: ‘done‘,
      },
      on: {
        STEP: {
          actions: send(‘STEP‘, { to: ‘child‘ }),
        },
      },
    },
    done: {},
  },
})

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

时间: 2024-10-13 03:58:44

[XState] Invoke Child XState Machines from a Parent Machine的相关文章

[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

Add the Multiple machines for the PVS Machine Catalog

1. Create the PVS machine catalog: New-BrokerCatalog -AllocationType Random -CatalogKind PVS -Description "PVS Catalog Desc" -Name "PVS Catalog Name" -PvsAddress "[email protected]" -PvsDomain "xf.com" -PvsForVM $($

bug_ _fragment_“The specified child already has a parent. You must call removeView"的解决以及产生的原因

这个异常的出现往往是因为非法使用了某些方法引起的. 从字面意思上是说这个特定的child已经有一个parent了,你必须在这个parent中首先调用removeView()方法,才能继续你的内容.这里很明显这个child是一个View,一个子(child)View必须依赖于父(parent)View,如果你要使用这个child,则必须通过parent,而你如果就是硬想使用这个child,那么就得让这个child与parent脱离父子关系(即removeView()) 何时会出现这种异常呢,典型的

Child <- many-to-one ->Parent

网上找到个描述的很精妙的例子 Child   <-   many-to-one   ->Parent         class   Child   {         private   Parent   parent;             public   Parent   getParent   (){             return   this.parent;//访问了实例变量         }             }         class   Parent  

关于异常“The specified child already has a parent. You

在android开发过程中,有时会在不同情况下遇到同种问题: [java] view plaincopy java.lang.IllegalStateException The specified child already has a parent. You must call removeView() on the child's parent first. 也就是非法状态异常,它说这个特定的child已经有一个parent了,你必须在这个parent中首先调用removeView()方法,

spring中&lt;bean&gt;中parent标签的使用

简介:spring 中parent标签是指:某个<bean>的父类.这个类可以覆盖parent的属性, 代码如下: Parent类的代码如下: package com.timo.domain; public class Parent { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } Child的代码如下:

Methods and systems to control virtual machines

Methods and systems are provided to control the execution of a virtual machine (VM). A VM Monitor (VMM) accesses VM Control Structures (VMCS) indirectly through access instructions passed to a processor. In one embodiment, the access instructions inc

the Laser Engraving Machines ensure accurate

(FCX) - Company Profile and SWOT Analysis" which contains in depth information and data about the company and its operations also contains a company overview, key facts, major products . A lack of proper means of transportation would denote everythin

PatentTips - Method to manage memory in a platform with virtual machines

BACKGROUND INFORMATION Various mechanisms exist for managing memory in a virtual machine environment. A virtual machine platform typically executes an underlying layer of software called a virtual machine monitor (VMM) which hosts one to many operati