Skip to content
This repository was archived by the owner on May 17, 2019. It is now read-only.

Commit 39574d5

Browse files
authored
Update api of withRPCRedux and withRPCReactor (#4)
1 parent 2769db8 commit 39574d5

File tree

7 files changed

+106
-52
lines changed

7 files changed

+106
-52
lines changed

README.md

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ function Example({count, loading, error, increment}) {
7070
}
7171

7272
const hoc = compose(
73-
withRPCRedux({rpcId: 'increment'}),
73+
withRPCRedux('increment'),
7474
connect(({count, loading, error}) => ({count, loading, error})),
7575
);
7676
export default hoc(Example);
@@ -79,22 +79,21 @@ export default hoc(Example);
7979
### Usage with Reactors
8080

8181
```js
82-
// add the reactor enhancer in main.js
82+
// add the reactor enhancer in src/main.js
8383
import {reactorEnhancer} from 'redux-reactors'
8484
// ...
8585
app.plugin(ReduxPlugin, {reducer, enhancer: reactorEnhancer});
8686

87-
// define a reactor
88-
import {createRPCReactor} from 'fusion-plugin-rpc-redux-react';
89-
export const incrementReactor = createRPCReactor('increment', {
87+
// define a reactor (src/reactors/increment.js)
88+
import {withRPCReactor} from 'fusion-plugin-rpc-redux-react';
89+
export const incrementReactor = withRPCReactor('increment', {
9090
start: (state, action) => ({count: state.count, loading: true, error: ''});
9191
success: (state, action) => ({count: action.payload.count, loading: false, error: ''});
9292
failure: (state, action) => ({count: state.count, loading: false, error: action.payload.error});
9393
});
9494

95-
// use the higher order component
95+
// use the higher order component (src/components/example.js)
9696
import React from 'react';
97-
import {withRPCReactor} from 'fusion-plugin-rpc-redux-react';
9897
import {connect} from 'react-redux';
9998
import {compose} from 'redux';
10099
import {incrementReactor} from './reactors/increment.js'
@@ -113,7 +112,7 @@ function Example({count, loading, error, increment}) {
113112
}
114113

115114
const hoc = compose(
116-
withRPCRedux(incrementReactor),
115+
incrementReactor,
117116
connect(({count, loading, error}) => ({count, loading, error})),
118117
);
119118
export default hoc(Example);
@@ -126,12 +125,24 @@ export default hoc(Example);
126125

127126
```js
128127
import {withRPCRedux} from 'fusion-plugin-rpc-redux-react';
129-
const NewComponent = withRPCRedux({
130-
rpcId: '', // required
128+
const NewComponent = withRPCRedux('rpcId', {
131129
propName: '', // optional, defaults to rpcId
132130
mapStateToParams: (state) => ({}), // optional
133131
transformParams(params) => ({}), // optional
134-
})Component)
132+
})(Component)
135133
```
136134

137135
#### `withRPCReactor`
136+
```js
137+
import {withRPCReactor} from 'fusion-plugin-rpc-redux-react';
138+
const NewComponent = withRPCReactor('rpcId', {
139+
start: (state, action) => newState, // optional
140+
success: (state, action) => newState, // optional
141+
failure: (state, action) => newState, // optional
142+
},
143+
{
144+
propName: '', // optional, defaults to rpcId
145+
mapStateToParams: (state) => ({}), // optional
146+
transformParams(params) => ({}), // optional
147+
})(Component);
148+
```

docs/migrations/00004.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#### Remove createRPCReactor export
2+
3+
```diff
4+
-import {createRPCReactor} from 'fusion-plugin-rpc-redux-react';
5+
```
6+
7+
#### Update withRPCReactor api
8+
9+
```diff
10+
-import {createRPCReactor} from 'fusion-plugin-rpc-redux-react';
11+
-const withIncrement = withRPCReactor({
12+
- ...createRPCReactor('rpcId', {
13+
- start: (state, action) => newState,
14+
- success: (state, action) => newState,
15+
- failure: (state, action) => newState,
16+
- }),
17+
- propName: '',
18+
- mapStateToParams: (state) => {},
19+
- transformParams: (params) => {},
20+
-});
21+
+const withIncrement = withRPCReactor('rpcId', {
22+
+ start: (state, action) => newState,
23+
+ success: (state, action) => newState,
24+
+ failure: (state, action) => newState,
25+
+}, {
26+
+ propName: '',
27+
+ mapStateToParams: (state) => {},
28+
+ transformParams: (params) => {},
29+
+})
30+
```
31+
32+
#### Update withRPCRedux api
33+
```diff
34+
-const withIncrement = withRPCRedux({
35+
- rpcId: 'increment',
36+
- propName: '',
37+
- mapStateToParams: (state) => {},
38+
- transformParams: (params) => {},
39+
-});
40+
+const withIncrement = withRPCRedux('increment', {
41+
+ propName: '',
42+
+ mapStateToParams: (state) => {},
43+
+ transformParams: (params) => {},
44+
+});
45+
```

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
"./dist/browser.es.js": "./dist/browser.es.es2015.js"
1919
},
2020
"dependencies": {
21-
"fusion-plugin-rpc": "^0.1.8",
22-
"web-rpc-redux": "^0.1.7"
21+
"fusion-plugin-rpc": "^0.2.0",
22+
"fusion-rpc-redux": "^0.2.0"
2323
},
2424
"peerDependencies": {
2525
"fusion-react": "^0.1.8",

src/__tests__/__node__/index.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ test('plugin', t => {
1111
of() {},
1212
};
1313
const RPCRedux = Plugin({handlers, EventEmitter});
14-
t.equal(typeof RPCRedux.of().test, 'function');
14+
const mockCtx = {headers: {}};
15+
t.equal(typeof RPCRedux.of(mockCtx).test, 'function');
1516
t.end();
1617
});
1718

1819
test('withRPCRedux hoc', t => {
1920
function Test() {}
2021
t.equals(typeof withRPCRedux, 'function');
21-
const Connected = withRPCRedux({
22-
rpcId: 'test',
22+
const Connected = withRPCRedux('test', {
2323
actions: {
2424
start() {},
2525
success() {},
@@ -51,24 +51,27 @@ test('withRPCRedux hoc', t => {
5151
test('withRPCReactor hoc', t => {
5252
function Test() {}
5353
t.equals(typeof withRPCReactor, 'function');
54-
const Connected = withRPCReactor({
55-
rpcId: 'test',
56-
reactors: {
57-
start() {},
58-
success() {},
59-
failure() {},
60-
},
61-
mapStateToParams: () => {},
62-
transformParams: () => {},
54+
const Connected = withRPCReactor('test', {
55+
start() {},
56+
success() {},
57+
failure() {},
6358
})(Test);
6459
t.equals(Connected.displayName, 'WithRPCRedux(Test)');
6560
const renderer = new ShallowRenderer();
61+
const expectedActions = ['TEST_START', 'TEST_SUCCESS'];
62+
const expectedPayloads = ['test-args', 'test-resolve'];
6663
renderer.render(React.createElement(Connected), {
6764
rpc: {
68-
test() {},
65+
test(args) {
66+
t.equal(args, 'test-args');
67+
return Promise.resolve('test-resolve');
68+
},
6969
},
7070
store: {
71-
dispatch() {},
71+
dispatch(action) {
72+
t.equal(action.type, expectedActions.shift());
73+
t.equal(action.payload, expectedPayloads.shift());
74+
},
7275
getState() {},
7376
},
7477
});
@@ -78,5 +81,6 @@ test('withRPCReactor hoc', t => {
7881
'function',
7982
'passes the handler through to props'
8083
);
84+
rendered.props.test('test-args');
8185
t.end();
8286
});

src/hoc.js

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,25 @@
11
import PropTypes from 'prop-types';
22
import React from 'react';
3-
import {createRPCHandler, createRPCReactor} from 'web-rpc-redux';
3+
import {createRPCHandler, createRPCReactors} from 'fusion-rpc-redux';
44

5-
export const withRPCReactor = ({
6-
propName,
5+
export const withRPCReactor = (
76
rpcId,
87
reactors,
9-
transformParams,
10-
mapStateToParams,
11-
}) => {
12-
const actions = createRPCReactor(rpcId, reactors);
13-
return withRPCRedux({
14-
actions,
8+
{propName, transformParams, mapStateToParams} = {}
9+
) => {
10+
return withRPCRedux(rpcId, {
11+
actions: createRPCReactors(rpcId, reactors),
1512
propName,
1613
rpcId,
1714
transformParams,
1815
mapStateToParams,
1916
});
2017
};
2118

22-
export function withRPCRedux({
23-
propName,
19+
export function withRPCRedux(
2420
rpcId,
25-
actions,
26-
transformParams,
27-
mapStateToParams,
28-
}) {
21+
{propName, actions, transformParams, mapStateToParams} = {}
22+
) {
2923
if (!propName) {
3024
propName = rpcId;
3125
}

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import plugin from './plugin';
2-
export {createRPCReducer, createRPCReactor} from 'web-rpc-redux';
2+
export {createRPCReducer} from 'fusion-rpc-redux';
33
export {withRPCRedux, withRPCReactor} from './hoc';
44

55
export default plugin;

yarn.lock

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1570,9 +1570,9 @@ fusion-core@^0.1.8:
15701570
koa-compose "^4.0.0"
15711571
node-mocks-http "^1.6.6"
15721572

1573-
fusion-plugin-rpc@^0.1.8:
1574-
version "0.1.8"
1575-
resolved "https://registry.yarnpkg.com/fusion-plugin-rpc/-/fusion-plugin-rpc-0.1.8.tgz#be96ccdcf3e7120af528171c157ee98f1a079bd7"
1573+
fusion-plugin-rpc@^0.2.0:
1574+
version "0.2.0"
1575+
resolved "https://registry.yarnpkg.com/fusion-plugin-rpc/-/fusion-plugin-rpc-0.2.0.tgz#a1fc7facec7de605b95c6ff74d9d517e3c2ecb7a"
15761576
dependencies:
15771577
body-parser "^1.17.1"
15781578
koa-bodyparser "4.2.0"
@@ -1587,6 +1587,12 @@ fusion-react@^0.1.8:
15871587
version "0.1.8"
15881588
resolved "https://registry.yarnpkg.com/fusion-react/-/fusion-react-0.1.8.tgz#fed6129ffe7745f2692a94ac513d80af30821e0b"
15891589

1590+
fusion-rpc-redux@^0.2.0:
1591+
version "0.2.0"
1592+
resolved "https://registry.yarnpkg.com/fusion-rpc-redux/-/fusion-rpc-redux-0.2.0.tgz#ad88778466af6f34d07945077325e9180379cc38"
1593+
dependencies:
1594+
redux-reactors "^1.0.3"
1595+
15901596
get-stdin@^4.0.1:
15911597
version "4.0.1"
15921598
resolved "https://registry.yarnpkg.com/get-stdin/-/get-stdin-4.0.1.tgz#b968c6b0a04384324902e8bf1a5df32579a450fe"
@@ -3350,12 +3356,6 @@ vlq@^0.2.1:
33503356
version "0.2.3"
33513357
resolved "https://registry.yarnpkg.com/vlq/-/vlq-0.2.3.tgz#8f3e4328cf63b1540c0d67e1b2778386f8975b26"
33523358

3353-
web-rpc-redux@^0.1.7:
3354-
version "0.1.7"
3355-
resolved "https://registry.yarnpkg.com/web-rpc-redux/-/web-rpc-redux-0.1.7.tgz#e9d5bee6a50b1a5813ea85c4b150aa97459b9358"
3356-
dependencies:
3357-
redux-reactors "^1.0.3"
3358-
33593359
whatwg-fetch@>=0.10.0:
33603360
version "2.0.3"
33613361
resolved "https://registry.yarnpkg.com/whatwg-fetch/-/whatwg-fetch-2.0.3.tgz#9c84ec2dcf68187ff00bc64e1274b442176e1c84"

0 commit comments

Comments
 (0)