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.

Quick-add: + add another resource

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.

Quick-add: + add another resource

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-observable 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 { createEpicMiddleware } = ReduxObservable;
const { Observable } = Rx;
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;
}
            
          
!
            
              /**
 * Polling Epic.
 */
const pollEpic = action$ =>
  action$.ofType(POLL_START)
    .switchMap(() =>
      Observable.timer(0, 4000)
        .takeUntil(action$.ofType(POLL_STOP))
        .exhaustMap(() =>
          Observable.ajax({ url: ENDPOINT, crossDomain: true }) // 1. See below
            .map(res => getDataSuccessAction(res.response))
          	.catch(error => Observable.of(getDataFailureAction(error)))
        )
    );




/**
 * 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 epicMiddleware = createEpicMiddleware(pollEpic);

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



/**
 * 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
Loading ..................

Console