From c18a2752af43e04e52fd887572643bebc9d69963 Mon Sep 17 00:00:00 2001 From: uwap Date: Sun, 22 Jul 2018 05:17:47 +0200 Subject: [PATCH] Use Throttling --- src/components/App.js | 8 ++++++-- src/components/ControlMap.js | 3 +-- src/components/UiItemList/UiItem.js | 4 ++-- src/components/UiItemList/index.js | 10 ++++++++-- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/components/App.js b/src/components/App.js index 9e9ad7c..b0fa510 100644 --- a/src/components/App.js +++ b/src/components/App.js @@ -5,6 +5,7 @@ import mapValues from "lodash/mapValues"; import filter from "lodash/filter"; import keys from "lodash/keys"; import merge from "lodash/merge"; +import throttle from "lodash/throttle"; import type { Config, Control, Topics } from "config/flowtypes"; @@ -94,14 +95,17 @@ class App extends React.PureComponent { const stateTopic = this.topics[topic].state; const parseVal = stateTopic ? stateTopic.type : null; const val = parseVal == null ? message.toString() : parseVal(message); - this.setState({mqttState: Object.assign({}, merge(this.state.mqttState, - { [topic]: val}))}); + this.setMqttStateDebounced( + {mqttState: Object.assign({}, + merge(this.state.mqttState, { [topic]: val}))}); } } catch (err) { this.setState({ error: err.toString() }); } } + setMqttStateDebounced = throttle(this.setState, 32); + changeControl(control: ?Control = null) { this.setState({selectedControl: control, drawerOpened: control != null}); } diff --git a/src/components/ControlMap.js b/src/components/ControlMap.js index 63bcbc2..a724253 100644 --- a/src/components/ControlMap.js +++ b/src/components/ControlMap.js @@ -67,10 +67,9 @@ export default class ControlMap extends React.PureComponent { renderMarker(control: Control, key: string) { return ( - + {({ state }) => ( this.props.onChangeControl(control)} > diff --git a/src/components/UiItemList/UiItem.js b/src/components/UiItemList/UiItem.js index 703b345..2fc6c11 100644 --- a/src/components/UiItemList/UiItem.js +++ b/src/components/UiItemList/UiItem.js @@ -2,7 +2,7 @@ import React from "react"; import keys from "lodash/keys"; import map from "lodash/map"; -import debounce from "lodash/debounce"; +import throttle from "lodash/throttle"; import { renderRawIcon } from "config/icon"; import ListItemSecondaryAction from "@material-ui/core/ListItemSecondaryAction"; import ListItemText from "@material-ui/core/ListItemText"; @@ -76,7 +76,7 @@ export class UiControl extends UiItem { this.debouncedChange(next); } - debouncedChange = debounce((next: string) => + debouncedChange = throttle((next: string) => this.props.onChangeState(this.props.item.topic, next), 50, { leading: true, trailing: true diff --git a/src/components/UiItemList/index.js b/src/components/UiItemList/index.js index 518e8d7..9e8a5ff 100644 --- a/src/components/UiItemList/index.js +++ b/src/components/UiItemList/index.js @@ -27,6 +27,13 @@ export default class UiItemList extends React.PureComponent { "A control is missing the \"type\" parameter" ); } + if (control.type === "section") { + return( + + {this.renderListItem(control, key)} + + ); + } return ( @@ -40,8 +47,7 @@ export default class UiItemList extends React.PureComponent { renderListItem(control: ControlUI, key: number) { return (mqtt: MqttContextValue) => { const node = this.renderControl(control, key.toString(), mqtt); - if (control.icon == null || control.type === "link" - || control.type === "section") { + if (control.icon == null || control.type === "link") { return node; } else { const listIconNode = (