Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dispatchOn): replace dispatchOnMount with dispatchOn #6

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions src/dispatchOn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import React, { Component, PropTypes } from 'react';
import { Subscription } from 'rxjs/Subscription';

const $$reduxObservableSubscription = '@@reduxObservableSubscription';

const dispatchFactories = (subscription, store, factories, args) => {
factories.map(factory => store.dispatch(factory(...args)))
.forEach(sub => sub && subscription.add(sub));
};

export function dispatchOn({
willMount = null,
mount = null,
update = null,
willRecieveProps = null
}) {
return (ComposedComponent) =>
class DispatchOnMountComponent extends Component {
constructor(props) {
super(props);
}

static contextTypes = {
store: PropTypes.object.isRequired
}

getSubscription() {
const subscription = this[$$reduxObservableSubscription];
if (!subscription || subscription.isUnsubscribed) {
this[$$reduxObservableSubscription] = new Subscription();
}
return this[$$reduxObservableSubscription];
}

componentWillMount() {
if (willMount) {
dispatchFactories(this.getSubscription(), this.context.store, willMount, [this.props]);
}
}

componentDidMount() {
if (mount) {
dispatchFactories(this.getSubscription(), this.context.store, mount, [this.props]);
}
}

componentDidUpdate(prevProps) {
if (update) {
dispatchFactories(this.getSubscription(), this.context.store, update, [this.props, prevProps]);
}
}

componentWillReceiveProps(nextProps) {
if (willRecieveProps) {
dispatchFactories(this.getSubscription(), this.context.store, willRecieveProps, [this.props, nextProps]);
}
}

componentWillUnmount() {
const subscription = this[$$reduxObservableSubscription];
if (subscription) {
subscription.unsubscribe();
}
}

render() {
return (<ComposedComponent {...this.props} />);
}
};
}
27 changes: 0 additions & 27 deletions src/dispatchOnMount.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { dispatchOnMount } from './dispatchOnMount';
export { dispatchOn } from './dispatchOn';
108 changes: 98 additions & 10 deletions test/dispatchOnMount-spec.js → test/dipatchOn-spec.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
/* global describe, it */
import { expect } from 'chai';
import { reduxObservable } from 'redux-observable';
import { dispatchOnMount } from '../';
import { dispatchOn } from '../';
import { Component } from 'react';
import { createStore, applyMiddleware } from 'redux';
import * as Rx from 'rxjs';
import 'babel-polyfill';

const { Observable } = Rx;

describe('dispatchOnMount', () => {
describe('dispatchOn', () => {
it('should exist', () => {
expect(dispatchOnMount).to.be.a('function');
expect(dispatchOn).to.be.a('function');
});

it('should wire a thunkservable to dispatch on componentWillMount', () => {
let reducedActions = [];
let reducer = (state, action) => {
reducedActions.push(action);
return state;
};
let store = createStore(reducer, applyMiddleware(reduxObservable()));

@dispatchOn({ willMount: [() => () => Observable.of({ type: 'TEST' })] })
class TestComponent extends Component {
}

let comp = new TestComponent();
// fake connection?
comp.context = { store };
comp.componentWillMount();

expect(reducedActions).to.deep.equal([{ type: '@@redux/INIT' }, { type: 'TEST' }]);
});

it('should wire a thunkservable to dispatch on componentDidMount', () => {
Expand All @@ -22,7 +42,7 @@ describe('dispatchOnMount', () => {
};
let store = createStore(reducer, applyMiddleware(reduxObservable()));

@dispatchOnMount(() => () => Observable.of({ type: 'TEST' }))
@dispatchOn({ mount: [() => () => Observable.of({ type: 'TEST' })] })
class TestComponent extends Component {
}

Expand All @@ -34,6 +54,65 @@ describe('dispatchOnMount', () => {
expect(reducedActions).to.deep.equal([{ type: '@@redux/INIT' }, { type: 'TEST' }]);
});

it('should wire a thunkservable to dispatch on componentDidUpdate', () => {
let reducedActions = [];
let reducer = (state, action) => {
reducedActions.push(action);
return state;
};
let store = createStore(reducer, applyMiddleware(reduxObservable()));
let _prevProps;
let _thisProps;

@dispatchOn({ update: [(thisProps, prevProps) => {
_thisProps = thisProps;
_prevProps = prevProps;
return () => Observable.of({ type: 'TEST' });
}] })
class TestComponent extends Component {
}

let comp = new TestComponent();
// fake connection?
comp.context = { store };
comp.props = { some: 'props' };
comp.componentDidUpdate({ prev: 'props' });

expect(_thisProps).to.deep.equal({ some: 'props' });
expect(_prevProps).to.deep.equal({ prev: 'props' });
expect(reducedActions).to.deep.equal([{ type: '@@redux/INIT' }, { type: 'TEST' }]);
});

it('should wire a thunkservable to dispatch on componentWillReceiveProps', () => {
let reducedActions = [];
let reducer = (state, action) => {
reducedActions.push(action);
return state;
};
let store = createStore(reducer, applyMiddleware(reduxObservable()));

let _thisProps;
let _nextProps;

@dispatchOn({ willRecieveProps: [(thisProps, nextProps) => {
_thisProps = thisProps;
_nextProps = nextProps;
return () => Observable.of({ type: 'TEST' });
}] })
class TestComponent extends Component {
}

let comp = new TestComponent();
// fake connection?
comp.context = { store };
comp.props = { some: 'props' };
comp.componentWillReceiveProps({ next: 'props' });

expect(_thisProps).to.deep.equal({ some: 'props' });
expect(_nextProps).to.deep.equal({ next: 'props' });
expect(reducedActions).to.deep.equal([{ type: '@@redux/INIT' }, { type: 'TEST' }]);
});

it('should unsubscribe on componentWillUnmount', () => {
let reducedActions = [];
let reducer = (state, action) => {
Expand All @@ -56,7 +135,10 @@ describe('dispatchOnMount', () => {
};
});

@dispatchOnMount(() => () => source1, () => () => source2)
@dispatchOn({ mount: [
() => () => source1,
() => () => source2
] })
class TestComponent extends Component {
}

Expand Down Expand Up @@ -85,7 +167,10 @@ describe('dispatchOnMount', () => {
let source1 = Observable.of({ type: 'SOURCE1' });
let source2 = Observable.of({ type: 'SOURCE2' });

@dispatchOnMount(() => () => source1, () => () => source2)
@dispatchOn({ mount: [
() => () => source1,
() => () => source2
] })
class TestComponent extends Component {
}

Expand All @@ -111,7 +196,7 @@ describe('dispatchOnMount', () => {

let source2 = Observable.of({ type: 'SOURCE2' });

@dispatchOnMount(() => ({ type: 'PLAIN_ACTION' }), () => () => source2)
@dispatchOn({ mount: [() => ({ type: 'PLAIN_ACTION' }), () => () => source2] })
class TestComponent extends Component {
}

Expand Down Expand Up @@ -139,9 +224,12 @@ describe('dispatchOnMount', () => {
};
let store = createStore(reducer, applyMiddleware(reduxObservable()));

@dispatchOnMount(
(props) => ({ type: 'PLAIN_ACTION', value: props.value }),
(props) => () => Observable.of({ type: 'SOURCE2', value: props.value }))
@dispatchOn({
mount: [
(props) => ({ type: 'PLAIN_ACTION', value: props.value }),
(props) => () => Observable.of({ type: 'SOURCE2', value: props.value })
]
})
class TestComponent extends Component {
}

Expand Down