Skip to content

Instantly share code, notes, and snippets.

@bfillmer
Forked from tungd/observable-store.js
Last active August 4, 2016 21:49
Show Gist options
  • Save bfillmer/cfc1053ec8713301c42b101b9f6f92bc to your computer and use it in GitHub Desktop.
Save bfillmer/cfc1053ec8713301c42b101b9f6f92bc to your computer and use it in GitHub Desktop.
How to 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