Skip to content

Commit 9903149

Browse files
committed
Feature: add throttle middleware
1 parent dbdbc2b commit 9903149

File tree

7 files changed

+91
-41
lines changed

7 files changed

+91
-41
lines changed

.babelrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
],
1515
"plugins": [
1616
"@babel/plugin-proposal-class-properties",
17+
"@babel/plugin-proposal-object-rest-spread",
1718
"@babel/plugin-proposal-export-default-from",
1819
["lodash", {"id": ["lodash", "semantic-ui-react"]}]
1920
],

src/App.js

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import React, { Component } from 'react';
2-
import { Button, Grid, Header, List, Segment } from 'semantic-ui-react';
2+
import {
3+
Button,
4+
Form,
5+
Grid,
6+
Header,
7+
Input,
8+
List,
9+
Segment
10+
} from 'semantic-ui-react';
311
import * as action from './actions/index.js';
412
import { connect } from 'react-redux';
513

@@ -39,10 +47,16 @@ class App extends Component {
3947
state = {};
4048

4149
callFade = () => {
42-
console.log('here');
4350
this.props.fadeLed();
4451
};
4552

53+
setFadeTime = (e, data) => {
54+
// console.log('event', e.target);
55+
e.preventDefault();
56+
console.log('data', data);
57+
this.props.fadeTime(data.value);
58+
};
59+
4660
render() {
4761
return (
4862
<Navbar leftItems={leftItems} rightItems={rightItems}>
@@ -51,34 +65,18 @@ class App extends Component {
5165

5266
<Grid>
5367
<Grid.Column computer={6} mobile={16}>
54-
<p>
55-
Welcome to your Semantic UI React App! It is awesome{' '}
56-
<span aria-label="emoji" role="img">
57-
😉
58-
</span>
59-
</p>
68+
<Input focus placeholder="Time" onChange={this.setFadeTime} />
69+
<Form>
70+
<Form.Field>
71+
<label>Fade Time</label>
72+
<input placeholder="Fade Time" onChange={this.setFadeTime} />
73+
</Form.Field>
74+
</Form>
6075
</Grid.Column>
6176
<Grid.Column computer={10} mobile={16}>
6277
<Header as="h3">
63-
Themed <code>Button</code>
78+
Click the <code>Button</code> to fade
6479
</Header>
65-
<p>
66-
Semantic UI React does not have own theming and fully relies on
67-
CSS part of Semantic UI. It is normal, Semantic UI theming is
68-
very powerful, it allows you fully modify the look of your app
69-
using theming variables.
70-
</p>
71-
<p>
72-
We changed the <code>primary</code> color of <code>Button</code>{' '}
73-
component, it is really easy
74-
<span aria-label="emoji" role="img">
75-
😁
76-
</span>{' '}
77-
Take a look to{' '}
78-
<code>/src/styling/theme/elements/button.variables</code>. By
79-
the way, the <code>theme</code> directory structure fully
80-
matches the component structure of Semantic UI React.
81-
</p>
8280
<Button primary>Primary Button</Button>
8381
<Button onClick={() => this.callFade()}>Fade LED</Button>
8482

@@ -122,11 +120,12 @@ class App extends Component {
122120
}
123121
}
124122
const mapStateToProps = state => ({
125-
fadedLed: state.fade
123+
time: state.time
126124
});
127125

128126
const mapDispatchToProps = dispatch => ({
129-
fadeLed: () => dispatch(action.fadeLed())
127+
fadeLed: () => dispatch(action.fadeLed()),
128+
fadeTime: millisec => dispatch(action.fadeTime(millisec))
130129
});
131130

132131
export default connect(mapStateToProps, mapDispatchToProps)(App);

src/actions/index.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,17 @@ export const fadeLed = data => ({
99
types: [FADE_REQUEST, FADE_SUCCESS, FADE_FAILURE],
1010
endpoint: '/fade',
1111
method: 'GET'
12+
},
13+
meta: {
14+
throttle: 1500
1215
}
1316
});
1417

15-
export const LOGOUT_USER = 'LOGOUT_USER';
18+
export const FADE_TIME = 'FADE_TIME';
1619

17-
export const logoutUser = () => ({
18-
type: LOGOUT_USER
19-
});
20-
21-
export const SET_USER = 'SET_USER';
22-
23-
export const setUser = data => ({
24-
type: SET_USER,
25-
data
20+
export const fadeTime = millisec => ({
21+
type: FADE_TIME,
22+
millisec
2623
});
2724

2825
export const goToUser = userId => ({

src/index.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,20 @@ import ReactDOM from 'react-dom';
33
import { AppContainer } from 'react-hot-loader';
44
import App from './App';
55
import { Provider } from 'react-redux';
6-
import { createStore, applyMiddleware } from 'redux';
6+
import { createStore, applyMiddleware, compose } from 'redux';
77

88
import reducers from './reducers';
99
import api from './middleware/api';
10+
import throttler from './middleware/throttle';
1011

11-
let store = createStore(reducers, applyMiddleware(api));
12+
const composeEnhancers =
13+
typeof window === 'object' && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
14+
? window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({})
15+
: compose;
16+
17+
const enhancer = composeEnhancers(applyMiddleware(throttler, api));
18+
19+
const store = createStore(reducers, enhancer);
1220

1321
const render = Component => {
1422
ReactDOM.render(

src/middleware/throttle.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { CALL_API } from './api';
2+
3+
const throttled = {};
4+
5+
const throttler = () => next => action => {
6+
const time = action[CALL_API] && action.meta && action.meta.throttle;
7+
console.log(throttled);
8+
console.log(action);
9+
10+
if (!time) return next(action);
11+
12+
if (throttled[CALL_API]) {
13+
console.log('action true');
14+
return;
15+
}
16+
throttled[CALL_API] = true;
17+
18+
setTimeout(() => {
19+
console.log('here');
20+
throttled[CALL_API] = false;
21+
}, time);
22+
23+
return next(action);
24+
};
25+
26+
export default throttler;

src/reducers/arduino.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import * as actions from '../actions/index.js';
2+
3+
const arduinoState = {
4+
fade_millisec: 1000
5+
};
6+
7+
const arduino = (state = arduinoState, action) => {
8+
switch (action.type) {
9+
case actions.FADE_TIME:
10+
return {
11+
...state,
12+
fade_millisec: action.millisec
13+
};
14+
default:
15+
return state;
16+
}
17+
};
18+
19+
export default arduino;

src/styling/theme/elements/button.variables

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
User Variable Overrides
33
*******************************/
44

5-
@primaryColor: @pink;
5+
@primaryColor: @red;

0 commit comments

Comments
 (0)