Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <script src="https://unpkg.com/react@15.2.1/dist/react.min.js"></script>
  <script src="https://unpkg.com/react-dom@15.2.1/dist/react-dom.min.js"></script>
  <script src="https://unpkg.com/redux@^3.5.2/dist/redux.min.js"></script>
  <script src="https://unpkg.com/react-redux@4.4.5/dist/react-redux.min.js"></script>
  <script src="https://unpkg.com/@reactivex/rxjs/dist/global/Rx.js"></script>
  <script src="https://unpkg.com/redux-observable/dist/redux-observable.min.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
  <div id="root"></div>
</body>
</html>
 
console.clear();
const { of, concat } = Rx.Observable;
const AppConfig = {
  serverUrl: () => '/whatever'
};
// mocks, either success or fail
const ajax = {
  getJSON: url => of({ url }).delay(100)
  //getJSON: url => Rx.Observable.throw({ url }).delay(100)
};
const finishLoadingThings = payload => ({ type: 'finishLoadingThings', payload });
const sendNotification = payload => ({ type: 'sendNotification', payload });
const loadThingsEpic = action$ => {
  return action$.ofType('LOAD_THINGS')
    .mergeMap(({things}) => {
      const requestURL = `${AppConfig.serverUrl()}/data/things`;
      return ajax.getJSON(requestURL).switchMap(response => {
        return concat(of(finishLoadingThings(response)),
                      of(sendNotification('success')));
      });
    })
    .catch(e => {
    console.error('caught this:', e);
      return concat(of(finishLoadingThings({ things: {} })),
                    of(sendNotification('error')));
    });   
}
const reducer = (state, action) => {
  console.log(action);
};
// redux/configureStore.js
const { Provider } = ReactRedux;
const { createStore, applyMiddleware } = Redux;
const { createEpicMiddleware } = ReduxObservable;
const epicMiddleware = createEpicMiddleware(loadThingsEpic);
const store = createStore(reducer,
  applyMiddleware(epicMiddleware)
);
store.dispatch({ type: 'LOAD_THINGS' });
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
jayphelpspro
0viewers