trackTimeMachine1.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { createMachine, interpret } from "xstate";
  2. import { getPlans, setPlan, getChecks, recordCheck } from '@/services/trackTimeDuration'
  3. export const machine = createMachine(
  4. {
  5. id: "trackTime",
  6. initial: "choose",
  7. context: {
  8. hi: 'hi'
  9. },
  10. states: {
  11. choose: {
  12. on: {
  13. SLEEPING: {
  14. target: "sleeping",
  15. },
  16. MIXED: {
  17. target: "mixed",
  18. },
  19. FAST: {
  20. target: "fasting",
  21. },
  22. },
  23. },
  24. sleeping: {
  25. initial: "WAIT_TO_START",
  26. states: {
  27. WAIT_TO_START: {
  28. on: {
  29. start: {
  30. target: "ON_GOING",
  31. },
  32. },
  33. },
  34. ON_GOING: {
  35. on: {
  36. end: {
  37. target: "DONE",
  38. },
  39. },
  40. },
  41. DONE: {},
  42. },
  43. },
  44. mixed: {
  45. initial: "WAIT_TO_START",
  46. states: {
  47. WAIT_TO_START: {
  48. on: {
  49. start_fast: {
  50. target: "ON_GOING1",
  51. },
  52. },
  53. },
  54. ON_GOING1: {
  55. on: {
  56. "start_sleep": {
  57. target: "ON_GOING2",
  58. },
  59. "end fast(skip_start_sleep)": {
  60. target: "DONE",
  61. },
  62. },
  63. },
  64. ON_GOING2: {
  65. on: {
  66. "end fast(skip_end_sleep)": {
  67. target: "DONE",
  68. },
  69. "end_sleep": {
  70. target: "ON_GOING3",
  71. },
  72. },
  73. },
  74. DONE: {},
  75. ON_GOING3: {
  76. on: {
  77. end_fast: {
  78. target: "DONE",
  79. },
  80. },
  81. },
  82. },
  83. },
  84. fasting: {
  85. initial: "WAIT_TO_START",
  86. states: {
  87. WAIT_TO_START: {
  88. on: {
  89. start: {
  90. target: "ON_GOING",
  91. },
  92. },
  93. },
  94. ON_GOING: {
  95. on: {
  96. end: {
  97. target: "DONE",
  98. },
  99. },
  100. },
  101. DONE: {},
  102. },
  103. },
  104. },
  105. schema: {
  106. events: {} as
  107. | { type: "end" }
  108. | { type: "FAST" }
  109. | { type: "MIXED" }
  110. | { type: "start" }
  111. | { type: "SLEEPING" }
  112. | { type: "end_fast" }
  113. | { type: "end_sleep" }
  114. | { type: "start_fast" }
  115. | { type: "start_sleep" }
  116. | { type: "end fast(skip_end_sleep)" }
  117. | { type: "end fast(skip_start_sleep)" }
  118. | { type: "GET_STATUS"}
  119. },
  120. predictableActionArguments: true,
  121. preserveActionOrder: true,
  122. },
  123. {
  124. actions: {},
  125. services: {
  126. getPlans, setPlan, getChecks, recordCheck
  127. },
  128. guards: {},
  129. delays: {},
  130. },
  131. );
  132. const service = interpret(machine).start();
  133. export default service;