Skip to content
This repository has been archived by the owner on Sep 10, 2022. It is now read-only.

Commit

Permalink
Use change-emitter to keep track of observers (#160)
Browse files Browse the repository at this point in the history
* Use change-emitter to keep track of observers

* Add test for multiple observers
  • Loading branch information
acdlite committed May 15, 2016
1 parent cfeffad commit 9fb8bb4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
19 changes: 19 additions & 0 deletions src/packages/rx-recompose/__tests__/mapPropsStream-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,22 @@ test('mapPropsStream renders null until stream of props emits value', t => {
props$.onNext({ foo: 'bar' })
t.is(wrapper.find('div').prop('foo'), 'bar')
})


test('handler multiple observers of props stream', t => {
const Container = mapPropsStream(props$ =>
// Adds three observers to props stream
props$.combineLatest(
props$, props$,
props1 => props1
)
)('div')

const wrapper = mount(<Container value={1} />)
const div = wrapper.find('div')

t.is(div.prop('value'), 1)
// Push onto props stream
wrapper.setProps({ value: 2 })
t.is(div.prop('value'), 2)
})
14 changes: 9 additions & 5 deletions src/packages/rx-recompose/createComponent.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { Component } from 'react'
import { Observable } from 'rx'
import { createChangeEmitter } from 'change-emitter'

const createComponent = propsToVdom =>
class RxComponent extends Component {
state = {};

propsEmitter = createChangeEmitter();

// Stream of props
props$ = Observable.create(observer => {
this.propsObserver = observer
this.propsObserver.onNext(this.props)
});
props$ = Observable.create(observer =>
this.propsEmitter.listen(props => observer.onNext(props))
);

// Stream of vdom
vdom$ = propsToVdom(this.props$);
Expand All @@ -29,6 +31,8 @@ const createComponent = propsToVdom =>
: this.setState({ vdom })
}
)

this.propsEmitter.emit(this.props)
}

componentDidMount() {
Expand All @@ -37,7 +41,7 @@ const createComponent = propsToVdom =>

componentWillReceiveProps(nextProps) {
// Receive new props from the owner
this.propsObserver.onNext(nextProps)
this.propsEmitter.emit(nextProps)
}

shouldComponentUpdate(nextProps, nextState) {
Expand Down
15 changes: 6 additions & 9 deletions src/packages/rx-recompose/createEventHandler.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import { Observable } from 'rx'
import { createChangeEmitter } from 'change-emitter'

const createEventHandler = () => {
const observers = []
const stream = Observable.create(observer => {
observers.push(observer)
return () => {
const i = observers.indexOf(observer)
observers.splice(i, 1)
}
})
const emitter = createChangeEmitter()
const stream = Observable.create(observer =>
emitter.listen(value => observer.onNext(value))
)
return {
handler: value => observers.forEach(observer => observer.onNext(value)),
handler: emitter.emit,
stream
}
}
Expand Down
1 change: 1 addition & 0 deletions src/packages/rx-recompose/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"composition"
],
"dependencies": {
"change-emitter": "^0.1.2",
"lodash": "^4.0.0"
},
"peerDependencies": {
Expand Down

0 comments on commit 9fb8bb4

Please sign in to comment.