Advertisement
Guest User

Untitled

a guest
May 26th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.10 KB | None | 0 0
  1. //------------------COMMUNICATING WITH REDUX STORE----------------------
  2. // put(action) : Instructs the middleware to dispatch an action to the Store
  3. // Ex- yield put({ type: 'ADD', payload: result });
  4.  
  5. // take(pattern): the middleware to wait for a specified action on the Store.
  6. //The Generator is suspended until an action that matches pattern is dispatched.
  7. // Ex-
  8. /*
  9.  
  10. Will be dispatched from the UI
  11. dispacth({
  12. type: 'FETCH_REQUEST',
  13. url: }
  14. );
  15.  
  16. function* watchFetchRequests() {
  17. while(true) {
  18. //watch for the dispatched action FETCH_REQUEST
  19. const action = yield take('FETCH_REQUEST');
  20.  
  21. //the parent process will wait until the 'forked' process is finished
  22. yield fork(fetchUrl, action.url);
  23. }
  24. }
  25. // The worker: perform the requested task
  26. function* fetchUrl(url) {
  27. const data = yield call(fetch, url);
  28.  
  29. // data would be
  30. { type: CALL, function: fetchUrl, args: [url] }.
  31.  
  32. yield put({
  33. type: 'FETCH_SUCCESS',
  34. data
  35. });
  36. }
  37. */
  38.  
  39. //------------------------------------------------------------------------------------------
  40.  
  41. //------------------COMMUNICATING WITH API(s)----------------------
  42. // call(fn, ...args): Instructs the middleware to call the function fn with args as arguments.
  43. // Ex- yield call(fetch, '/increment-add-calls', { method: 'post' });
  44. //apply(): Alias for call
  45. //-------------------------------------------------------------------------------------------
  46.  
  47.  
  48. //------------------COMMUNICATING WITH OTHER SAGA(s)----------------------
  49. // fork(fn, ...args): Creates an Effect description that instructs the middleware to perform a call on fn
  50. // Ex: ref 22
  51. //spawn(fn, ...args): Same as fork(fn, ...args) but creates a detached task. A detached task remains independent from its parent and acts like a top-level task.
  52. /*Ex:
  53.  
  54. function* rootSaga() { |
  55. // Returns immediately with a Task object |
  56. const task = yield spawn(serverHello, 'world'); | const result0 = yield call(serverHello, 'world');
  57.  
  58. // Perform an effect in the meantime //This wont be performed in the meantime.
  59. yield call(console.log, "waiting on server result..."); | yield call(console.log, "waiting on server result...");
  60.  
  61. // Block on the result of serverHello
  62. const result = yield join(task);
  63.  
  64. // Use the result of serverHello
  65. yield call(console.log, result);
  66. }
  67.  
  68.  
  69. */
  70. //join(...tasks): Creates an Effect description that instructs the middleware to wait for the results of previously forked tasks.
  71. //Ex: ref 67
  72. //-------------------------------------------------------------------------------------------
  73.  
  74.  
  75. //------------------SAGA(s) Helpers----------------------
  76. //takeEvery(pattern, saga, ...args): allows concurrent actions to be handled
  77. //Ex: import { takeEvery } from `redux-saga/effects`
  78.  
  79. // function* fetchUser(action) {
  80. // ...
  81. // }
  82.  
  83. // function* watchFetchUser() {
  84. // yield takeEvery('USER_REQUESTED', fetchUser)
  85. // }
  86.  
  87. //internal code
  88. /*
  89. function* takeEvery(pattern, saga, ...args) {
  90. const task = yield fork(function* () {
  91. while (true) {
  92. const action = yield take(pattern)
  93. yield fork(saga, ...args.concat(action))
  94. }
  95. })
  96. return task
  97. }
  98.  
  99. */
  100. //There is no guarantee that the tasks will termiate in the same order they were started
  101. //takeLatest:
  102.  
  103.  
  104. //takeLatest(pattern, saga, ...args): Spawns a saga on each action dispatched to the Store that matches pattern.
  105. //And automatically cancels any previous saga task started previous if it's still running.
  106.  
  107. //Ex: import { takeEvery } from `redux-saga/effects`
  108.  
  109. // function* fetchUser(action) {
  110. // ...
  111. // }
  112.  
  113. // function* watchFetchUser() {
  114. // yield takeEvery('USER_REQUESTED', fetchUser)
  115. // }
  116. // Since takeLatest cancels any pending task started previously,
  117. //we ensure that if a user triggers multiple consecutive USER_REQUESTED actions rapidly,
  118. //we'll only conclude with the latest action
  119.  
  120. //internal code
  121. /*
  122. function* takeLatest(pattern, saga, ...args) {
  123. const task = yield fork(function* () {
  124. let lastTask
  125. while (true) {
  126. const action = yield take(pattern)
  127. if (lastTask)
  128. yield cancel(lastTask) // cancel is no-op if the task has already terminated
  129.  
  130. lastTask = yield fork(saga, ...args.concat(action))
  131. }
  132. })
  133. return task
  134. }
  135.  
  136. */
  137. // Much safer and reliable than takeEvery
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement