Skip to content

Commit 307e4a2

Browse files
committed
unsubscribe sink when component unmounted
1 parent 9215dd8 commit 307e4a2

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

lib/__tests__/react-most-test.jsx

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ describe('react-most', () => {
251251
})
252252
})
253253

254-
describe('convension default to `action` field in sinks', ()=>{
254+
describe('ERROR', ()=>{
255255
const Counter = connect(intent$=>{
256256
return {
257257
sink$: intent$.map(intent=>{
@@ -263,7 +263,7 @@ describe('react-most', () => {
263263
}
264264
}),
265265
actions: {
266-
throwExeption: ()=>({type: 'exception'})
266+
throwExeption: ()=>({type: 'exception'}),
267267
},
268268
}
269269
})(CounterView)
@@ -283,4 +283,47 @@ describe('react-most', () => {
283283
})
284284
})
285285
})
286+
287+
describe('unsubscribe when component unmounted', ()=>{
288+
it('unsubscribe', (done)=>{
289+
const Counter = connect(intent$=>{
290+
let incForever$ = most.periodic(100, {type:'inc'}).map(intent=>{
291+
done.fail('should not send intent any more')
292+
return _=>_
293+
})
294+
return {
295+
incForever$,
296+
sink$: intent$.map(intent=>{
297+
switch(intent.type) {
298+
case 'inc':
299+
return state=>({count:state.count+1})
300+
default:
301+
return state=>state
302+
}
303+
})
304+
}
305+
})(CounterView)
306+
307+
const TogglableMount = React.createClass({
308+
getInitialState(){
309+
return {
310+
mount: true
311+
}
312+
},
313+
render(){
314+
return this.state.mount && <Counter history={true} />
315+
}
316+
})
317+
spyOn(console, 'error')
318+
let counterWrapper = TestUtils.renderIntoDocument(
319+
<Most >
320+
<TogglableMount />
321+
</Most>
322+
)
323+
let toggle = TestUtils.findRenderedComponentWithType(counterWrapper, TogglableMount)
324+
let counter = TestUtils.findRenderedComponentWithType(counterWrapper, Counter)
325+
toggle.setState({mount:false})
326+
done()
327+
})
328+
})
286329
})

lib/engine/most.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ export default function Engine() {
1414
console.error('There is Error in your reducer:',e, e.stack)
1515
return of(x=>x)
1616
})
17-
.observe(f);
17+
.subscribe({
18+
next:f,
19+
error: (e)=>console.error('Something is Wrong:',e, e.stack),
20+
});
1821
}
1922
historyStream.travel = travelStream;
2023
return {intentStream, flatObserve, historyStream}

lib/react-most.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export function connect(main, initprops={}) {
5454
this.setState(state=>pick(keys(state), nextProps))
5555
}
5656
componentDidMount(){
57-
this.context[EACH_FLATMAP](this.sink$, (action)=>{
57+
this.subscribtions = this.context[EACH_FLATMAP](this.sink$, (action)=>{
5858
if(action instanceof Function) {
5959
this.setState((prevState, props)=>{
6060
let newState = action.call(this, prevState,props);
@@ -70,6 +70,9 @@ export function connect(main, initprops={}) {
7070
}
7171
});
7272
}
73+
componentWillUnmount(){
74+
this.subscribtions.unsubscribe()
75+
}
7376
render() {
7477
return <WrappedComponent {...this.props} {...this.state} {...initprops} actions={this.actions} />
7578
}

0 commit comments

Comments
 (0)