cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <h1>redux-saga polling example</h1>
<div class="controls">
  <button class="controls-item" id="pollStartBtn">Poll Start</button>
  <button class="controls-item" id="pollStopBtn">Poll Stop</button>
  <pre class="controls-item" id="pollingState">Polling false</pre>
</div>
<div class="response">
  <h3 id="responseState"><em>Start polling to see random jokes...</em></h3>
</div>

<script>
// Set up globals
const createSagaMiddleware = ReduxSaga.default;
const { delay } = ReduxSaga;
const { take, put, call , race } = ReduxSaga.effects;
const { createStore, combineReducers, applyMiddleware } = Redux;

// Set up API endpoint to use for the example.
const ENDPOINT = 'https://08ad1pao69.execute-api.us-east-1.amazonaws.com/dev/random_joke';
</script>
            
          
!
            
              html {
  font-family: sans-serif;
  color: #333;
}

responseState {
  font-weight: 600
}

#responseState em {
  font-weight: 400
}

.controls {
  display: flex;
  margin-bottom: 20px;
}

.controls-item {
  margin-right: 20px;
}

.response {
  background-color: #eee;
  padding: 10px 20px;
}
            
          
!
            
              /**
 * Saga worker.
 */
function* pollSaga(action) {
  while (true) {
    try {
      const { data } = yield call(() => axios({ url: ENDPOINT }));
      yield put(getDataSuccessAction(data));
      yield call(delay, 4000);
    } catch (err) {
      yield put(getDataFailureAction(err));
    }
  }
}

/**
 * Saga watcher.
 */
function* watchPollSaga() {
	while (true) {
  	yield take(POLL_START);
    yield race([
      call(pollSaga),
      take(POLL_STOP)
    ]);
  }
}




/**
 * Set up of Redux and the view layer.
 */

/**
 * ACTION TYPES.
 * - Redux Boilerplate
 */
const POLL_START = 'POLL_START';
const POLL_STOP = 'POLL_STOP';
const GET_DATA_SUCCESS = 'GET_DATA_SUCCESS';
const GET_DATA_FAILURE = 'GET_DATA_FAILURE';


/**
 * Action Creators.
 * - Redux Boilerplate
 */
const pollStartAction = () => ({ type: POLL_START });
const pollStopAction = () => ({ type: POLL_STOP });
const getDataSuccessAction = payload => ({ type: GET_DATA_SUCCESS, payload });
const getDataFailureAction = payload => ({ type: GET_DATA_FAILURE, payload });


/**
 * Reducer.
 * - Redux Boilerplate
 */
const initialState = {
  data: false,
  polling: false,
};

const dataReducer = (state = initialState, action) => {
  switch (action.type) {
    case POLL_START:
      return {
        ...state,
        polling: true,
      }
    case POLL_STOP:
      return {
        ...state,
        polling: false,
      }
    case GET_DATA_SUCCESS:
      return {
        ...state,
        data: action.payload
      };
    default:
      return state;
  }
};


/**
 * Create the store.
 * - Redux Boilerplate
 */
const sagaMiddleware = createSagaMiddleware();

const createStoreWithMiddleware = applyMiddleware(sagaMiddleware)(createStore);
const store = createStoreWithMiddleware(
  dataReducer,
  window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
);

sagaMiddleware.run(watchPollSaga);


/**
 * Handle the view.
 * - Typically would be handled by something like React / Angular etc.
 */
const pollStartBtn = document.getElementById('pollStartBtn');
const pollStopBtn = document.getElementById('pollStopBtn');
const pollingState = document.getElementById('pollingState');
const responseState = document.getElementById('responseState');

pollStartBtn.addEventListener('click', () => {
  store.dispatch(pollStartAction())
});
pollStopBtn.addEventListener('click',() => {
  store.dispatch(pollStopAction());
});

store.subscribe(() => {
  const { data, polling } = store.getState();
  pollingState.textContent = `Polling ${polling}`;

  if (data) {
    responseState.innerHTML = `${data.setup}<br/>— <em>${data.punchline}</em>`;
  }
});
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console