使用 xstate,是否可以配置一个适用于所有状态并在所有状态和子状态下以相同方式处理的事件?

Using xstate, is it possible to configure an event that is applicable under all states and is handled in the same way across all states and substates?

我是 xstate 的新手,我正在尝试在应用程序中使用它,用户可以根据父状态 and/or 子状态在应用程序中请求不同的东西。但是,无论 state/sub-state 应用程序处于什么状态,用户都应该能够发出一些请求。无论之前的状态如何,对这些事件的响应都是相同的。 如何配置此事件,以便我不必在所有 states/sub-states?

下重复定义它

是 - 选择转换的算法类似于 DOM 事件传播,因为它从叶节点搜索到根节点。

您可以在根节点(顶层)上定义转换,这将在任何状态下自然处理:

import { createMachine } from 'xstate';

const machine = createMachine({
  // ...

  // top-level transitions
  on: {
    ESC: {/* ... */}
  },
  states: {
    // ...
    someState: {
      on: {
        ESC: {/* override top-level transition */}
      }
    }
  }
});