Skip to content

Commit 4375d69

Browse files
committed
Explicit > implicit
1 parent b2210f1 commit 4375d69

File tree

9 files changed

+118
-188
lines changed

9 files changed

+118
-188
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import React from 'react';
2+
import { createDevTools } from 'redux-devtools';
3+
import createLogMonitor from 'redux-devtools-log-monitor';
4+
import createDockMonitor from '../dock/DockMonitor';
5+
6+
export default createDevTools(
7+
createDockMonitor(
8+
createLogMonitor()
9+
)
10+
);
Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
11
import React, { Component } from 'react';
22
import { Provider } from 'react-redux';
3-
import { DevToolsProvider } from 'redux-devtools';
43
import CounterApp from './CounterApp';
5-
import LogMonitor from 'redux-devtools-log-monitor';
6-
import DockMonitor from '../dock/DockMonitor';
4+
import DevTools from './DevTools';
75

86
export default class Root extends Component {
97
render() {
108
const { store } = this.props;
119
return (
12-
<div>
13-
<Provider store={store}>
10+
<Provider store={store}>
11+
<div>
1412
<CounterApp />
15-
</Provider>
16-
<DevToolsProvider store={store}>
17-
<DockMonitor>
18-
<LogMonitor />
19-
</DockMonitor>
20-
</DevToolsProvider>
21-
</div>
13+
<DevTools />
14+
</div>
15+
</Provider>
2216
);
2317
}
2418
}

examples/counter/src/dock/DockMonitor.js

Lines changed: 77 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -4,65 +4,23 @@
44

55
import React, { Component, PropTypes } from 'react';
66
import Dock from 'react-dock';
7-
import MapProvider from './MapProvider';
87
import { connect } from 'react-redux';
9-
import { combineReducers } from 'redux';
10-
11-
const TOGGLE_VISIBILITY = '@@redux-devtools/dock/TOGGLE_VISIBILITY';
12-
function toggleVisibility() {
13-
return { type: TOGGLE_VISIBILITY };
14-
}
15-
16-
const CHANGE_POSITION = '@@redux-devtools/dock/CHANGE_POSITION';
17-
function changePosition() {
18-
return { type: CHANGE_POSITION };
19-
}
8+
import { combineReducers, bindActionCreators } from 'redux';
209

2110
const POSITIONS = ['left', 'top', 'right', 'bottom'];
2211

23-
function wrapReducer(options = {}) {
24-
const {
25-
isVisible: initialIsVisible = true,
26-
position: initialPosition = 'right'
27-
} = options;
28-
29-
function position(state = initialPosition, action) {
30-
return (action.type === CHANGE_POSITION) ?
31-
POSITIONS[(POSITIONS.indexOf(state) + 1) % POSITIONS.length] :
32-
state;
33-
}
34-
35-
function isVisible(state = initialIsVisible, action) {
36-
return (action.type === TOGGLE_VISIBILITY) ?
37-
!state :
38-
state;
39-
}
40-
41-
return childMonitorReducer => combineReducers({
42-
childMonitorState: childMonitorReducer,
43-
position,
44-
isVisible
45-
});
46-
}
47-
48-
function mapUpstreamStateToDownstreamState(state) {
49-
return {
50-
devToolsState: state.devToolsState,
51-
monitorState: state.monitorState.childMonitorState
52-
};
53-
}
54-
55-
@connect(
56-
state => state.monitorState,
57-
{ toggleVisibility, changePosition }
58-
)
59-
export default class DockMonitor extends Component {
12+
class DockMonitor extends Component {
6013
static propTypes = {
61-
position: PropTypes.oneOf(['left', 'top', 'right', 'bottom']).isRequired,
62-
isVisible: PropTypes.bool.isRequired,
63-
childMonitorState: PropTypes.any,
64-
toggleVisibility: PropTypes.func.isRequired,
65-
changePosition: PropTypes.func.isRequired
14+
monitorState: PropTypes.shape({
15+
position: PropTypes.oneOf(POSITIONS).isRequired,
16+
isVisible: PropTypes.bool.isRequired,
17+
childState: PropTypes.any
18+
}).isRequired,
19+
20+
monitorActions: PropTypes.shape({
21+
toggleVisibility: PropTypes.func.isRequired,
22+
changePosition: PropTypes.func.isRequired
23+
}).isRequired
6624
};
6725

6826
componentDidMount() {
@@ -84,28 +42,86 @@ export default class DockMonitor extends Component {
8442
const char = String.fromCharCode(key);
8543
switch (char) {
8644
case 'H':
87-
this.props.toggleVisibility();
45+
this.props.monitorActions.toggleVisibility();
8846
break;
8947
case 'D':
90-
this.props.changePosition();
48+
this.props.monitorActions.changePosition();
9149
break;
9250
default:
9351
break;
9452
}
9553
}
9654

9755
render() {
98-
const { position, isVisible, children } = this.props;
56+
const { children, monitorState } = this.props;
57+
const { position, isVisible } = monitorState;
9958
return (
10059
<Dock position={position}
10160
isVisible={isVisible}
10261
dimMode='none'>
103-
<MapProvider mapState={mapUpstreamStateToDownstreamState}>
104-
{children}
105-
</MapProvider>
62+
{children}
10663
</Dock>
10764
);
10865
}
10966
}
11067

111-
DockMonitor.wrapReducer = wrapReducer;
68+
const TOGGLE_VISIBILITY = '@@redux-devtools/dock/TOGGLE_VISIBILITY';
69+
function toggleVisibility() {
70+
return { type: TOGGLE_VISIBILITY };
71+
}
72+
73+
const CHANGE_POSITION = '@@redux-devtools/dock/CHANGE_POSITION';
74+
function changePosition() {
75+
return { type: CHANGE_POSITION };
76+
}
77+
78+
export default function create(ChildMonitor, {
79+
defaultIsVisible = true,
80+
defaultPosition = 'right'
81+
} = {}) {
82+
function position(state = defaultPosition, action) {
83+
return (action.type === CHANGE_POSITION) ?
84+
POSITIONS[(POSITIONS.indexOf(state) + 1) % POSITIONS.length] :
85+
state;
86+
}
87+
88+
function isVisible(state = defaultIsVisible, action) {
89+
return (action.type === TOGGLE_VISIBILITY) ?
90+
!state :
91+
state;
92+
}
93+
94+
function getChildStore(store) {
95+
return {
96+
...store,
97+
getState() {
98+
const state = store.getState();
99+
return {
100+
...state,
101+
monitorState: state.monitorState.childState
102+
};
103+
}
104+
};
105+
}
106+
107+
const Monitor = connect(
108+
state => state,
109+
dispatch => ({
110+
monitorActions: bindActionCreators({ toggleVisibility, changePosition }, dispatch)
111+
})
112+
)(DockMonitor);
113+
114+
const CompositeMonitor = ({ store }) => (
115+
<Monitor store={store}>
116+
<ChildMonitor store={getChildStore(store)} />
117+
</Monitor>
118+
);
119+
120+
CompositeMonitor.reducer = combineReducers({
121+
childState: ChildMonitor.reducer,
122+
position,
123+
isVisible
124+
});
125+
126+
return CompositeMonitor;
127+
}

examples/counter/src/dock/MapProvider.js

Lines changed: 0 additions & 52 deletions
This file was deleted.

examples/counter/src/store/configureStore.dev.js

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,12 @@
11
import { createStore, applyMiddleware, compose } from 'redux';
2-
import devTools, { persistState } from 'redux-devtools';
2+
import { persistState } from 'redux-devtools';
33
import thunk from 'redux-thunk';
44
import rootReducer from '../reducers';
5-
import DockMonitor from '../dock/DockMonitor';
6-
import LogMonitor from 'redux-devtools-log-monitor';
5+
import DevTools from '../containers/DevTools';
76

87
const finalCreateStore = compose(
9-
applyMiddleware(
10-
thunk
11-
),
12-
devTools(
13-
LogMonitor.createReducer({
14-
preserveScrollTop: true
15-
}),
16-
DockMonitor.wrapReducer({
17-
position: 'right',
18-
isVisible: true
19-
})
20-
),
8+
applyMiddleware(thunk),
9+
DevTools.enhance,
2110
persistState(
2211
window.location.href.match(
2312
/[?&]debug_session=([^&]+)\b/

src/DevToolsProvider.js

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/createDevTools.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React, { cloneElement, Component, PropTypes } from 'react';
2+
import enhance from './enhance';
3+
4+
export default function createDevTools(Monitor) {
5+
return class DevTools extends Component {
6+
static contextTypes = {
7+
store: PropTypes.object.isRequired
8+
};
9+
10+
static enhance = enhance(Monitor.reducer);
11+
12+
render() {
13+
return <Monitor store={this.context.store.devToolsStore} />;
14+
}
15+
}
16+
}
17+

src/devTools.js renamed to src/enhance.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -264,18 +264,11 @@ export const ActionCreators = {
264264
/**
265265
* Redux DevTools store enhancer.
266266
*/
267-
export default function devTools(
268-
monitorReducer = () => null,
269-
...monitorReducerWrappers
270-
) {
267+
export default function enhance(monitorReducer = () => null) {
271268
return next => (reducer, initialState) => {
272-
const finalMonitorReducer = compose(
273-
...monitorReducerWrappers.slice().reverse()
274-
)(monitorReducer);
275-
276269
const wrapReducer = (r) => combineReducers({
277270
devToolsState: createDevToolsStateReducer(r, initialState),
278-
monitorState: finalMonitorReducer
271+
monitorState: monitorReducer
279272
});
280273

281274
const devToolsStore = next(wrapReducer(reducer));

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export { default, ActionCreators, ActionTypes } from './devTools';
2-
export { default as DevToolsProvider } from './DevToolsProvider';
1+
export { default, ActionCreators, ActionTypes } from './enhance';
32
export { default as persistState } from './persistState';
3+
export { default as createDevTools } from './createDevTools';

0 commit comments

Comments
 (0)