Skip to content

Commit

Permalink
[navigator] set root and listen to changes only when workspace is ready
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Kosyakov <anton.kosyakov@typefox.io>
  • Loading branch information
akosyakov authored and svenefftinge committed Dec 4, 2019
1 parent 5b45e12 commit c101c61
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 40 deletions.
11 changes: 2 additions & 9 deletions packages/navigator/src/browser/navigator-model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,6 @@ describe('FileNavigatorModel', () => {
toRestore.length = 0;
});

it('should update the root(s) on receiving a WorkspaceChanged event from the WorkspaceService', done => {
sinon.stub(navigatorModel, 'updateRoot').callsFake(() => {
done(); // This test would time out if updateRoot() is not called
});
mockWorkspaceServiceEmitter.fire([]);
}).timeout(2000);

describe('updateRoot() function', () => {
it('should assign "this.root" a WorkspaceNode with WorkspaceRootNodes (one for each root folder in the workspace) as its children', async () => {
sinon.stub(mockWorkspaceService, 'roots').value([folderA, folderB]);
Expand All @@ -215,7 +208,7 @@ describe('FileNavigatorModel', () => {
})
);

await navigatorModel.updateRoot();
await navigatorModel['updateRoot']();
const thisRoot = navigatorModel['root'] as WorkspaceNode;
expect(thisRoot).not.to.be.undefined;
expect(thisRoot.children.length).to.eq(2);
Expand All @@ -226,7 +219,7 @@ describe('FileNavigatorModel', () => {
it('should assign "this.root" undefined if there is no workspace open', async () => {
sinon.stub(mockWorkspaceService, 'opened').value(false);

await navigatorModel.updateRoot();
await navigatorModel['updateRoot']();
const thisRoot = navigatorModel['root'] as WorkspaceNode;
expect(thisRoot).to.be.undefined;
});
Expand Down
40 changes: 27 additions & 13 deletions packages/navigator/src/browser/navigator-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { injectable, inject, postConstruct } from 'inversify';
import URI from '@theia/core/lib/common/uri';
import { FileNode, FileTreeModel } from '@theia/filesystem/lib/browser';
import { OpenerService, open, TreeNode, ExpandableTreeNode } from '@theia/core/lib/browser';
import { OpenerService, open, TreeNode, ExpandableTreeNode, CompositeTreeNode, SelectableTreeNode } from '@theia/core/lib/browser';
import { FileNavigatorTree, WorkspaceRootNode, WorkspaceNode } from './navigator-tree';
import { WorkspaceService } from '@theia/workspace/lib/browser';
import { FrontendApplicationStateService } from '@theia/core/lib/browser/frontend-application-state';
Expand All @@ -32,17 +32,32 @@ export class FileNavigatorModel extends FileTreeModel {

@postConstruct()
protected init(): void {
this.toDispose.push(
this.workspaceService.onWorkspaceChanged(event => {
this.updateRoot();
})
);
this.toDispose.push(
this.workspaceService.onWorkspaceLocationChanged(() => {
this.updateRoot();
})
);
super.init();
this.initializeRoot();
}

protected async initializeRoot(): Promise<void> {
await Promise.all([
this.applicationState.reachedState('initialized_layout'),
this.workspaceService.roots
]);
await this.updateRoot();
if (this.toDispose.disposed) {
return;
}
this.toDispose.push(this.workspaceService.onWorkspaceChanged(() => this.updateRoot()));
this.toDispose.push(this.workspaceService.onWorkspaceLocationChanged(() => this.updateRoot()));
if (this.selectedNodes.length) {
return;
}
const root = this.root;
if (CompositeTreeNode.is(root) && root.children.length === 1) {
const child = root.children[0];
if (SelectableTreeNode.is(child) && !child.selected && ExpandableTreeNode.is(child)) {
this.selectNode(child);
this.expandNode(child);
}
}
}

previewNode(node: TreeNode): void {
Expand Down Expand Up @@ -72,8 +87,7 @@ export class FileNavigatorModel extends FileTreeModel {
}
}

async updateRoot(): Promise<void> {
await this.applicationState.reachedState('initialized_layout');
protected async updateRoot(): Promise<void> {
this.root = await this.createRoot();
}

Expand Down
19 changes: 1 addition & 18 deletions packages/navigator/src/browser/navigator-widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ import { CommandService, SelectionService } from '@theia/core/lib/common';
import { CommonCommands, CorePreferences, LabelProvider, ViewContainerTitleOptions, Key } from '@theia/core/lib/browser';
import {
ContextMenuRenderer, ExpandableTreeNode,
TreeProps, TreeModel, TreeNode,
SelectableTreeNode, CompositeTreeNode
TreeProps, TreeModel, TreeNode
} from '@theia/core/lib/browser';
import { FileTreeWidget, FileNode, DirNode } from '@theia/filesystem/lib/browser';
import { WorkspaceService, WorkspaceCommands } from '@theia/workspace/lib/browser';
Expand Down Expand Up @@ -68,7 +67,6 @@ export class FileNavigatorWidget extends FileTreeWidget {
super(props, model, contextMenuRenderer);
this.id = FILE_NAVIGATOR_ID;
this.addClass(CLASS);
this.initialize();
}

@postConstruct()
Expand All @@ -91,21 +89,6 @@ export class FileNavigatorWidget extends FileTreeWidget {
]);
}

protected async initialize(): Promise<void> {
await this.model.updateRoot();
if (this.model.selectedNodes.length) {
return;
}
const root = this.model.root;
if (CompositeTreeNode.is(root) && root.children.length === 1) {
const child = root.children[0];
if (SelectableTreeNode.is(child) && !child.selected && ExpandableTreeNode.is(child)) {
this.model.selectNode(child);
this.model.expandNode(child);
}
}
}

protected doUpdateRows(): void {
super.doUpdateRows();
this.title.label = LABEL;
Expand Down

0 comments on commit c101c61

Please sign in to comment.