Created
October 3, 2018 06:40
-
-
Save ezekielchentnik/0a60daa651fcb239a2e59913454d7b03 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const stream$ = new Rx.BehaviorSubject() | |
const createStore = (rootReducer, initialState) => stream$ | |
.mergeMap(ensureStream) | |
.scan(rootReducer, initialState) | |
const dispatch = (actionType, data) => stream$ | |
.next({ type: actionType, payload: data}) | |
const isStream = (value) => (value instanceof Rx.Observable) | |
const ensureStream = (value) => (isStream(value) ? action : Rx.Observable.of(value)) | |
const createAction = (actionType) => { return (data) => { dispatcher(actionType, data) } } | |
const dispatch = (actionType, data) => { | |
stream$.next({ type: actionType, payload: data}) | |
if (isStream(data)) { stream$.next(data) } | |
} | |
const fetchContacts = createAction(‘Fetch’); | |
const loadContacts = createAction(‘Load’); | |
let source = Rx.Observable | |
.fromPromise( | |
fetch(url) | |
.then(resp => resp.json()) | |
) | |
.map(list => loadContacts(list)) | |
fetchContacts(source) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment