Skip to content

Commit 76eb0fd

Browse files
committed
refactor such that only instance node is refreshed
1 parent dd82ff9 commit 76eb0fd

File tree

5 files changed

+31
-13
lines changed

5 files changed

+31
-13
lines changed

src/ec2/activation.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +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-
refreshExplorer(node)
33+
await refreshExplorer(node)
3434
}),
3535

3636
Commands.register('aws.ec2.stopInstance', async (node?: Ec2Node) => {
3737
await stopInstance(node)
38-
refreshExplorer(node)
38+
await refreshExplorer(node)
3939
}),
4040

4141
Commands.register('aws.ec2.rebootInstance', async (node?: Ec2Node) => {
4242
await rebootInstance(node)
43-
refreshExplorer(node)
43+
await refreshExplorer(node)
4444
})
4545
)
4646
}

src/ec2/commands.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ import { Ec2Prompter, instanceFilter } from './prompter'
1111
import { Ec2Selection } from './utils'
1212
import { Ec2Instance } from '../shared/clients/ec2Client'
1313

14-
export function refreshExplorer(node?: Ec2Node) {
15-
if (node) {
16-
node instanceof Ec2InstanceNode ? node.parent.refreshNode() : node.refreshNode()
17-
}
14+
export async function refreshExplorer(node?: Ec2Node) {
15+
await node?.refreshNode()
1816
}
1917

2018
export async function openTerminal(node?: Ec2Node) {

src/ec2/explorer/ec2InstanceNode.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { Ec2Instance } from '../../shared/clients/ec2Client'
1010
import globals from '../../shared/extensionGlobals'
1111
import { Ec2Selection, getIconCodeForInstanceStatus } from '../utils'
1212
import { Ec2ParentNode } from './ec2ParentNode'
13+
import { Commands } from '../../shared/vscode/commands'
1314

1415
export const Ec2InstanceRunningContext = 'awsEc2RunningNode'
1516
export const Ec2InstanceStoppedContext = 'awsEc2StoppedNode'
@@ -90,4 +91,9 @@ export class Ec2InstanceNode extends AWSTreeNodeBase implements AWSResourceNode
9091
this.regionCode
9192
}:${globals.awsContext.getCredentialAccountId()}:instance/${this.InstanceId}`
9293
}
94+
95+
public async refreshNode(): Promise<void> {
96+
await this.updateStatus()
97+
Commands.vscode().execute('aws.refreshAwsExplorerNode', this)
98+
}
9399
}

src/ec2/explorer/ec2ParentNode.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,19 @@ export class Ec2ParentNode extends AWSTreeNodeBase {
6565
this.pollTimer ?? globals.clock.setInterval(this.updatePollingNodes.bind(this), pollingInterval)
6666
}
6767

68-
public updatePollingNodes() {
68+
private checkForPendingNodes() {
6969
this.pollingNodes.forEach(async instanceId => {
7070
const childNode = this.ec2InstanceNodes.get(instanceId)!
7171
await childNode.updateStatus()
72-
7372
if (!childNode.isPending()) {
7473
this.pollingNodes.delete(instanceId)
74+
childNode.refreshNode()
7575
}
7676
})
77-
this.refreshNode()
77+
}
78+
79+
public updatePollingNodes() {
80+
this.checkForPendingNodes()
7881
if (!this.isPolling()) {
7982
this.clearPollTimer()
8083
}

src/test/ec2/explorer/ec2ParentNode.test.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ describe('ec2ParentNode', function () {
2323
let clock: FakeTimers.InstalledClock
2424
let refreshStub: sinon.SinonStub<[], Promise<void>>
2525
let clearTimerStub: sinon.SinonStub<[], void>
26+
let statusUpdateFromClient: string
2627

2728
const testRegion = 'testRegion'
2829
const testPartition = 'testPartition'
@@ -38,13 +39,14 @@ describe('ec2ParentNode', function () {
3839
}))
3940
)
4041
)
42+
client.getInstanceStatus.callsFake(async () => statusUpdateFromClient)
4143

4244
return client
4345
}
4446

4547
before(function () {
4648
clock = installFakeClock()
47-
refreshStub = sinon.stub(Ec2ParentNode.prototype, 'refreshNode')
49+
refreshStub = sinon.stub(Ec2InstanceNode.prototype, 'refreshNode')
4850
clearTimerStub = sinon.stub(Ec2ParentNode.prototype, 'clearPollTimer')
4951
})
5052

@@ -126,15 +128,24 @@ describe('ec2ParentNode', function () {
126128
assert.strictEqual(testNode.pollingNodes.size, 1)
127129
})
128130

129-
it('refreshes explorer when timer goes off', async function () {
131+
it('does not refresh explorer when timer goes off if status unchanged', async function () {
132+
statusUpdateFromClient = 'pending'
130133
instances = [
131134
{ name: 'firstOne', InstanceId: '0', status: 'pending' },
132135
{ name: 'secondOne', InstanceId: '1', status: 'stopped' },
133136
{ name: 'thirdOne', InstanceId: '2', status: 'running' },
134137
]
135138
await testNode.updateChildren()
136139
await clock.tickAsync(6000)
137-
sinon.assert.calledOn(refreshStub, testNode)
140+
sinon.assert.notCalled(refreshStub)
141+
})
142+
143+
it('does refresh explorer when timer goes and status changed', async function () {
144+
sinon.assert.notCalled(refreshStub)
145+
statusUpdateFromClient = 'running'
146+
testNode.pollingNodes.add('0')
147+
await clock.tickAsync(6000)
148+
sinon.assert.called(refreshStub)
138149
})
139150

140151
it('stops timer once polling nodes are empty', async function () {

0 commit comments

Comments
 (0)