trackTimeMachine.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import { createMachine, interpret,assign } from "xstate";
  2. export const machine = createMachine(
  3. {
  4. id: "trackTime",
  5. initial: "choose",
  6. context:{
  7. currentStatus:'',
  8. checkData:null
  9. },
  10. on: {
  11. RESET:{
  12. target:'.choose',
  13. actions:assign(_=>{
  14. return {
  15. currentStatus:''
  16. }
  17. })
  18. }
  19. },
  20. states: {
  21. choose: {
  22. on: {
  23. SLEEP: {
  24. target: "SLEEP"
  25. },
  26. FAST_SLEEP: {
  27. target: "FAST_SLEEP"
  28. },
  29. FAST: {
  30. target: "FAST"
  31. },
  32. },
  33. },
  34. SLEEP: {
  35. initial: "WAIT_FOR_START",
  36. states: {
  37. WAIT_FOR_START: {
  38. on: {
  39. START_SLEEP: {
  40. target: "ONGOING"
  41. },
  42. },
  43. },
  44. ONGOING: {
  45. on: {
  46. END_SLEEP: {
  47. target: "COMPLETED"
  48. },
  49. },
  50. },
  51. COMPLETED: {},
  52. },
  53. },
  54. FAST_SLEEP: {
  55. initial: "WAIT_FOR_START",
  56. states: {
  57. WAIT_FOR_START: {
  58. on: {
  59. START_FAST: {
  60. target: "ONGOING1"
  61. },
  62. },
  63. },
  64. ONGOING1: {
  65. on: {
  66. START_SLEEP: {
  67. target: "ONGOING2"
  68. },
  69. END_FAST: {
  70. target: "COMPLETED"
  71. },
  72. },
  73. },
  74. ONGOING2: {
  75. on: {
  76. END_FAST: {
  77. target: "COMPLETED"
  78. },
  79. END_SLEEP: {
  80. target: "ONGOING3"
  81. },
  82. },
  83. },
  84. COMPLETED: {},
  85. ONGOING3: {
  86. on: {
  87. END_FAST: {
  88. target: "COMPLETED"
  89. },
  90. },
  91. },
  92. },
  93. },
  94. FAST: {
  95. initial: "WAIT_FOR_START",
  96. states: {
  97. WAIT_FOR_START: {
  98. on: {
  99. START_FAST: {
  100. target: "ONGOING"
  101. },
  102. },
  103. },
  104. ONGOING: {
  105. on: {
  106. END_FAST: {
  107. target: "COMPLETED"
  108. },
  109. },
  110. },
  111. COMPLETED: {},
  112. },
  113. },
  114. },
  115. schema: {
  116. events: {} as
  117. | { type: "FAST" }
  118. | { type: "FAST_SLEEP" }
  119. | { type: "SLEEP" }
  120. | { type: "END_FAST" }
  121. | { type: "END_SLEEP" }
  122. | { type: "START_FAST" }
  123. | { type: "START_SLEEP" }
  124. | { type: "RESET" },
  125. },
  126. predictableActionArguments: true,
  127. preserveActionOrder: true,
  128. },
  129. {
  130. actions: {
  131. apple:assign({
  132. }),
  133. setCurrentStatus:assign((context, event) => {
  134. return {
  135. currentStatus: event.type
  136. }
  137. })
  138. },
  139. services: {},
  140. guards: {},
  141. delays: {},
  142. },
  143. );
  144. export const setSpecifiedState = (currentState, specifiedState) => {
  145. return assign({
  146. myMachine: {
  147. ...currentState.myMachine,
  148. value: specifiedState,
  149. },
  150. });
  151. };
  152. export const setSpecifiedStatus = (currentState: any, specifiedState: any) => {
  153. return assign({
  154. machine:{
  155. currentState,
  156. specifiedState
  157. }
  158. })
  159. }
  160. const service = interpret(machine).start();
  161. // const service = machine.withContext(initialContext).start();
  162. export default service;