Skip to content

Commit 96d7d42

Browse files
committed
Add kill method to poller
1 parent bc7df5c commit 96d7d42

File tree

4 files changed

+48
-37
lines changed

4 files changed

+48
-37
lines changed

packages/core/src/codewhisperer/activation.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,7 @@ export async function activate(context: ExtContext): Promise<void> {
498498
export async function shutdown() {
499499
RecommendationHandler.instance.reportUserDecisions(-1)
500500
await CodeWhispererTracker.getTracker().shutdown()
501+
AuthUtil.instance.regionProfileManager.globalStatePoller.kill()
501502
}
502503

503504
function toggleIssuesVisibility(visibleCondition: (issue: CodeScanIssue, filePath: string) => boolean) {

packages/core/src/codewhisperer/region/regionProfileManager.ts

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ const endpoints = createConstantMap({
3838
'eu-central-1': 'https://q.eu-central-1.amazonaws.com/',
3939
})
4040

41+
const getRegionProfile = () =>
42+
globals.globalState.tryGet<{ [label: string]: RegionProfile }>('aws.amazonq.regionProfiles', Object, {})
43+
4144
/**
4245
* 'user' -> users change the profile through Q menu
4346
* 'auth' -> users change the profile through webview profile selector page
@@ -80,6 +83,17 @@ export class RegionProfileManager {
8083
}
8184
})(this.listRegionProfile.bind(this))
8285

86+
// This is a poller that handles synchornization of selected region profiles between different IDE windows.
87+
// It checks for changes in global state of region profile, invoking the change handler to switch profiles
88+
public globalStatePoller = GlobalStatePoller.create({
89+
getState: getRegionProfile,
90+
changeHandler: async () => {
91+
const profile = this.loadPersistedRegionProfle()
92+
void this._switchRegionProfile(profile[this.authProvider.profileName], 'reload')
93+
},
94+
pollIntervalInMs: 2000,
95+
})
96+
8397
get activeRegionProfile() {
8498
if (this.authProvider.isBuilderIdConnection()) {
8599
return undefined
@@ -121,18 +135,7 @@ export class RegionProfileManager {
121135
return this._profiles
122136
}
123137

124-
constructor(private readonly authProvider: IAuthProvider) {
125-
const getProfileFunction = () =>
126-
globals.globalState.tryGet<{ [label: string]: RegionProfile }>('aws.amazonq.regionProfiles', Object, {})
127-
const profileChangedHandler = async () => {
128-
const profile = this.loadPersistedRegionProfle()
129-
void this._switchRegionProfile(profile[this.authProvider.profileName], 'reload')
130-
}
131-
132-
// This is a poller that handles synchornization of selected region profiles between different IDE windows.
133-
// It checks for changes in global state of region profile, invoking the change handler to switch profiles
134-
GlobalStatePoller.create(getProfileFunction, profileChangedHandler)
135-
}
138+
constructor(private readonly authProvider: IAuthProvider) {}
136139

137140
async getProfiles(): Promise<RegionProfile[]> {
138141
return this.cache.getResource()
@@ -309,13 +312,7 @@ export class RegionProfileManager {
309312
}
310313

311314
private loadPersistedRegionProfle(): { [label: string]: RegionProfile } {
312-
const previousPersistedState = globals.globalState.tryGet<{ [label: string]: RegionProfile }>(
313-
'aws.amazonq.regionProfiles',
314-
Object,
315-
{}
316-
)
317-
318-
return previousPersistedState
315+
return getRegionProfile()
319316
}
320317

321318
async persistSelectRegionProfile() {
@@ -325,11 +322,7 @@ export class RegionProfileManager {
325322
}
326323

327324
// persist connectionId to profileArn
328-
const previousPersistedState = globals.globalState.tryGet<{ [label: string]: RegionProfile }>(
329-
'aws.amazonq.regionProfiles',
330-
Object,
331-
{}
332-
)
325+
const previousPersistedState = getRegionProfile()
333326

334327
previousPersistedState[this.authProvider.profileName] = this.activeRegionProfile
335328
await globals.globalState.update('aws.amazonq.regionProfiles', previousPersistedState)

packages/core/src/codewhisperer/util/authUtil.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ export class AuthUtil implements IAuthProvider {
277277
})
278278
}
279279

280-
private async cacheChangedHandler(event: string) {
280+
private async cacheChangedHandler(event: 'create' | 'delete') {
281281
if (event === 'delete') {
282282
await this.logout()
283283
} else if (event === 'create') {

packages/core/src/shared/globalState.ts

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -319,20 +319,29 @@ export class GlobalState implements vscode.Memento {
319319
}
320320
}
321321

322+
export interface GlobalStatePollerProps {
323+
getState: () => any
324+
changeHandler: () => void
325+
pollIntervalInMs: number
326+
}
327+
322328
/**
323329
* Utility class that polls a state value at regular intervals and triggers a callback when the state changes.
324330
*
325331
* This class can be used to monitor changes in global state and react to those changes.
326332
*/
327333
export class GlobalStatePoller {
328334
protected oldValue: any
335+
protected pollIntervalInMs: number
329336
protected getState: () => any
330337
protected changeHandler: () => void
338+
protected intervalId?: NodeJS.Timeout
331339

332-
constructor(getState: () => any, changeHandler: () => void) {
333-
this.getState = getState
334-
this.changeHandler = changeHandler
335-
this.oldValue = getState()
340+
constructor(props: GlobalStatePollerProps) {
341+
this.getState = props.getState
342+
this.changeHandler = props.changeHandler
343+
this.pollIntervalInMs = props.pollIntervalInMs
344+
this.oldValue = this.getState()
336345
}
337346

338347
/**
@@ -342,24 +351,32 @@ export class GlobalStatePoller {
342351
* @param changeHandler - Callback function that is invoked when the state changes
343352
* @returns A new GlobalStatePoller instance that has already started polling
344353
*/
345-
static create(getState: () => any, changeHandler: () => void) {
346-
const instance = new GlobalStatePoller(getState, changeHandler)
354+
static create(props: GlobalStatePollerProps) {
355+
const instance = new GlobalStatePoller(props)
347356
instance.poll()
348357
return instance
349358
}
350359

351360
/**
352-
* Starts polling the state value at 1 second intervals.
353-
* When a change is detected, the changeHandler callback is invoked.
361+
* Starts polling the state value. When a change is detected, the changeHandler callback is invoked.
354362
*/
355-
poll() {
356-
const interval = 1000 // ms
357-
setInterval(() => {
363+
private poll() {
364+
this.intervalId = setInterval(() => {
358365
const newValue = this.getState()
359366
if (this.oldValue !== newValue) {
360367
this.oldValue = newValue
361368
this.changeHandler()
362369
}
363-
}, interval)
370+
}, this.pollIntervalInMs)
371+
}
372+
373+
/**
374+
* Stops the polling interval.
375+
*/
376+
kill() {
377+
if (this.intervalId) {
378+
clearInterval(this.intervalId)
379+
this.intervalId = undefined
380+
}
364381
}
365382
}

0 commit comments

Comments
 (0)