Skip to content

Instantly share code, notes, and snippets.

@Hypnosphi
Last active November 27, 2017 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hypnosphi/14cb7220e42100e73c359e4e9a3596ce to your computer and use it in GitHub Desktop.
Save Hypnosphi/14cb7220e42100e73c359e4e9a3596ce to your computer and use it in GitHub Desktop.
// 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};
}
@dredzone
Copy link

can you share how actionsToReducers function will look like?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment