Skip to content

Commit

Permalink
Revert "chore: optimize treeview re-render (#2833)"
Browse files Browse the repository at this point in the history
This reverts commit d988f14.
  • Loading branch information
Aaaaash committed Jul 26, 2023
1 parent 80f849d commit 9e616cd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import React, {
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from 'react';

import { Injector } from '@opensumi/di';
Expand Down Expand Up @@ -38,8 +38,8 @@ export interface ExtensionTabBarTreeViewProps {

export const ExtensionTabBarTreeView = observer(
({ viewState, model, dataProvider, treeViewId }: PropsWithChildren<ExtensionTabBarTreeViewProps>) => {
const isReady = useRef<boolean>(false);
const isEmpty = useRef<boolean>(dataProvider.isTreeEmpty);
const [isReady, setIsReady] = useState<boolean>(false);
const [isEmpty, setIsEmpty] = useState(dataProvider.isTreeEmpty);
const layoutService = useInjectable<IMainLayoutService>(IMainLayoutService);
const decorationService = useInjectable<IDecorationsService>(IDecorationsService);
const accordionService = useMemo(() => layoutService.getViewAccordionService(treeViewId), []);
Expand All @@ -54,7 +54,7 @@ export const ExtensionTabBarTreeView = observer(

useEffect(() => {
const disposable = dataProvider.onDidChangeEmpty(() => {
isEmpty.current = dataProvider.isTreeEmpty;
setIsEmpty(dataProvider.isTreeEmpty);
});
return () => disposable.dispose();
}, []);
Expand Down Expand Up @@ -193,7 +193,7 @@ export const ExtensionTabBarTreeView = observer(
await model.treeModel.ensureReady;
}
if (!unmouted) {
isReady.current = true;
setIsReady(true);
}
})();
return () => {
Expand Down Expand Up @@ -222,8 +222,8 @@ export const ExtensionTabBarTreeView = observer(
data-tree-view-id={treeViewId}
>
<TreeView
isReady={isReady.current}
isEmpty={isEmpty.current}
isReady={isReady}
isEmpty={isEmpty}
height={height}
handleTreeReady={handleTreeReady}
handleItemClicked={handleItemClicked}
Expand Down
25 changes: 12 additions & 13 deletions packages/file-tree-next/src/browser/file-tree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ export const FILE_TREE_FILTER_DELAY = 500;
const FilterableRecycleTree = RecycleTreeFilterDecorator(RecycleTree);

export const FileTree = ({ viewState }: PropsWithChildren<{ viewState: ViewState }>) => {
const isReady = useRef<boolean>(false);
const isLoading = useRef<boolean>(true);
const [isReady, setIsReady] = useState<boolean>(false);
const [isLoading, setIsLoading] = useState<boolean>(true);
const [outerActive, setOuterActive] = useState<boolean>(false);
const [outerDragOver, setOuterDragOver] = useState<boolean>(false);
const [model, setModel] = useState<TreeModel>();
Expand Down Expand Up @@ -142,13 +142,13 @@ export const FileTree = ({ viewState }: PropsWithChildren<{ viewState: ViewState
// 首次初始化完成时,监听后续变化,适配工作区变化事件
// 监听工作区变化
fileTreeModelService.onFileTreeModelChange(async (treeModel) => {
isLoading.current = true;
setIsLoading(true);
if (treeModel) {
// 确保数据初始化完毕,减少初始化数据过程中多次刷新视图
await treeModel.ensureReady;
}
isLoading.current = false;
setModel(treeModel);
setIsLoading(false);
});
}
}, [isReady]);
Expand Down Expand Up @@ -264,28 +264,27 @@ export const FileTree = ({ viewState }: PropsWithChildren<{ viewState: ViewState

const ensureIsReady = useCallback(
async (token: CancellationToken) => {
isReady.current = false;
await fileTreeModelService.whenReady;
if (token.isCancellationRequested) {
return;
}
// 文件服务已经初始化完毕,但文件树数据仍需要加载
isReady.current = true;
if (fileTreeModelService.treeModel) {
// 确保数据初始化完毕,减少初始化数据过程中多次刷新视图
await fileTreeModelService.treeModel.ensureReady;
setModel(fileTreeModelService.treeModel);
if (token.isCancellationRequested) {
return;
}
setModel(fileTreeModelService.treeModel);
// 文件树数据加载完毕
isLoading.current = false;
if (wrapperRef.current) {
fileTreeService.initContextKey(wrapperRef.current);
}
}
setIsLoading(false);
if (!disposableRef.current?.disposed) {
setIsReady(true);
}
},
[fileTreeModelService],
[fileTreeModelService, disposableRef.current],
);

const handleTreeReady = useCallback(
Expand Down Expand Up @@ -366,8 +365,8 @@ export const FileTree = ({ viewState }: PropsWithChildren<{ viewState: ViewState
onDrop={handleOuterDrop}
>
<FileTreeView
isLoading={isLoading.current}
isReady={isReady.current}
isLoading={isLoading}
isReady={isReady}
height={height}
model={model}
iconTheme={iconTheme}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import clx from 'classnames';
import { observer } from 'mobx-react-lite';
import React, { FC, useState, useRef, createRef, RefObject, useEffect, useCallback, memo } from 'react';
import React, { FC, useState, createRef, RefObject, useEffect, useCallback, memo } from 'react';

import { RecycleTree, IRecycleTreeHandle, TreeNodeType, TreeModel } from '@opensumi/ide-components';
import { isOSX } from '@opensumi/ide-core-browser';
Expand All @@ -20,7 +20,7 @@ export const SCMResourceTree: FC<{
width: number;
height: number;
}> = observer(({ height }) => {
const isReady = useRef<boolean>(false);
const [isReady, setIsReady] = useState<boolean>(false);
const [model, setModel] = useState<TreeModel>();

const wrapperRef: RefObject<HTMLDivElement> = createRef();
Expand All @@ -39,7 +39,7 @@ export const SCMResourceTree: FC<{
// 这里需要重新取一下treeModel的值确保为最新的TreeModel
await scmTreeModelService.treeModel.ensureReady;
}
isReady.current = true;
setIsReady(true);
})();
}, []);

Expand Down Expand Up @@ -142,7 +142,7 @@ export const SCMResourceTree: FC<{
data-name={TREE_FIELD_NAME}
>
<SCMTreeView
isReady={isReady.current}
isReady={isReady}
model={model}
height={height}
onTreeReady={handleTreeReady}
Expand Down

0 comments on commit 9e616cd

Please sign in to comment.