Skip to content

Commit 3e50145

Browse files
committed
merge 2 global states variable but might need to revert because it will require users to reselect profile
1 parent ed782e2 commit 3e50145

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed

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

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { isAwsError, ToolkitError } from '../../shared/errors'
2929
import { telemetry } from '../../shared/telemetry/telemetry'
3030
import { localize } from '../../shared/utilities/vsCodeUtils'
3131
import { Commands } from '../../shared/vscode/commands2'
32-
import { CachedResource } from '../../shared/utilities/resourceCache'
32+
import { CachedResource, GlobalStateSchema } from '../../shared/utilities/resourceCache'
3333

3434
// TODO: is there a better way to manage all endpoint strings in one place?
3535
export const defaultServiceConfig: CodeWhispererConfig = {
@@ -51,6 +51,10 @@ const endpoints = createConstantMap({
5151
*/
5252
export type ProfileSwitchIntent = 'user' | 'auth' | 'update' | 'reload'
5353

54+
interface RegionProfileGlobalState extends GlobalStateSchema<RegionProfile> {
55+
previousSelection: { [label: string]: RegionProfile }
56+
}
57+
5458
export class RegionProfileManager {
5559
private static logger = getLogger()
5660
private _activeRegionProfile: RegionProfile | undefined
@@ -62,7 +66,7 @@ export class RegionProfileManager {
6266

6367
private readonly cache = new (class extends CachedResource<RegionProfile[]> {
6468
constructor(private readonly profileProvider: () => Promise<RegionProfile[]>) {
65-
super('aws.amazonq.regionProfiles.cache', 60000, {
69+
super('aws.amazonq.regionProfiles', 60000, {
6670
resource: {
6771
locked: false,
6872
timestamp: 0,
@@ -293,13 +297,20 @@ export class RegionProfileManager {
293297
}
294298

295299
private loadPersistedRegionProfle(): { [label: string]: RegionProfile } {
296-
const previousPersistedState = globals.globalState.tryGet<{ [label: string]: RegionProfile }>(
300+
const previousPersistedState = globals.globalState.tryGet<RegionProfileGlobalState>(
297301
'aws.amazonq.regionProfiles',
298302
Object,
299-
{}
303+
{
304+
previousSelection: {},
305+
resource: {
306+
locked: false,
307+
result: undefined,
308+
timestamp: 0,
309+
},
310+
}
300311
)
301312

302-
return previousPersistedState
313+
return previousPersistedState.previousSelection
303314
}
304315

305316
async persistSelectRegionProfile() {
@@ -311,13 +322,20 @@ export class RegionProfileManager {
311322
}
312323

313324
// persist connectionId to profileArn
314-
const previousPersistedState = globals.globalState.tryGet<{ [label: string]: RegionProfile }>(
325+
const previousPersistedState = globals.globalState.tryGet<RegionProfileGlobalState>(
315326
'aws.amazonq.regionProfiles',
316327
Object,
317-
{}
328+
{
329+
previousSelection: {},
330+
resource: {
331+
locked: false,
332+
result: undefined,
333+
timestamp: 0,
334+
},
335+
}
318336
)
319337

320-
previousPersistedState[conn.id] = this.activeRegionProfile
338+
previousPersistedState.previousSelection[conn.id] = this.activeRegionProfile
321339
await globals.globalState.update('aws.amazonq.regionProfiles', previousPersistedState)
322340
}
323341

@@ -368,7 +386,23 @@ export class RegionProfileManager {
368386
const updatedProfiles = Object.fromEntries(
369387
Object.entries(profiles).filter(([connId, profile]) => profile.arn !== arn)
370388
)
371-
await globals.globalState.update('aws.amazonq.regionProfiles', updatedProfiles)
389+
const previousPersistedState = globals.globalState.tryGet<RegionProfileGlobalState>(
390+
'aws.amazonq.regionProfiles',
391+
Object,
392+
{
393+
previousSelection: {},
394+
resource: {
395+
locked: false,
396+
result: undefined,
397+
timestamp: 0,
398+
},
399+
}
400+
)
401+
const toUpdate: RegionProfileGlobalState = {
402+
previousSelection: updatedProfiles,
403+
resource: previousPersistedState.resource,
404+
}
405+
await globals.globalState.update('aws.amazonq.regionProfiles', toUpdate)
372406
}
373407
}
374408

packages/core/src/shared/utilities/resourceCache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ interface Resource<V> {
1616

1717
// GlobalStates schema, which is used for vscode global states deserialization
1818
// [globals.globalState.tryGet<T>]
19-
interface GlobalStateSchema<V> {
19+
export interface GlobalStateSchema<V> {
2020
resource: Resource<V>
2121
}
2222

0 commit comments

Comments
 (0)