Skip to content

Instantly share code, notes, and snippets.

@tungd
Created June 30, 2016 09:15
Show Gist options
  • Save tungd/796fbeee3448a8688cd0b6576449ea4f to your computer and use it in GitHub Desktop.
Save tungd/796fbeee3448a8688cd0b6576449ea4f to your computer and use it in GitHub Desktop.
How use Rx.Observable in-place of Redux store
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