Skip to content

Commit

Permalink
Some refactoring/standardization on tree nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
stevejroberts committed Aug 21, 2018
1 parent 90d26a5 commit 5ae6885
Show file tree
Hide file tree
Showing 13 changed files with 142 additions and 124 deletions.
12 changes: 6 additions & 6 deletions src/lambda/explorer/blueprintNode.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict';

import * as path from 'path';
import { ExplorerNodeBase } from '../../shared/nodes';
import { TreeItem, Uri, ThemeIcon } from 'vscode';
import * as path from 'path';
import { AWSTreeNodeBase } from '../../shared/awsTreeNodeBase';
import { Blueprint } from '../models/blueprint';

export class BlueprintNode extends ExplorerNodeBase implements TreeItem {
export class BlueprintNode extends AWSTreeNodeBase implements TreeItem {
public static contextValue: string = 'awsLambdaBlueprint';
public contextValue: string = BlueprintNode.contextValue;

Expand All @@ -25,11 +25,11 @@ export class BlueprintNode extends ExplorerNodeBase implements TreeItem {
};
}

public getChildren(): BlueprintNode[] {
return [];
public getChildren(): Thenable<BlueprintNode[]> {
return new Promise(resolve => resolve([]));
}

public getTreeItem(): BlueprintNode | Promise<BlueprintNode> {
public getTreeItem(): TreeItem {
return this;
}
}
24 changes: 13 additions & 11 deletions src/lambda/explorer/blueprintsLanguageNode.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
'use strict';

import * as vscode from 'vscode';
import { ExplorerNodeBase } from '../../shared/nodes';
import { TreeItem, TreeItemCollapsibleState } from 'vscode';
import { AWSTreeNodeBase } from '../../shared/awsTreeNodeBase';
import { BlueprintNode } from './blueprintNode';
import { BlueprintsCollection } from '../models/blueprintsCollection';
import { Blueprint } from '../models/blueprint';

export class BlueprintsLanguageNode extends ExplorerNodeBase {
export class BlueprintsLanguageNode extends AWSTreeNodeBase {

constructor(public readonly language: string, public readonly blueprintsCollection: BlueprintsCollection) {
super();
}

public getChildren(): BlueprintNode[] {
let blueprints: BlueprintNode[] = [];
this.blueprintsCollection.filterBlueprintsForLanguage(this.language).forEach((b: Blueprint) => {
blueprints.push(new BlueprintNode(b));
});
public getChildren(): Thenable<BlueprintNode[]> {
return new Promise(resolve => {
let blueprints: BlueprintNode[] = [];
this.blueprintsCollection.filterBlueprintsForLanguage(this.language).forEach((b: Blueprint) => {
blueprints.push(new BlueprintNode(b));
});

return blueprints;
resolve(blueprints);
});
}

public getTreeItem(): vscode.TreeItem | Promise<vscode.TreeItem> {
const item = new vscode.TreeItem(this.language, vscode.TreeItemCollapsibleState.Collapsed);
public getTreeItem(): TreeItem {
const item = new TreeItem(this.language, TreeItemCollapsibleState.Collapsed);
item.tooltip = `Project blueprints for creating new projects targeting AWS Lambda in ${this.language}`;

return item;
Expand Down
12 changes: 6 additions & 6 deletions src/lambda/explorer/functionNode.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict';

import { TreeItem, Uri, ThemeIcon } from 'vscode';
import * as path from 'path';
import { AWSTreeNodeBase } from '../../shared/awsTreeNodeBase';
import Lambda = require('aws-sdk/clients/lambda');
import { ExplorerNodeBase } from '../../shared/nodes';
import { TreeItem, Uri, ThemeIcon } from 'vscode';

export class FunctionNode extends ExplorerNodeBase implements TreeItem {
export class FunctionNode extends AWSTreeNodeBase implements TreeItem {
public static contextValue: string = 'awsLambdaFn';
public contextValue: string = FunctionNode.contextValue;

Expand All @@ -26,11 +26,11 @@ export class FunctionNode extends ExplorerNodeBase implements TreeItem {
};
}

public getChildren(): FunctionNode[] {
return [];
public getChildren(): Thenable<FunctionNode[]> {
return new Promise(resolve => resolve([]));
}

public getTreeItem(): FunctionNode | Promise<FunctionNode> {
public getTreeItem(): TreeItem {
return this;
}
}
22 changes: 15 additions & 7 deletions src/lambda/explorer/functionsNode.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
'use strict';

import * as vscode from 'vscode';
import { ExplorerNodeBase } from '../../shared/nodes';
import { TreeItem, TreeItemCollapsibleState } from 'vscode';
import { AWSTreeNodeBase } from '../../shared/awsTreeNodeBase';
import { FunctionNode } from './functionNode';
import { ext } from '../../shared/extensionGlobals';
import Lambda = require('aws-sdk/clients/lambda');
import { listLambdas } from '../utils';

export class FunctionsNode extends ExplorerNodeBase {
export class FunctionsNode extends AWSTreeNodeBase {
public static contextValue: string = 'awsLambdaFns';
public readonly contextValue: string = FunctionsNode.contextValue;
public readonly label: string = 'Lambda Functions';

public async getChildren(): Promise<ExplorerNodeBase[]> {
return await listLambdas(await ext.sdkClientBuilder.createAndConfigureSdkClient(Lambda, undefined));
public getChildren(): Thenable<AWSTreeNodeBase[]> {
return new Promise(resolve => {
this.queryDeployedLambdaFunctions().then((result) => resolve(result));
});
}

public getTreeItem(): vscode.TreeItem | Promise<vscode.TreeItem> {
const item = new vscode.TreeItem('Functions', vscode.TreeItemCollapsibleState.Collapsed);
public getTreeItem(): TreeItem {
const item = new TreeItem('Functions', TreeItemCollapsibleState.Collapsed);
item.tooltip = 'My deployed Lambda functions';

return item;
}

private async queryDeployedLambdaFunctions() : Promise<FunctionNode[]> {
const client = await ext.sdkClientBuilder.createAndConfigureSdkClient(Lambda, undefined);
return await listLambdas(client);
}
}
14 changes: 7 additions & 7 deletions src/lambda/explorer/guideNode.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict';

import * as vscode from 'vscode';
import { TreeItem, TreeItemCollapsibleState } from 'vscode';
import * as path from 'path';
import { ExplorerNodeBase } from '../../shared/nodes';
import { AWSTreeNodeBase } from '../../shared/awsTreeNodeBase';
import { URL } from 'url';

export class GuideNode extends ExplorerNodeBase {
export class GuideNode extends AWSTreeNodeBase {

public static contextValue: string = 'awsLambdaGuide';
public contextValue: string = GuideNode.contextValue;
Expand All @@ -17,12 +17,12 @@ export class GuideNode extends ExplorerNodeBase {
super();
}

public getChildren(): ExplorerNodeBase[] | Promise<ExplorerNodeBase[]> {
return [];
public getChildren(): Thenable<AWSTreeNodeBase[]> {
return new Promise(resolve => resolve([]));
}

public getTreeItem(): vscode.TreeItem | Promise<vscode.TreeItem> {
const item = new vscode.TreeItem(`${this.guideName}`, vscode.TreeItemCollapsibleState.Collapsed);
public getTreeItem(): TreeItem {
const item = new TreeItem(`${this.guideName}`, TreeItemCollapsibleState.Collapsed);
item.tooltip = `${this.guideUri}`;
item.iconPath = {
light: path.join(__filename, '..', '..', '..', 'resources', 'light', 'lambda_function.svg'),
Expand Down
16 changes: 8 additions & 8 deletions src/lambda/explorer/guidesNode.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
'use strict';

import * as vscode from 'vscode';
import { ExplorerNodeBase } from '../../shared/nodes';
import { TreeItem, TreeItemCollapsibleState } from 'vscode';
import { AWSTreeNodeBase } from '../../shared/awsTreeNodeBase';
import { GuideNode } from './guideNode';
import { URL } from 'url';

export class GuidesNode extends ExplorerNodeBase {
export class GuidesNode extends AWSTreeNodeBase {

rootNodes: ExplorerNodeBase[] = [
rootNodes: AWSTreeNodeBase[] = [
new GuideNode('Developer Guide', new URL('https://docs.aws.amazon.com/lambda/latest/dg/welcome.html')),
new GuideNode('API Reference', new URL('https://docs.aws.amazon.com/lambda/latest/dg/API_Reference.html'))
];

public getChildren(): ExplorerNodeBase[] | Promise<ExplorerNodeBase[]> {
return this.rootNodes;
public getChildren(): Thenable<AWSTreeNodeBase[]> {
return new Promise(resolve => resolve(this.rootNodes));
}

public getTreeItem(): vscode.TreeItem | Promise<vscode.TreeItem> {
const item = new vscode.TreeItem('Reference Guides', vscode.TreeItemCollapsibleState.Collapsed);
public getTreeItem(): TreeItem {
const item = new TreeItem('Reference Guides', TreeItemCollapsibleState.Collapsed);
item.tooltip = 'Reference materials for working with AWS Lambda';

return item;
Expand Down
12 changes: 6 additions & 6 deletions src/lambda/explorer/projectBlueprintsNode.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
'use strict';

import * as vscode from 'vscode';
import { ExplorerNodeBase } from '../../shared/nodes';
import { TreeItem, TreeItemCollapsibleState } from 'vscode';
import { AWSTreeNodeBase } from '../../shared/awsTreeNodeBase';
import { BlueprintsCollection } from '../models/blueprintsCollection';
import { BlueprintsLanguageNode } from './blueprintsLanguageNode';

export class ProjectBlueprintsNode extends ExplorerNodeBase {
export class ProjectBlueprintsNode extends AWSTreeNodeBase {

private allBlueprints: BlueprintsCollection = new BlueprintsCollection();

public async getChildren(): Promise<ExplorerNodeBase[]> {
public async getChildren(): Promise<AWSTreeNodeBase[]> {

await this.allBlueprints.loadAllBlueprints(); // to date we do VS blueprints only

Expand All @@ -22,8 +22,8 @@ export class ProjectBlueprintsNode extends ExplorerNodeBase {
return languageNodes;
}

public getTreeItem(): vscode.TreeItem | Promise<vscode.TreeItem> {
const item = new vscode.TreeItem('Project Blueprints', vscode.TreeItemCollapsibleState.Collapsed);
public getTreeItem(): TreeItem {
const item = new TreeItem('Project Blueprints', TreeItemCollapsibleState.Collapsed);
item.tooltip = 'Blueprints for creating new projects targeting AWS Lambda';

return item;
Expand Down
15 changes: 8 additions & 7 deletions src/lambda/lambdaProvider.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';

import * as vscode from 'vscode';
import { ExplorerNodeBase, IRefreshableAWSTreeProvider } from '../shared/nodes';
import { AWSTreeNodeBase } from '../shared/awsTreeNodeBase';
import { IRefreshableAWSTreeProvider } from '../shared/IAWSTreeProvider';
import { FunctionsNode } from './explorer/functionsNode';
import { GuidesNode } from './explorer/guidesNode';
import { ProjectBlueprintsNode } from './explorer/projectBlueprintsNode';
Expand All @@ -14,7 +15,7 @@ import { getLambdaConfig } from './commands/getLambdaConfig';
import { AWSContext } from '../shared/awsContext';
import { ext } from '../shared/extensionGlobals';

export class LambdaProvider implements vscode.TreeDataProvider<ExplorerNodeBase>, IRefreshableAWSTreeProvider {
export class LambdaProvider implements vscode.TreeDataProvider<AWSTreeNodeBase>, IRefreshableAWSTreeProvider {
private _onDidChangeTreeData: vscode.EventEmitter<FunctionNode | undefined> = new vscode.EventEmitter<FunctionNode | undefined>();
readonly onDidChangeTreeData: vscode.Event<FunctionNode | undefined> = this._onDidChangeTreeData.event;

Expand All @@ -30,25 +31,25 @@ export class LambdaProvider implements vscode.TreeDataProvider<ExplorerNodeBase>
ext.treesToRefreshOnContextChange.push(this);
}

rootNodes: ExplorerNodeBase[] = [
rootNodes: AWSTreeNodeBase[] = [
new FunctionsNode(),
new GuidesNode(),
new ProjectBlueprintsNode()
];

getTreeItem(element: any): vscode.TreeItem | Thenable<vscode.TreeItem> {
getTreeItem(element: AWSTreeNodeBase): vscode.TreeItem {
return element.getTreeItem();
}

getChildren(element?: any): vscode.ProviderResult<any[]> {
getChildren(element?: AWSTreeNodeBase): Thenable<AWSTreeNodeBase[]> {
if (element) {
return element.getChildren();
}

return this.rootNodes;
return new Promise(resolve => resolve(this.rootNodes));
}

refresh(context: AWSContext) {
refresh(context?: AWSContext) {
this._onDidChangeTreeData.fire();
}

Expand Down
14 changes: 14 additions & 0 deletions src/shared/IAWSTreeProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

import { AWSContext } from "./awsContext";

export interface IAWSTreeProvider {
viewProviderId: string;

initialize(): void;
}

export interface IRefreshableAWSTreeProvider extends IAWSTreeProvider {
refresh(newContext?: AWSContext): void;
}

43 changes: 43 additions & 0 deletions src/shared/awsTreeNodeBase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';

import { Command, Disposable, TreeItem } from 'vscode';
import { AWSContext } from './awsContext';

export abstract class AWSTreeNodeBase extends Disposable {

public readonly supportsPaging: boolean = false;

protected children: AWSTreeNodeBase[] | undefined;
protected disposable: Disposable | undefined;

constructor() {
super(() => this.dispose());
}

dispose() {
if (this.disposable !== undefined) {
this.disposable.dispose();
this.disposable = undefined;
}

this.resetChildren();
}

public abstract getTreeItem(): TreeItem;

public abstract getChildren(): Thenable<AWSTreeNodeBase[]>;

public getCommand(): Command | undefined {
return undefined;
}

public refresh(newContext: AWSContext): void { }

public resetChildren(): void {
if (this.children !== undefined) {
this.children.forEach(c => c.dispose());
this.children = undefined;
}
}
}

2 changes: 1 addition & 1 deletion src/shared/extensionGlobals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { ExtensionContext, OutputChannel } from 'vscode';
import { AWSClientBuilder } from './awsClientBuilder';
import { AWSContext } from './awsContext';
import { IRefreshableAWSTreeProvider } from './nodes';
import { IRefreshableAWSTreeProvider } from './IAWSTreeprovider';
import { AWSStatusBar } from './statusBar';

/**
Expand Down
Loading

0 comments on commit 5ae6885

Please sign in to comment.