Skip to content

Commit

Permalink
fix: improve filetree snapshot recovery progress
Browse files Browse the repository at this point in the history
  • Loading branch information
erha19 committed Apr 1, 2022
1 parent 009ccd9 commit 8cc089d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
1 change: 0 additions & 1 deletion packages/file-tree-next/src/browser/file-tree.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
ITreeNodeOrCompositeTreeNode,
IWatcherEvent,
TreeNodeType,
CompositeTreeNode,
} from '@opensumi/ide-components';
import {
CommandService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export class FileTreeModelService {
private _isMultiSelected = false;

private _loadSnapshotReady: Promise<void>;
private loadSnapshotDeferred: Deferred<void> = new Deferred();

private _explorerStorage: IStorage;

Expand All @@ -171,6 +172,8 @@ export class FileTreeModelService {
private treeStateWatcher: TreeStateWatcher;
private willSelectedNodePath: string | null;

private _initTreeModelReady = false;

get onDidFocusedFileChange() {
return this.onDidFocusedFileChangeEmitter.event;
}
Expand All @@ -188,7 +191,7 @@ export class FileTreeModelService {
}

get loadSnapshotReady() {
return this._loadSnapshotReady;
return this.loadSnapshotDeferred.promise;
}

get fileTreeHandle() {
Expand Down Expand Up @@ -285,21 +288,19 @@ export class FileTreeModelService {
return this.fileTreeService.contextKey;
}

get initTreeModelReady() {
return this._initTreeModelReady;
}

async initTreeModel() {
this._initTreeModelReady = false;
// 根据是否为多工作区创建不同根节点
const root = (await this.fileTreeService.resolveChildren())[0];
if (!root) {
this._whenReady.resolve();
return;
}
this._treeModel = this.injector.get<any>(FileTreeModel, [root]);
this._explorerStorage = await this.storageProvider(STORAGE_NAMESPACE.EXPLORER);
// 获取上次文件树的状态
const snapshot = this.explorerStorage.get<ISerializableState>(FileTreeModelService.FILE_TREE_SNAPSHOT_KEY);
if (snapshot) {
// 初始化时。以右侧编辑器打开的文件进行定位
this._loadSnapshotReady = this.loadFileTreeSnapshot(snapshot);
}
this.initDecorations(root);
// _dndService依赖装饰器逻辑加载
this._dndService = this.injector.get<any>(DragAndDropService, [this]);
Expand All @@ -321,6 +322,9 @@ export class FileTreeModelService {
);
this.disposableCollection.push(
this.fileTreeService.onNodeRefreshed(() => {
if (!this.initTreeModelReady) {
return;
}
if (!this.refreshedActionDelayer.isTriggered) {
this.refreshedActionDelayer.cancel();
}
Expand All @@ -346,6 +350,9 @@ export class FileTreeModelService {
);
this.disposableCollection.push(
this.treeModel?.onWillUpdate(() => {
if (!this.initTreeModelReady) {
return;
}
// 更新树前更新下选中节点
if (this.willSelectedNodePath) {
const node = this.fileTreeService.getNodeByPathOrUri(this.willSelectedNodePath);
Expand Down Expand Up @@ -375,6 +382,9 @@ export class FileTreeModelService {
// 当labelService注册的对应节点图标变化时,通知视图更新
this.disposableCollection.push(
this.labelService.onDidChange(async () => {
if (this.initTreeModelReady) {
return;
}
if (!this.labelChangedDelayer.isTriggered()) {
this.labelChangedDelayer.cancel();
}
Expand All @@ -393,13 +403,26 @@ export class FileTreeModelService {
this.loadingDecoration.removeTarget(target);
}),
);
await this.loadSnapshotReady;
this._explorerStorage = await this.storageProvider(STORAGE_NAMESPACE.EXPLORER);
// 获取上次文件树的状态
const snapshot = this.explorerStorage.get<ISerializableState>(FileTreeModelService.FILE_TREE_SNAPSHOT_KEY);
if (snapshot) {
if (this.loadSnapshotDeferred) {
this.loadSnapshotDeferred.resolve();
}
this.loadSnapshotDeferred = new Deferred();
// 初始化时。以右侧编辑器打开的文件进行定位
this._loadSnapshotReady = this.loadFileTreeSnapshot(snapshot);
}
await this._loadSnapshotReady;
this.loadSnapshotDeferred.resolve();
// 先加载快照后再监听文件变化,同时操作会出现Tree更新后节点无法对齐问题
// 即找到插入节点位置为 0,导致重复问题
this.fileTreeService.startWatchFileEvent();
this.onFileTreeModelChangeEmitter.fire(this._treeModel);

this._whenReady.resolve();
this._initTreeModelReady = true;
}

initDecorations(root) {
Expand Down

0 comments on commit 8cc089d

Please sign in to comment.