Skip to content

Instantly share code, notes, and snippets.

@ezekielchentnik
Created October 3, 2018 06:40
Show Gist options
  • Save ezekielchentnik/0a60daa651fcb239a2e59913454d7b03 to your computer and use it in GitHub Desktop.
Save ezekielchentnik/0a60daa651fcb239a2e59913454d7b03 to your computer and use it in GitHub Desktop.
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