Guest User

Untitled

a guest
Mar 21st, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. const { createStore, applyMiddleware } = require('redux');
  2.  
  3. const initialState = { count: 0 };
  4. const incrementReducer = (state = initialState, action) => {
  5. switch (action.type) {
  6. case 'INC': return { count: Math.min(10, state.count + 1) };
  7. case 'DEC': return { count: state.count - 1 };
  8. case 'RND': return { count: state.count + action.amt };
  9. default: return state;
  10. }
  11. };
  12.  
  13. const uselessMW = store => next => (action) => {
  14. console.log(action);
  15. next(action);
  16. };
  17.  
  18. const blockDec = store => next => (action) => {
  19. if (action.type !== 'DEC') {
  20. next(action);
  21. }
  22. };
  23.  
  24. const delayAction = store => next => (action) => {
  25. setTimeout(() => {
  26. next(action);
  27. }, 1500);
  28. };
  29.  
  30. const randomRequest = () => {
  31. const randomDelayTimeout = (Math.random() * 2000) + 1000;
  32. const randomDelayPromise = new Promise((resolve) => {
  33. setTimeout(resolve, randomDelayTimeout);
  34. });
  35. return randomDelayPromise
  36. .then(() => Math.floor(Math.random() * 5) - 2);
  37. };
  38. const makeRandomRequestMW = store => next => (action) => {
  39. if (action.type === 'REQ') {
  40. randomRequest()
  41. .then((amt) => {
  42. store.dispatch({ type: 'RND', amt });
  43. });
  44. } else {
  45. next(action);
  46. }
  47. };
  48.  
  49. // const getUsersMW = store => next => (action) => {
  50. // if (action.type === 'FETCH_USERS') {
  51. // store.dispatch({ type: 'FETCH_USERS_PENDING' });
  52. // getUsersFromAPI()
  53. // .then((users) => {
  54. // store.dispatch({ type: 'FETCH_USERS_SUCCESS', users });
  55. // }, (error) => {
  56. // store.dispatch({ type: 'FETCH_USERS_ERROR', error });
  57. // });
  58. // } else {
  59. // next(action);
  60. // }
  61. // };
  62.  
  63. const incrementAction = { type: 'INC' };
  64. const decrementAction = { type: 'DEC' };
  65. const requestRandomIncAction = { type: 'REQ' };
  66.  
  67. const store = createStore(incrementReducer, applyMiddleware(uselessMW, makeRandomRequestMW));
  68. console.log(store.getState());
  69. const unsubscribe = store.subscribe(() => {
  70. console.log(store.getState());
  71. });
  72.  
  73. setInterval(() => {
  74. store.dispatch(requestRandomIncAction);
  75. }, 1500);
  76.  
  77. // setInterval(() => {
  78. // store.dispatch(incrementAction);
  79. // }, 2000);
  80.  
  81. // setInterval(() => {
  82. // store.dispatch(decrementAction);
  83. // }, 3400);
  84.  
  85.  
  86. // setTimeout(unsubscribe, 10000);
Add Comment
Please, Sign In to add comment