ferrybig

Redux saga nodejs

Sep 4th, 2020
1,256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const { call, put, delay, all } = require('redux-saga/effects');
  2. const { runSaga, stdChannel }  = require("redux-saga");
  3.  
  4. // Setup
  5. const channel = stdChannel/*<never>*/();
  6. const myIO/*: RunSagaOptions<never, never>*/ = {
  7.     // this will be used to orchestrate take and put Effects
  8.     channel,
  9.     // this will be used to resolve put Effects
  10.     dispatch(output) {
  11.         throw new Error('[PUT] This saga is not setup with a store connection: ' + output);
  12.     },
  13.     // this will be used to resolve select Effects
  14.     getState() {
  15.         throw new Error('[SELECT] This saga is not setup with a store connection');
  16.     },
  17. };
  18.  
  19. // Main code
  20. function callApi(url) {
  21.     console.log(new Date() + 'Calling ' + url);
  22.     return Promise.resolve(['Yass']);
  23. }
  24.  
  25. function* callApiWithDelay(url) {
  26.     const result = yield call(callApi, url);
  27.     yield delay(5000);
  28.     return result;
  29. }
  30.  
  31. function* fetchResults() {
  32.     console.log(new Date() + 'fetchResults starting');
  33.     const listing = yield callApiWithDelay('/list')
  34.     for(const item of listing) {
  35.         yield callApiWithDelay('/res/'+item)
  36.     }
  37.     console.log(new Date() + 'fetchResults finished');
  38. }
  39. function* mainLoop() {
  40.     yield delay(100); // Startup delay so console logs make sense
  41.     while(true) {
  42.         yield all([
  43.             call(fetchResults),
  44.             //delay(1000 * 60 * 20),
  45.             delay(1000 * 7),
  46.         ]);
  47.     }
  48. }
  49.  
  50. const task = runSaga( myIO, mainLoop )
  51.  
Advertisement
Add Comment
Please, Sign In to add comment