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

Commit 8e459b6

Browse files
ganemonefusion-bot[bot]
authored andcommitted
Use custom provider to handle calling from with ctx
#51
1 parent ecdc32e commit 8e459b6

File tree

5 files changed

+34
-25
lines changed

5 files changed

+34
-25
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"fusion-tokens": "0.0.5",
5353
"nyc": "^11.4.1",
5454
"prettier": "1.10.2",
55+
"prop-types": "^15.6.0",
5556
"react": "^16.2.0",
5657
"react-dom": "16.2.0",
5758
"react-redux": "^5.0.6",

src/__tests__/index.node.js

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,10 @@ test('withRPCRedux hoc', t => {
4545
const expectedPayloads = ['test-args', 'test-resolve'];
4646
renderer.render(React.createElement(Connected), {
4747
rpc: {
48-
from() {
49-
return {
50-
request(method, args) {
51-
t.equal(method, 'test');
52-
t.equal(args, 'test-args');
53-
return Promise.resolve('test-resolve');
54-
},
55-
};
48+
request(method, args) {
49+
t.equal(method, 'test');
50+
t.equal(args, 'test-args');
51+
return Promise.resolve('test-resolve');
5652
},
5753
},
5854
store: {
@@ -87,14 +83,10 @@ test('withRPCReactor hoc', t => {
8783
const expectedPayloads = ['test-args', 'test-resolve'];
8884
renderer.render(React.createElement(Connected), {
8985
rpc: {
90-
from() {
91-
return {
92-
request(method, args) {
93-
t.equal(method, 'test');
94-
t.equal(args, 'test-args');
95-
return Promise.resolve('test-resolve');
96-
},
97-
};
86+
request(method, args) {
87+
t.equal(method, 'test');
88+
t.equal(args, 'test-args');
89+
return Promise.resolve('test-resolve');
9890
},
9991
},
10092
store: {

src/hoc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export function withRPCRedux(
3535
const {rpc, store} = this.context;
3636
const handler = createRPCHandler({
3737
rpcId,
38-
rpc: rpc.from(this.props.ctx),
38+
rpc,
3939
store,
4040
actions,
4141
mapStateToParams,

src/index.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,13 @@
44
* LICENSE file in the root directory of this source tree.
55
*/
66

7-
import {ProviderPlugin} from 'fusion-react';
8-
import {mock as RPCMock} from 'fusion-plugin-rpc';
9-
import plugin from './plugin';
7+
import plugin, {mock} from './plugin';
108

119
export {createRPCReducer} from 'fusion-rpc-redux';
1210
export {withRPCRedux, withRPCReactor} from './hoc';
1311

1412
export default plugin;
15-
16-
export const mock = ProviderPlugin.create('rpc', RPCMock);
17-
13+
export {mock};
1814
export {
1915
RPCToken,
2016
RPCHandlersToken,

src/plugin.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,26 @@
55
*/
66

77
import {ProviderPlugin} from 'fusion-react';
8-
import rpc from 'fusion-plugin-rpc';
8+
import PropTypes from 'prop-types';
9+
import React from 'react';
10+
import rpc, {mock as RPCMock} from 'fusion-plugin-rpc';
911

10-
export default ProviderPlugin.create('rpc', rpc);
12+
class RPCProvider extends React.Component {
13+
constructor(props, context) {
14+
super(props, context);
15+
this.rpc = props.provides.from(props.ctx);
16+
}
17+
getChildContext() {
18+
return {rpc: this.rpc};
19+
}
20+
render() {
21+
return React.Children.only(this.props.children);
22+
}
23+
}
24+
25+
RPCProvider.childContextTypes = {
26+
rpc: PropTypes.object.isRequired,
27+
};
28+
29+
export default ProviderPlugin.create('rpc', rpc, RPCProvider);
30+
export const mock = ProviderPlugin.create('rpc', RPCMock, RPCProvider);

0 commit comments

Comments
 (0)