Skip to content

Commit

Permalink
Moved queriesArray from render() to local state (#1505)
Browse files Browse the repository at this point in the history
* Moved queriesArray from render() to local state, so that QueriesArray
is only reloaded only during switching tabs or queries object is updated.

* Changed object comparison function to take length into consideration
  • Loading branch information
vera-liu authored Nov 2, 2016
1 parent 769fb08 commit 26318f9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
24 changes: 17 additions & 7 deletions caravel/assets/javascripts/SqlLab/components/TabbedSqlEditors.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as Actions from '../actions';
import SqlEditor from './SqlEditor';
import { getParamFromQuery } from '../../../utils/common';
import CopyQueryTabUrl from './CopyQueryTabUrl';
import { areObjectsEqual } from '../../reduxUtils';

const propTypes = {
actions: React.PropTypes.object.isRequired,
Expand Down Expand Up @@ -34,6 +35,7 @@ class TabbedSqlEditors extends React.PureComponent {
uri,
cleanUri,
query,
queriesArray: [],
};
}
componentWillMount() {
Expand All @@ -51,6 +53,19 @@ class TabbedSqlEditors extends React.PureComponent {
window.history.replaceState({}, document.title, this.state.cleanUri);
}
}
componentWillReceiveProps(nextProps) {
const activeQeId = this.props.tabHistory[this.props.tabHistory.length - 1];
const newActiveQeId = nextProps.tabHistory[nextProps.tabHistory.length - 1];
if (activeQeId !== newActiveQeId || !areObjectsEqual(this.props.queries, nextProps.queries)) {
const queriesArray = [];
for (const id in this.props.queries) {
if (this.props.queries[id].sqlEditorId === newActiveQeId) {
queriesArray.push(this.props.queries[id]);
}
}
this.setState({ queriesArray });
}
}
renameTab(qe) {
/* eslint no-alert: 0 */
const newTitle = prompt('Enter a new title for the tab');
Expand Down Expand Up @@ -93,12 +108,7 @@ class TabbedSqlEditors extends React.PureComponent {
render() {
const editors = this.props.queryEditors.map((qe, i) => {
const isSelected = (qe.id === this.activeQueryEditor().id);
const queriesArray = [];
for (const id in this.props.queries) {
if (this.props.queries[id].sqlEditorId === qe.id) {
queriesArray.push(this.props.queries[id]);
}
}

let latestQuery;
if (qe.latestQueryId) {
latestQuery = this.props.queries[qe.latestQueryId];
Expand Down Expand Up @@ -142,7 +152,7 @@ class TabbedSqlEditors extends React.PureComponent {
<SqlEditor
tables={this.props.tables.filter((t) => (t.queryEditorId === qe.id))}
queryEditor={qe}
queries={queriesArray}
queries={this.state.queriesArray}
latestQuery={latestQuery}
database={database}
actions={this.props.actions}
Expand Down
18 changes: 18 additions & 0 deletions caravel/assets/javascripts/reduxUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,21 @@ export function areArraysShallowEqual(arr1, arr2) {
}
return true;
}

export function areObjectsEqual(obj1, obj2) {
if (!obj1 || !obj2) {
return false;
}
if (! Object.keys(obj1).length !== Object.keys(obj2).length) {
return false;
}
for (const id in obj1) {
if (!obj2.hasOwnProperty(id)) {
return false;
}
if (obj1[id] !== obj2[id]) {
return false;
}
}
return true;
}

0 comments on commit 26318f9

Please sign in to comment.