From 523887d6c52267d6c41fb18454f86e0b84d4a55f Mon Sep 17 00:00:00 2001 From: Alexandre Poirot Date: Mon, 27 Jun 2022 14:03:19 +0000 Subject: [PATCH] Bug 1775200 - [devtools] Avoid updating CodeMirror unless it is strictly 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 --- .../debugger/src/components/Editor/index.js | 59 +++++++++++-------- 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/devtools/client/debugger/src/components/Editor/index.js b/devtools/client/debugger/src/components/Editor/index.js index 9b7f486c08ec9..d7afb2d846cd4 100644 --- a/devtools/client/debugger/src/components/Editor/index.js +++ b/devtools/client/debugger/src/components/Editor/index.js @@ -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(); @@ -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) {