Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. // Available variables:
  2. // Machine (machine factory function)
  3. // assign (action)
  4. // XState (all XState exports)
  5.  
  6. const fetchMachine = Machine({
  7. id: 'fetch',
  8. context: { attempts: 0 },
  9. initial: 'idle',
  10. states: {
  11. idle: {
  12. on: {
  13. FETCH: {
  14. target: 'pending',
  15. cond: function canFetch() {
  16. return true
  17. },
  18. }
  19. },
  20. },
  21. pending: {
  22. entry: assign({
  23. attempts: ctx => ctx.attempts + 1
  24. }),
  25. after: {
  26. TIMEOUT: 'rejected'
  27. },
  28. on: {
  29. RESOLVE: 'fulfilled',
  30. REJECT: 'rejected'
  31. }
  32. },
  33. fulfilled: {
  34. initial: 'first',
  35. states: {
  36. first: {
  37. on: {
  38. NEXT: 'second'
  39. }
  40. },
  41. second: {
  42. on: {
  43. NEXT: 'third'
  44. }
  45. },
  46. third: {
  47. type: 'final'
  48. }
  49. }
  50. },
  51. rejected: {
  52. entry: assign({
  53. ref: () => spawn(Machine({ initial: 'foo', states: {foo: {}}}))
  54. }),
  55. initial: 'can retry',
  56. states: {
  57. 'can retry': {
  58. on: {
  59. '': {
  60. target: 'failure',
  61. cond: 'maxAttempts'
  62. }
  63. }
  64. },
  65. failure: {
  66. on: {
  67. RETRY: 'pending',
  68. },
  69. type: 'final'
  70. }
  71. },
  72. on: {
  73. RETRY: 'pending'
  74. }
  75. }
  76. }
  77. }, {
  78. guards: {
  79. maxAttempts: ctx => ctx.attempts >= 5
  80. },
  81. delays: {
  82. TIMEOUT: 2000
  83. }
  84. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement