Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. // Available variables:
  2. // - Machine
  3. // - interpret
  4. // - assign
  5. // - send
  6. // - sendParent
  7. // - spawn
  8. // - raise
  9. // - actions
  10. // - XState (all XState exports)
  11.  
  12. const fetchMachine = Machine({
  13. id: 'fetch',
  14. initial: 'idle',
  15. context: {
  16. url: null,
  17. data: null,
  18. retries: 0,
  19. maxRetries: 3
  20. },
  21. states: {
  22. idle: {
  23. on: {
  24. FETCH: 'loading',
  25. }
  26. },
  27. loading: {
  28. on: {
  29. RESOLVE: {
  30. target: 'success',
  31. actions: assign({
  32.  
  33. })
  34. },
  35. REJECT: 'failure'
  36. }
  37. },
  38. success: {
  39. on: {
  40. REFETCH: 'loading',
  41. }
  42. },
  43. failure: {
  44. on: {
  45. RETRY: {
  46. target: 'loading',
  47. cond: 'canRetry',
  48. actions: 'incrementRetry'
  49. }
  50. }
  51. }
  52. }
  53. }, {
  54. guards: {
  55. canRetry: (context, event) => {
  56. return context.retries < context.maxRetries;
  57. }
  58. },
  59. actions: {
  60. incrementRetry: assign({
  61. retries: (context, event) => context.retries + 1
  62. })
  63. }
  64. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement