Created
July 23, 2016 19:40
-
-
Save tusharmath/f35f83e18ea8733d5bb07329aaf11bb4 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
import {Component} from 'react' | |
import {Subject, Observable as O} from 'rx' | |
import R from 'ramda' | |
const isFunction = R.compose(R.equals('Function'), R.type) | |
const isObservable = i => i instanceof O | |
const toObservable = i => { | |
const canSkip = R.anyPass([isFunction, isObservable]) | |
return canSkip(i) ? i : O.just(i) | |
} | |
const toProp$ = R.map(toObservable) | |
export default class SmartComponent extends Component { | |
constructor () { | |
super() | |
const __subject = new Subject() | |
this.fromCB = R.curry((name, value) => __subject.onNext([name, value])) | |
this.select = (name) => __subject.filter(x => x[0] === name).map(x => x[1]) | |
} | |
componentWillMount () { | |
const props = this.props | |
if (this.init) { | |
const prop$ = toProp$(props) | |
const {intent$, state$} = this.init.call(null, prop$, R.pickAll(['select'], this)) | |
if (state$) this.__disposable = state$.subscribe(state => this.setState(state)) | |
if (props.onIntent && intent$) props.onIntent(intent$) | |
} | |
} | |
componentWillUnMount () { | |
if (this.__disposable) { | |
this.__disposable.dispose() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment