-
-
Save bfillmer/cfc1053ec8713301c42b101b9f6f92bc to your computer and use it in GitHub Desktop.
How to use Rx.Observable in-place of Redux store
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
import update from 'react/lib/update' | |
import { Observable, Subject, ReplaySubject } from 'rx' | |
const INITIAL_STATE = { | |
user: null | |
} | |
const updates = new ReplaySubject() | |
export const state = Observable.of(INITIAL_STATE) | |
.merge(updates.map(change => state => update(state, change))) | |
.scan((state, reducer) => reducer(state)) | |
.shareReplay(16) | |
export const subscribe = (path, fn) => state.pluck(path).subscribe(fn) | |
export const dispatch = (change) => updates.onNext(change) | |
// Usage | |
// dispatch({ user: { $set: { name: "Some user" } }}) | |
// subscribe('user', user => this.setState({ user }) | |
// propsToState replacement | |
// state.pluck(['user', 'selection']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment