Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. const resetableEditableContext = () => {
  2. return {
  3. pushing: {
  4. updates: [],
  5. error: null,
  6. lastSuccess: null,
  7. },
  8. pulling: {
  9. error: null,
  10. lastSuccess: null,
  11. updates: [],
  12. },
  13. };
  14. };
  15.  
  16. const editableMachineError = key => {
  17. return assign({
  18. [key]: (context, event) => {
  19. // using an immutable pattern
  20. return {
  21. ...context[key],
  22. error: event.error,
  23. };
  24. },
  25. });
  26. };
  27.  
  28. const editableMachineSuccess = key => {
  29. return assign({
  30. [key]: (context, event) => {
  31. // using an immutable pattern
  32. return {
  33. ...context[key],
  34. lastSuccess: new Date().getTime(),
  35. error: null,
  36. updates: [],
  37. };
  38. },
  39. });
  40. };
  41.  
  42. // A editableMachine provides a state machine to manage a component which
  43. // can take on a viewable or editable state. More importantly when
  44. // edits do occur the process and constraints to make sure no changes are lost are provided by the state machine.
  45. const editableMachine = Machine(
  46. {
  47. id: 'editable',
  48. strict: true,
  49. initial: 'viewing',
  50. context: {
  51. canEdit: true,
  52. ...resetableEditableContext(),
  53. },
  54. states: {
  55. viewing: {
  56. on: {EDIT: {target: 'editing', cond: 'canEdit'}},
  57. },
  58. unsaved_prompt: {
  59. exit: ['resetContext'],
  60. on: {
  61. DONE: {target: 'viewing'},
  62. },
  63. },
  64. editing: {
  65. type: 'parallel',
  66. on: {
  67. DONE: [
  68. {target: 'viewing', cond: 'isSaved' /*actions: 'flush', */},
  69. {target: 'unsaved_prompt', cond: 'waitingOnSave'},
  70. ],
  71. CHANGE: {
  72. target: 'editing',
  73. actions: assign({
  74. pushing: (context, event) => {
  75. // using an immutable pattern
  76. return {
  77. ...context.pushing,
  78. updates: [...context.pushing.updates, event.change],
  79. };
  80. },
  81. }),
  82. },
  83. },
  84.  
  85. states: {
  86. pushing: {
  87. initial: 'idle',
  88. states: {
  89. idle: {
  90. on: {
  91. PUSH: {target: 'sync'},
  92. },
  93. },
  94. sync: {
  95. on: {
  96. SUCCESS: {target: 'idle', actions: ['pushingSuccess']},
  97. FAILURE: {target: 'idle', actions: ['pushingError']},
  98. },
  99. },
  100. },
  101. },
  102. pulling: {
  103. initial: 'idle',
  104. states: {
  105. idle: {
  106. on: {
  107. PULL: {target: 'sync'},
  108. },
  109. },
  110. sync: {
  111. on: {
  112. SUCCESS: {target: 'idle', actions: ['pullingSuccess']},
  113. FAILURE: {target: 'idle', actions: ['pullingError']},
  114. },
  115. },
  116. },
  117. },
  118. },
  119. // invoke: {
  120. // id: 'syncMachine',
  121. // src: syncMachine,
  122. // // Deriving child context from parent context
  123. // data: {
  124. // duration: (context, event) => context.customDuration
  125. // }
  126. // }
  127. },
  128.  
  129. // TODO: state waiting for confirmation that FORCE_DONE is ok
  130. },
  131. },
  132. {
  133. actions: {
  134. resetContext: assign({
  135. ...resetableEditableContext(),
  136. }),
  137. pushingError: editableMachineError('pushing'),
  138. pullingError: editableMachineError('pulling'),
  139. pushingSuccess: editableMachineSuccess('pushing'),
  140. pullingSuccess: editableMachineSuccess('pulling'),
  141. },
  142. guards: {
  143. canEdit: (context, event) => context.canEdit,
  144. isSaved: (context, event) =>
  145. context.pushing.updates.length === 0 && !context.error,
  146. waitingOnSave: (context, event) =>
  147. context.pushing.updates.length !== 0 &&
  148. !context.pushing.error &&
  149. !context.pulling.error,
  150. },
  151. }
  152. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement