@@ -13,26 +13,63 @@ import {
1313 isPassThroughStateKey ,
1414} from "../shared/globalState"
1515import { API_CONFIG_KEYS , ApiConfiguration } from "../shared/api"
16+ import { ContextHolder } from "./contextHolder"
17+
18+ /*
19+ * This class provides a proxy for the vscode.ExtensionContext.
20+ * It caches global state and secrets in memory for faster access.
21+ * It also provides a method to reset all state and secrets.
22+ */
23+
24+ export interface ContextProxyInstance {
25+ get isInitialized ( ) : boolean
26+ get extensionUri ( ) : vscode . Uri
27+ get extensionPath ( ) : string
28+ get globalStorageUri ( ) : vscode . Uri
29+ get logUri ( ) : vscode . Uri
30+ get extension ( ) : vscode . Extension < any >
31+ get extensionMode ( ) : vscode . ExtensionMode
32+ getGlobalState < T > ( key : GlobalStateKey ) : T | undefined
33+ getGlobalState < T > ( key : GlobalStateKey , defaultValue : T ) : T
34+ getGlobalState < T > ( key : GlobalStateKey , defaultValue ?: T ) : T | undefined
35+ updateGlobalState < T > ( key : GlobalStateKey , value : T ) : any
36+ getSecret ( key : SecretKey ) : string | undefined
37+ storeSecret ( key : SecretKey , value ?: string ) : Thenable < void >
38+ setValue ( key : ConfigurationKey , value : any ) : Thenable < void >
39+ setValues ( values : Partial < ConfigurationValues > ) : Thenable < void >
40+ setApiConfiguration ( apiConfiguration : ApiConfiguration ) : Thenable < void >
41+ resetAllState ( ) : Thenable < void >
42+ }
43+
44+ export class ContextProxy implements ContextProxyInstance {
45+ private static instance : ContextProxy
1646
17- export class ContextProxy {
1847 private readonly originalContext : vscode . ExtensionContext
1948
2049 private stateCache : Map < GlobalStateKey , any >
2150 private secretCache : Map < SecretKey , string | undefined >
2251 private _isInitialized = false
2352
24- constructor ( context : vscode . ExtensionContext ) {
25- this . originalContext = context
53+ private constructor ( ) {
54+ this . originalContext = ContextHolder . getInstanceWithoutArgs ( ) . getContext ( )
2655 this . stateCache = new Map ( )
2756 this . secretCache = new Map ( )
28- this . _isInitialized = false
57+ this . initialize ( ) // Initialize immediately
2958 }
3059
31- public get isInitialized ( ) {
32- return this . _isInitialized
60+ public static getInstance ( ) : ContextProxy {
61+ if ( ! ContextProxy . instance ) {
62+ ContextProxy . instance = new ContextProxy ( )
63+ }
64+ return ContextProxy . instance
3365 }
3466
35- public async initialize ( ) {
67+ /**
68+ * Initialize the context proxy by loading global state and secrets.
69+ * This method is called automatically when the instance is created.
70+ */
71+ private async initialize ( ) {
72+ if ( this . _isInitialized ) return
3673 for ( const key of GLOBAL_STATE_KEYS ) {
3774 try {
3875 this . stateCache . set ( key , this . originalContext . globalState . get ( key ) )
@@ -54,6 +91,10 @@ export class ContextProxy {
5491 this . _isInitialized = true
5592 }
5693
94+ public get isInitialized ( ) {
95+ return this . _isInitialized
96+ }
97+
5798 get extensionUri ( ) {
5899 return this . originalContext . extensionUri
59100 }
@@ -97,11 +138,23 @@ export class ContextProxy {
97138 return this . originalContext . globalState . update ( key , value )
98139 }
99140
141+ /**
142+ * Retrieve a secret from the cache.
143+ * @param key The secret key to retrieve
144+ * @returns The cached secret value or undefined if not found
145+ */
100146 getSecret ( key : SecretKey ) {
101147 return this . secretCache . get ( key )
102148 }
103149
104- storeSecret ( key : SecretKey , value ?: string ) {
150+ /**
151+ * Store a secret in the cache and write it to the context.
152+ * If the value is undefined, the secret will be deleted from the context.
153+ * @param key The secret key to store
154+ * @param value The secret value to store
155+ * @returns A promise that resolves when the operation completes
156+ */
157+ storeSecret ( key : SecretKey , value ?: string ) : Thenable < void > {
105158 // Update cache.
106159 this . secretCache . set ( key , value )
107160
@@ -119,7 +172,7 @@ export class ContextProxy {
119172 * @param value The value to set
120173 * @returns A promise that resolves when the operation completes
121174 */
122- setValue ( key : ConfigurationKey , value : any ) {
175+ setValue ( key : ConfigurationKey , value : any ) : Thenable < void > {
123176 if ( isSecretKey ( key ) ) {
124177 return this . storeSecret ( key , value )
125178 }
0 commit comments