最终状态

在状态图中,你可以将状态声明为 最终状态。 最终状态表示其父状态为“完成”。 要了解更多信息,请阅读我们对 状态图的介绍中的最后状态部分

API

要指示状态节点是最终节点,请将其 type 属性设置为 'final'

  1. const lightMachine = createMachine({
  2. id: 'light',
  3. initial: 'green',
  4. states: {
  5. green: {
  6. on: {
  7. TIMER: { target: 'yellow' }
  8. }
  9. },
  10. yellow: {
  11. on: {
  12. TIMER: { target: 'red' }
  13. }
  14. },
  15. red: {
  16. type: 'parallel',
  17. states: {
  18. crosswalkNorth: {
  19. initial: 'walk',
  20. states: {
  21. walk: {
  22. on: {
  23. PED_WAIT: { target: 'wait' }
  24. }
  25. },
  26. wait: {
  27. on: {
  28. PED_STOP: { target: 'stop' }
  29. }
  30. },
  31. stop: {
  32. // 'stop' 是 crosswalkNorth' 的最终状态节点
  33. type: 'final'
  34. }
  35. },
  36. onDone: {
  37. actions: 'stopCrosswalkNorth'
  38. }
  39. },
  40. crosswalkEast: {
  41. initial: 'walk',
  42. states: {
  43. walk: {
  44. on: {
  45. PED_WAIT: { target: 'wait' }
  46. }
  47. },
  48. wait: {
  49. on: {
  50. PED_STOP: { target: 'stop' }
  51. }
  52. },
  53. stop: {
  54. type: 'final'
  55. }
  56. },
  57. onDone: {
  58. // 'stop' 是 'crosswalkEast' 的最终状态节点
  59. actions: 'stopCrosswalkEast'
  60. }
  61. }
  62. },
  63. onDone: 'green'
  64. }
  65. }
  66. });

在复合状态中,到达最终子状态节点(使用 { type: 'final' })将在内部引发该复合状态节点的 done(...) 事件(例如,"done.state. light.crosswalkEast")。 使用 onDone 相当于为此事件定义一个转换。

并行状态

当并行状态节点中的每个子状态节点都 完成 时,父并行状态节点也 完成。 当到达每个子复合节点中的每个最终状态节点时,将为并行状态节点引发 done(...) 事件。

这在建模并行任务时非常有用。 例如,下面有一个购物机,其中 useritems 表示 cart 状态的两个并行任务:

  1. const shoppingMachine = createMachine({
  2. id: 'shopping',
  3. initial: 'cart',
  4. states: {
  5. cart: {
  6. type: 'parallel',
  7. states: {
  8. user: {
  9. initial: 'pending',
  10. states: {
  11. pending: {
  12. entry: 'getUser',
  13. on: {
  14. RESOLVE_USER: { target: 'success' },
  15. REJECT_USER: { target: 'failure' }
  16. }
  17. },
  18. success: { type: 'final' },
  19. failure: {}
  20. }
  21. },
  22. items: {
  23. initial: 'pending',
  24. states: {
  25. pending: {
  26. entry: 'getItems',
  27. on: {
  28. RESOLVE_ITEMS: { target: 'success' },
  29. REJECT_ITEMS: { target: 'failure' }
  30. }
  31. },
  32. success: { type: 'final' },
  33. failure: {}
  34. }
  35. }
  36. },
  37. onDone: 'confirm'
  38. },
  39. confirm: {
  40. // ...
  41. }
  42. }
  43. });

onDone 转换只会在 'cart' 的所有子状态(例如,'user''items')都处于它们的最终状态时发生。 在购物机的情况下,一旦到达'shopping.cart.user.success''shopping.cart.items.success'状态节点,状态机将从'cart'过渡到 'confirm' 状态。

::: warning

不能在状态机的根节点上定义 onDone 转换。 这是因为 onDone 是对 'done.state.*' 事件的转换,当状态机达到最终状态时,它不能再接受任何事件。

:::

笔记

  • 最终状态节点仅指示其直接父节点已 完成。 它不会影响任何更高父节点的 完成 状态,除非在其所有子复合状态节点 完成 时。
  • 到达最终子状态的并行状态在其所有同级完成之前不会停止接收事件。 最后的子状态仍然可以通过事件退出。
  • 最终状态节点不能有任何子节点。 它们是原子状态节点。
  • 你可以在最终状态节点上指定 entryexit 动作。