Skip to content

Commit bd44cbc

Browse files
committed
minor improvement, call throttle on mouseevent, does not apply another mouseevent when one is in progress
1 parent 8864fd9 commit bd44cbc

File tree

4 files changed

+37
-42
lines changed

4 files changed

+37
-42
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
"@tableau/tableau-ui": "^2.1.0",
1111
"core-js": "^2.6.0",
1212
"gh-pages": "^2.0.1",
13-
"jshint": "^2.10.2",
1413
"kepler.gl": "^1.0.0-1",
14+
"lodash.throttle": "^4.1.1",
1515
"react": "^16.8.4",
1616
"react-beautiful-dnd": "^9.0.2",
1717
"react-bootstrap": "^0.32.1",

src/App.js

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/* eslint-disable complexity */
22
import React, {Component} from 'react';
33
import {connect} from 'react-redux';
4-
import PropTypes from 'prop-types';
4+
import throttle from 'lodash.throttle';
5+
56
import './App.css';
67

78
// actions
@@ -101,6 +102,9 @@ class App extends Component {
101102
TableauSettings.setEnvName(this.props.isConfig ? 'CONFIG' : 'EXTENSION');
102103

103104
this.unregisterHandlerFunctions = [];
105+
this.applyingMouseActions = false;
106+
this.clickCallBack = throttle(this.clickCallBack, 200);
107+
this.hoverCallBack = throttle(this.hoverCallBack, 200);
104108
}
105109

106110
// eslint-disable-next-line react/sort-comp
@@ -109,7 +113,7 @@ class App extends Component {
109113
// since we add them back later in this function.
110114
// provided by tableau extension samples
111115

112-
log('%c addEventListeners', 'background: purple; color:yellow');
116+
console.log('%c addEventListeners', 'background: purple; color:yellow');
113117
this.removeEventListeners();
114118

115119
const localUnregisterHandlerFunctions = [];
@@ -137,27 +141,17 @@ class App extends Component {
137141
});
138142

139143
this.unregisterHandlerFunctions = localUnregisterHandlerFunctions;
140-
log(`%c added ${this.unregisterHandlerFunctions.length} EventListeners`, 'background: purple, color:yellow');
141-
142-
// this.setState({unregisterHandlerFunctions: localUnregisterHandlerFunctions }
143-
// , () => console.log('event listeners added', this.state)
144-
// );
144+
// log(`%c added ${this.unregisterHandlerFunctions.length} EventListeners`, 'background: purple, color:yellow');
145145
}
146146

147147
removeEventListeners = () => {
148-
// let localUnregisterHandlerFunctions = [ ...this.unregisterHandlerFunctions ];
149-
150-
log(`%c remove ${this.unregisterHandlerFunctions.length} EventListeners`, 'background: green; color:black');
148+
console.log(`%c remove ${this.unregisterHandlerFunctions.length} EventListeners`, 'background: green; color:black');
151149

152150
this.unregisterHandlerFunctions.forEach(unregisterHandlerFunction => {
153151
unregisterHandlerFunction();
154152
});
155153

156-
// localUnregisterHandlerFunctions = [];
157154
this.unregisterHandlerFunctions = [];
158-
// this.setState({ unregisterHandlerFunctions: []}
159-
// , () => console.log('event listeners removed', this.state)
160-
// );
161155
}
162156

163157
onNextStep = () => {
@@ -179,12 +173,12 @@ class App extends Component {
179173
clickCallBack = d => {
180174
const {clickField, clickAction} = this.state.tableauSettings;
181175

182-
log(
176+
console.log(
183177
'%c in on click callback',
184178
'background: brown',
185-
d,
186-
findColumnIndexByFieldName(this.state, clickField),
187-
clickAction
179+
// d,
180+
// findColumnIndexByFieldName(this.state, clickField),
181+
// clickAction
188182
);
189183

190184
this.applyMouseActionsToSheets(d, clickAction, clickField);
@@ -193,19 +187,22 @@ class App extends Component {
193187
hoverCallBack = d => {
194188
const {hoverField, hoverAction} = this.state.tableauSettings;
195189

196-
log(
190+
console.log(
197191
'%c in on hover callback',
198192
'background: OLIVE',
199-
d,
200-
findColumnIndexByFieldName(this.state, hoverField),
201-
hoverAction
193+
// d,
194+
// findColumnIndexByFieldName(this.state, hoverField),
195+
// hoverAction
202196
);
203197

204198
this.applyMouseActionsToSheets(d, hoverAction, hoverField);
205199
// go through each worksheet and select marks
206200
};
207201

208202
applyMouseActionsToSheets = (d, action, fieldName) => {
203+
if (this.applyingMouseActions) {
204+
return;
205+
}
209206
const {ConfigSheet} = this.state.tableauSettings;
210207
const toHighlight = action === 'Highlight' && (fieldName || 'None') !== 'None';
211208
const toFilter = action === 'Filter' && (fieldName || 'None') !== 'None';
@@ -217,6 +214,8 @@ class App extends Component {
217214

218215
// remove EventListeners before apply any async actions
219216
this.removeEventListeners();
217+
this.applyingMouseActions = true;
218+
220219
let tasks = [];
221220

222221
if (d) {
@@ -235,8 +234,10 @@ class App extends Component {
235234
}
236235

237236
Promise.all(tasks).then(() => {
237+
// all selection should be completed
238238
// Add event listeners back
239239
this.addEventListeners();
240+
this.applyingMouseActions = false;
240241
});
241242
}
242243

@@ -372,7 +373,10 @@ class App extends Component {
372373

373374
marksSelected = e => {
374375
if ( this.state.tableauSettings.keplerFilterField ) {
375-
log(
376+
if (this.applyingMouseActions) {
377+
return;
378+
}
379+
console.log(
376380
'%c ==============App Marker selected',
377381
'background: red; color: white'
378382
);
@@ -395,7 +399,6 @@ class App extends Component {
395399
}
396400

397401
const keplerData = dataToKeplerRow(marksDataTable.data, keplerFields);
398-
// console.log('zzz mark do we see data', marksDataTable.data.length, marksDataTable.data, keplerData, keplerFields, col_indexes);
399402

400403
const filterKeplerObject = {
401404
field: keplerFilterField,
@@ -405,9 +408,6 @@ class App extends Component {
405408
// @shan you can remove this console once you are good with the object
406409
this.props.dispatch(markerSelect(filterKeplerObject));
407410
this.setState({filterKeplerObject}, () => this.addEventListeners());
408-
// console the select marks table
409-
// log('marks', marksDataTable, col_indexes, data, this.state.tableauSettings.hoverField, this.state.tableauSettings.clickField);
410-
411411
});
412412
}
413413
}

src/components/KeplerGL/app.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import React, {Component} from 'react';
2222
import {connect} from 'react-redux';
23-
import _ from 'lodash';
2423
import {MAP_ID, DATA_ID} from '../../constants';
2524

2625
// Kepler.gl actions

src/utils/interaction-utils.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
11
const tableauExt = window.tableau.extensions;
22

33
export function selectMarksByField(fieldName, fieldValues, ConfigSheet) {
4-
console.log('%c select marks by field', 'background: OLIVE; color: white')
54
const marksValue = [{
65
fieldName,
76
value: fieldValues
87
}];
98

9+
// Empty promise that resolves when the selection is complete.
1010
return tableauExt.dashboardContent.dashboard.worksheets
1111
.filter(ws => ws.name !== ConfigSheet)
12-
.map(worksheet =>
13-
worksheet
14-
.selectMarksByValueAsync(
15-
marksValue,
16-
window.tableau.SelectionUpdateType.Replace
17-
)
12+
.map(worksheet => worksheet
13+
.selectMarksByValueAsync(
14+
marksValue,
15+
window.tableau.SelectionUpdateType.Replace
16+
)
1817
);
1918
}
2019

2120
export function applyFilterByField(fieldName, fieldValues, ConfigSheet) {
22-
console.log('%c applyFilterByField', 'background: OLIVE; color: white')
23-
21+
// Empty promise that resolves when the selection is complete.
2422
return tableauExt.dashboardContent.dashboard.worksheets
2523
.filter(ws => ws.name !== ConfigSheet)
2624
.map(worksheet => {
@@ -34,16 +32,14 @@ export function applyFilterByField(fieldName, fieldValues, ConfigSheet) {
3432
}
3533

3634
export function clearFilterByField(fieldName, ConfigSheet) {
37-
console.log('%c clearFilterByField', 'background: MAROON; color: white')
38-
35+
// Empty promise that resolves when the selection is complete.
3936
return tableauExt.dashboardContent.dashboard.worksheets
4037
.filter(ws => ws.name !== ConfigSheet)
4138
.map(worksheet => {
42-
worksheet.clearFilterAsync(fieldName)
39+
worksheet.clearFilterAsync(fieldName);
4340
});
4441
}
4542

4643
export function clearMarksByField(fieldName, ConfigSheet) {
47-
console.log('%c clearMarksByField', 'background: MAROON; color: white')
4844
return selectMarksByField(fieldName, [], ConfigSheet);
4945
}

0 commit comments

Comments
 (0)