Last active
November 27, 2017 18:55
-
-
Save Hypnosphi/14cb7220e42100e73c359e4e9a3596ce 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
// Rx | |
function createStore(actionsToReducers, initialState = {}) { | |
let dispatch; | |
const action$ = $.create(observer => { | |
dispatch = action => observer.onNext(action) | |
}); | |
const reducer$ = actionsToReducers(action$); | |
const state$ = reducer$ | |
.scan((state, [scope, reducer]) => | |
({ ...state, [scope]: reducer(state[scope]) }), initialState) | |
.startWith(initialState) | |
.publishReplay(1) | |
.refCount(); | |
return {state$, dispatch}; | |
} | |
// xstream | |
function createStore(actionsToReducers, initialState = {}) { | |
let dispatch; | |
const action$ = xs.create({ | |
start(listener) { | |
dispatch = action => listener.next(action) | |
} | |
}); | |
const reducer$ = actionsToReducers(action$); | |
const state$ = reducer$ | |
.fold((state, [scope, reducer]) => | |
({ ...state, [scope]: reducer(state[scope]) }), initialState); | |
return {state$, dispatch}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can you share how
actionsToReducers
function will look like?