Skip to content

Commit

Permalink
Allow async refresh of label (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
ejizba authored Jan 6, 2018
1 parent de8fd9c commit d7001d5
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 21 deletions.
7 changes: 4 additions & 3 deletions ui/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export declare class AzureTreeDataProvider implements TreeDataProvider<IAzureNod
constructor(resourceProvider: IChildProvider, loadMoreCommandId: string, rootTreeItems?: IAzureTreeItem[]);
public getTreeItem(node: IAzureNode): TreeItem;
public getChildren(node?: IAzureParentNode): Promise<IAzureNode[]>;
public refresh(node?: IAzureNode, clearCache?: boolean): void;
public refresh(node?: IAzureNode, clearCache?: boolean): Promise<void>;
public loadMore(node: IAzureNode): Promise<void>;
public showNodePicker(expectedContextValue: string): Promise<IAzureNode>;
public dispose(): void;
Expand All @@ -44,7 +44,7 @@ export interface IAzureNode<T extends IAzureTreeItem = IAzureTreeItem> {
/**
* Refresh this node in the tree
*/
refresh(): void;
refresh(): Promise<void>;

/**
* This class wraps IAzureTreeItem.deleteTreeItem and ensures the tree is updated correctly when an item is deleted
Expand Down Expand Up @@ -79,6 +79,7 @@ export interface IAzureTreeItem {
commandId?: string;
contextValue: string;
deleteTreeItem?(node: IAzureNode): Promise<void>;
refreshLabel?(node: IAzureNode): Promise<void>;
}

export interface IChildProvider {
Expand All @@ -105,7 +106,7 @@ export interface IAzureParentTreeItem extends IAzureTreeItem, IChildProvider {
* If this treeItem should not show up in the node picker, implement this to provide a child that corresponds to the expectedContextValue
* Otherwise, all children will be shown in the node picker
*/
pickTreeItem?(expectedContextValue: string): IAzureParentTreeItem | undefined;
pickTreeItem?(expectedContextValue: string): IAzureTreeItem | undefined;
}

export declare class UserCancelledError extends Error { }
Expand Down
2 changes: 1 addition & 1 deletion ui/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "vscode-azureextensionui",
"author": "Microsoft Corporation",
"version": "0.5.0",
"version": "0.5.1",
"description": "Common UI tools for developing Azure extensions for VS Code",
"tags": [
"azure",
Expand Down
12 changes: 8 additions & 4 deletions ui/src/treeDataProvider/AzureNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,12 @@ export class AzureNode<T extends IAzureTreeItem = IAzureTreeItem> implements IAz
}
}

public refresh(): void {
this.treeDataProvider.refresh(this.parent, false);
public async refresh(): Promise<void> {
if (this.treeItem.refreshLabel) {
await this.treeItem.refreshLabel(this);
}

await this.treeDataProvider.refresh(this.parent, false /* clearCache */);
}

public openInPortal(): void {
Expand All @@ -78,7 +82,7 @@ export class AzureNode<T extends IAzureTreeItem = IAzureTreeItem> implements IAz
if (this.treeItem.deleteTreeItem) {
await this.treeItem.deleteTreeItem(this);
if (this.parent) {
this.parent.removeNodeFromCache(this);
await this.parent.removeNodeFromCache(this);
}
} else {
throw new NotImplementedError('deleteTreeItem', this.treeItem);
Expand All @@ -87,5 +91,5 @@ export class AzureNode<T extends IAzureTreeItem = IAzureTreeItem> implements IAz
}

export interface IAzureParentNodeInternal extends IAzureParentNode {
removeNodeFromCache(node: AzureNode): void;
removeNodeFromCache(node: AzureNode): Promise<void>;
}
15 changes: 8 additions & 7 deletions ui/src/treeDataProvider/AzureParentNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,17 @@ export class AzureParentNode<T extends IAzureParentTreeItem = IAzureParentTreeIt
const newTreeItem: IAzureTreeItem = await this.treeItem.createChild(this, (label: string): void => {
creatingNode = new AzureNode(this, new CreatingTreeItem(label));
this._creatingNodes.push(creatingNode);
//tslint:disable-next-line:no-floating-promises
this.treeDataProvider.refresh(this, false);
});

const newNode: AzureNode = this.createNewNode(newTreeItem);
this.addNodeToCache(newNode);
await this.addNodeToCache(newNode);
return newNode;
} finally {
if (creatingNode) {
this._creatingNodes.splice(this._creatingNodes.indexOf(creatingNode), 1);
this.treeDataProvider.refresh(this, false);
await this.treeDataProvider.refresh(this, false);
}
}
} else {
Expand Down Expand Up @@ -84,19 +85,19 @@ export class AzureParentNode<T extends IAzureParentTreeItem = IAzureParentTreeIt
return await pick.data();
}

public addNodeToCache(node: AzureNode): void {
public async addNodeToCache(node: AzureNode): Promise<void> {
if (this._cachedChildren) {
this._cachedChildren.unshift(node);
this.treeDataProvider.refresh(this, false);
await this.treeDataProvider.refresh(this, false);
}
}

public removeNodeFromCache(node: AzureNode): void {
public async removeNodeFromCache(node: AzureNode): Promise<void> {
if (this._cachedChildren) {
const index: number = this._cachedChildren.indexOf(node);
if (index !== -1) {
this._cachedChildren.splice(index, 1);
this.treeDataProvider.refresh(this, false);
await this.treeDataProvider.refresh(this, false);
}
}
}
Expand All @@ -115,7 +116,7 @@ export class AzureParentNode<T extends IAzureParentTreeItem = IAzureParentTreeIt
picks.push(new PickWithData<GetNodeFunction>(
async (): Promise<AzureNode> => {
await this.loadMoreChildren();
this.treeDataProvider.refresh(this, false);
await this.treeDataProvider.refresh(this, false);
return this;
},
LoadMoreTreeItem.label
Expand Down
18 changes: 12 additions & 6 deletions ui/src/treeDataProvider/AzureTreeDataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ export class AzureTreeDataProvider implements TreeDataProvider<IAzureNode>, Disp
this._azureAccount = azureAccountExtension.exports;
}

this._disposables.push(this._azureAccount.onFiltersChanged(() => this.refresh()));
this._disposables.push(this._azureAccount.onStatusChanged((status: AzureLoginStatus) => {
this._disposables.push(this._azureAccount.onFiltersChanged(async () => await this.refresh()));
this._disposables.push(this._azureAccount.onStatusChanged(async (status: AzureLoginStatus) => {
// Ignore status change to 'LoggedIn' and wait for the 'onFiltersChanged' event to fire instead
// (so that the tree stays in 'Loading...' state until the filters are actually ready)
if (status !== 'LoggedIn') {
this.refresh();
await this.refresh();
}
}));
}
Expand Down Expand Up @@ -122,9 +122,15 @@ export class AzureTreeDataProvider implements TreeDataProvider<IAzureNode>, Disp
}
}

public refresh(node?: IAzureNode, clearCache: boolean = true): void {
if (node instanceof AzureParentNode && clearCache) {
node.clearCache();
public async refresh(node?: IAzureNode, clearCache: boolean = true): Promise<void> {
if (clearCache) {
if (node && node.treeItem.refreshLabel) {
await node.treeItem.refreshLabel(node);
}

if (node instanceof AzureParentNode) {
node.clearCache();
}
}

this._onDidChangeTreeDataEmitter.fire(node);
Expand Down

0 comments on commit d7001d5

Please sign in to comment.