Skip to content

Commit f0900d7

Browse files
committed
refactor such that it only updates ec2 parent node
1 parent cf05c8e commit f0900d7

File tree

4 files changed

+30
-17
lines changed

4 files changed

+30
-17
lines changed

src/ec2/activation.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,17 @@ export async function activate(ctx: ExtContext): Promise<void> {
3030

3131
Commands.register('aws.ec2.startInstance', async (node?: Ec2Node) => {
3232
await startInstance(node)
33-
34-
refreshExplorer()
33+
refreshExplorer(node)
3534
}),
3635

3736
Commands.register('aws.ec2.stopInstance', async (node?: Ec2Node) => {
3837
await stopInstance(node)
39-
refreshExplorer()
38+
refreshExplorer(node)
4039
}),
4140

4241
Commands.register('aws.ec2.rebootInstance', async (node?: Ec2Node) => {
4342
await rebootInstance(node)
44-
refreshExplorer()
43+
refreshExplorer(node)
4544
})
4645
)
4746
}

src/ec2/commands.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,10 @@ import { Ec2ConnectionManager } from './model'
1010
import { Ec2Prompter, instanceFilter } from './prompter'
1111
import { Ec2Selection } from './utils'
1212
import { Ec2Instance } from '../shared/clients/ec2Client'
13-
import { isCloud9 } from '../shared/extensionUtilities'
14-
import { commands } from 'vscode'
1513

1614
export function refreshExplorer(node?: Ec2Node) {
17-
if (isCloud9()) {
18-
commands.executeCommand('aws.refreshAwsExplorer', true)
19-
} else {
20-
commands.executeCommand('aws.refreshAwsExplorerNode', node)
15+
if (node) {
16+
node instanceof Ec2InstanceNode ? node.parent.refreshNode() : node.refreshNode()
2117
}
2218
}
2319

src/ec2/explorer/ec2InstanceNode.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,41 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55
import * as vscode from 'vscode'
6-
import { getNameOfInstance } from '../../shared/clients/ec2Client'
6+
import { Ec2Client, getNameOfInstance } from '../../shared/clients/ec2Client'
77
import { AWSResourceNode } from '../../shared/treeview/nodes/awsResourceNode'
88
import { AWSTreeNodeBase } from '../../shared/treeview/nodes/awsTreeNodeBase'
99
import { Ec2Instance } from '../../shared/clients/ec2Client'
1010
import globals from '../../shared/extensionGlobals'
1111
import { Ec2Selection, getIconCodeForInstanceStatus } from '../utils'
12+
import { Ec2ParentNode } from './ec2ParentNode'
1213

1314
type Ec2InstanceNodeContext = 'awsEc2RunningNode' | 'awsEc2StoppedNode' | 'awsEc2PendingNode'
1415

1516
export class Ec2InstanceNode extends AWSTreeNodeBase implements AWSResourceNode {
1617
public constructor(
18+
public readonly parent: Ec2ParentNode,
19+
public readonly client: Ec2Client,
1720
public override readonly regionCode: string,
1821
private readonly partitionId: string,
1922
private instance: Ec2Instance
2023
) {
2124
super('')
22-
this.update(instance)
25+
this.updateInstance(instance)
2326
}
2427

25-
public update(newInstance: Ec2Instance) {
28+
public updateInstance(newInstance: Ec2Instance) {
2629
this.setInstance(newInstance)
2730
this.label = `${this.name} (${this.InstanceId})`
2831
this.contextValue = this.getContext()
2932
this.iconPath = new vscode.ThemeIcon(getIconCodeForInstanceStatus(this.instance))
3033
this.tooltip = `${this.name}\n${this.InstanceId}\n${this.instance.status}\n${this.arn}`
3134
}
3235

36+
public async updateStatus() {
37+
const newStatus = await this.client.getInstanceStatus(this.InstanceId)
38+
this.updateInstance({ ...this.instance, status: newStatus })
39+
}
40+
3341
private getContext(): Ec2InstanceNodeContext {
3442
if (this.instance.status == 'running') {
3543
return 'awsEc2RunningNode'

src/ec2/explorer/ec2ParentNode.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,19 @@ import { PlaceholderNode } from '../../shared/treeview/nodes/placeholderNode'
99
import { Ec2InstanceNode } from './ec2InstanceNode'
1010
import { Ec2Client } from '../../shared/clients/ec2Client'
1111
import { updateInPlace } from '../../shared/utilities/collectionUtils'
12+
import { Commands } from '../../shared/vscode/commands'
1213

1314
export const parentContextValue = 'awsEc2ParentNode'
1415
export type Ec2Node = Ec2InstanceNode | Ec2ParentNode
1516

1617
export class Ec2ParentNode extends AWSTreeNodeBase {
1718
protected readonly placeHolderMessage = '[No EC2 Instances Found]'
18-
protected readonly ec2InstanceNodes: Map<string, Ec2InstanceNode>
19+
protected ec2InstanceNodes: Map<string, Ec2InstanceNode>
1920
public override readonly contextValue: string = parentContextValue
2021

2122
public constructor(
2223
public override readonly regionCode: string,
23-
private readonly partitionId: string,
24+
public readonly partitionId: string,
2425
protected readonly ec2Client: Ec2Client
2526
) {
2627
super('EC2', vscode.TreeItemCollapsibleState.Collapsed)
@@ -44,8 +45,17 @@ export class Ec2ParentNode extends AWSTreeNodeBase {
4445
updateInPlace(
4546
this.ec2InstanceNodes,
4647
ec2Instances.keys(),
47-
key => this.ec2InstanceNodes.get(key)!.update(ec2Instances.get(key)!),
48-
key => new Ec2InstanceNode(this.regionCode, this.partitionId, ec2Instances.get(key)!)
48+
key => this.ec2InstanceNodes.get(key)!.updateInstance(ec2Instances.get(key)!),
49+
key => new Ec2InstanceNode(this, this.ec2Client, this.regionCode, this.partitionId, ec2Instances.get(key)!)
4950
)
5051
}
52+
53+
public async clearChildren() {
54+
this.ec2InstanceNodes = new Map<string, Ec2InstanceNode>()
55+
}
56+
57+
public async refreshNode(): Promise<void> {
58+
this.clearChildren()
59+
Commands.vscode().execute('aws.refreshAwsExplorerNode', this)
60+
}
5161
}

0 commit comments

Comments
 (0)