Managing application state asynchronously
JavaScript library designed for managing application state
{
todos: [{
text: 'Eat food',
completed: true
}, {
text: 'Exercise',
completed: false
}],
visibilityFilter: 'SHOW_COMPLETED'
}
{
type: 'ADD_TODO',
text: 'Go to swimming pool'
}
{
type: 'TOGGLE_TODO',
index: 1
}
{
type: 'SET_VISIBILITY_FILTER',
filter: 'SHOW_ALL'
}
function todos(state = [], action) {
switch (action.type) {
case 'ADD_TODO':
return state.concat([{ text: action.text, completed: false }])
case 'TOGGLE_TODO':
return state.map(
(todo, index) =>
action.index === index
? { text: todo.text, completed: !todo.completed }
: todo
)
default:
return state
}
}
function fetchData(someValue) {
return (dispatch, getState) => {
dispatch({type : "REQUEST_STARTED"});
myAjaxLib.post("/someEndpoint", {data : someValue})
.then(res => dispatch({type : "REQUEST_SUCCEEDED", payload : res})
.catch(error => dispatch({type : "REQUEST_FAILED", error});
};
}
Library for
reactive programming
*Lodash for async
Declarative programming paradigm concerned with data streams
const source = Rx.Observable.create(observer => {
// Yield a single value and complete
observer.next(42);
observer.complete();
// Any cleanup logic might go here
return () => console.log('disposed')
});
const subscription = source.subscribe(
x => console.log('onNext: %s', x),
e => console.log('onError: %s', e),
() => console.log('onCompleted'));
// => next: 42
// => completed
subscription.unsubscribe();
// => disposed
import { ajax } from 'rxjs/observable/dom/ajax';
const fetchUserEpic = action$ =>
action$.ofType(FETCH_USER)
.mergeMap(action =>
ajax.getJSON(`/api/users/${action.payload}`)
.map(response => fetchUserFulfilled(response))
.takeUntil(action$.ofType(FETCH_USER_CANCELLED))
);
// epic(action$, store).subscribe(store.dispatch)