Skip to content

Commit

Permalink
Bug 1775200 - [devtools] Avoid updating CodeMirror unless it is stric…
Browse files Browse the repository at this point in the history
…tly necessary. r=bomsy

There is many props that are only relevant to the children components,
and we were calling expensive CodeMirror method on any prop change.

Differential Revision: https://phabricator.services.mozilla.com/D149853
  • Loading branch information
ochameau committed Jun 27, 2022
1 parent 3187ab6 commit 523887d
Showing 1 changed file with 33 additions and 26 deletions.
59 changes: 33 additions & 26 deletions devtools/client/debugger/src/components/Editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,33 @@ class Editor extends PureComponent {
editor = this.setupEditor();
}

startOperation();
this.setText(nextProps, editor);
this.setSize(nextProps, editor);
this.scrollToLocation(nextProps, editor);
endOperation();
const shouldUpdateText =
nextProps.selectedSource !== this.props.selectedSource ||
nextProps.selectedSourceTextContent !==
this.props.selectedSourceTextContent ||
nextProps.symbols !== this.props.symbols;

const shouldUpdateSize =
nextProps.startPanelSize !== this.props.startPanelSize ||
nextProps.endPanelSize !== this.props.endPanelSize;

const shouldScroll =
nextProps.selectedLocation &&
this.shouldScrollToLocation(nextProps, editor);

if (shouldUpdateText || shouldUpdateSize || shouldScroll) {
startOperation();
if (shouldUpdateText) {
this.setText(nextProps, editor);
}
if (shouldUpdateSize) {
editor.codeMirror.setSize();
}
if (shouldScroll) {
this.scrollToLocation(nextProps, editor);
}
endOperation();
}

if (this.props.selectedSource != nextProps.selectedSource) {
this.props.updateViewport();
Expand Down Expand Up @@ -555,30 +577,15 @@ class Editor extends PureComponent {
scrollToLocation(nextProps, editor) {
const { selectedLocation, selectedSource } = nextProps;

if (selectedLocation && this.shouldScrollToLocation(nextProps, editor)) {
let { line, column } = toEditorPosition(selectedLocation);

if (selectedSource && hasDocument(selectedSource.id)) {
const doc = getDocument(selectedSource.id);
const lineText = doc.getLine(line);
column = Math.max(column, getIndentation(lineText));
}
let { line, column } = toEditorPosition(selectedLocation);

scrollToColumn(editor.codeMirror, line, column);
}
}

setSize(nextProps, editor) {
if (!editor) {
return;
if (selectedSource && hasDocument(selectedSource.id)) {
const doc = getDocument(selectedSource.id);
const lineText = doc.getLine(line);
column = Math.max(column, getIndentation(lineText));
}

if (
nextProps.startPanelSize !== this.props.startPanelSize ||
nextProps.endPanelSize !== this.props.endPanelSize
) {
editor.codeMirror.setSize();
}
scrollToColumn(editor.codeMirror, line, column);
}

setText(props, editor) {
Expand Down

0 comments on commit 523887d

Please sign in to comment.