contextProxy.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import * as vscode from "vscode"
  2. import { logger } from "../utils/logging"
  3. import { GLOBAL_STATE_KEYS, SECRET_KEYS } from "../shared/globalState"
  4. export class ContextProxy {
  5. private readonly originalContext: vscode.ExtensionContext
  6. private stateCache: Map<string, any>
  7. private secretCache: Map<string, string | undefined>
  8. constructor(context: vscode.ExtensionContext) {
  9. // Initialize properties first
  10. this.originalContext = context
  11. this.stateCache = new Map()
  12. this.secretCache = new Map()
  13. // Initialize state cache with all defined global state keys
  14. this.initializeStateCache()
  15. // Initialize secret cache with all defined secret keys
  16. this.initializeSecretCache()
  17. logger.debug("ContextProxy created")
  18. }
  19. // Helper method to initialize state cache
  20. private initializeStateCache(): void {
  21. for (const key of GLOBAL_STATE_KEYS) {
  22. try {
  23. const value = this.originalContext.globalState.get(key)
  24. this.stateCache.set(key, value)
  25. } catch (error) {
  26. logger.error(`Error loading global ${key}: ${error instanceof Error ? error.message : String(error)}`)
  27. }
  28. }
  29. }
  30. // Helper method to initialize secret cache
  31. private initializeSecretCache(): void {
  32. for (const key of SECRET_KEYS) {
  33. // Get actual value and update cache when promise resolves
  34. ;(this.originalContext.secrets.get(key) as Promise<string | undefined>)
  35. .then((value) => {
  36. this.secretCache.set(key, value)
  37. })
  38. .catch((error: Error) => {
  39. logger.error(`Error loading secret ${key}: ${error.message}`)
  40. })
  41. }
  42. }
  43. get extensionUri(): vscode.Uri {
  44. return this.originalContext.extensionUri
  45. }
  46. get extensionPath(): string {
  47. return this.originalContext.extensionPath
  48. }
  49. get globalStorageUri(): vscode.Uri {
  50. return this.originalContext.globalStorageUri
  51. }
  52. get logUri(): vscode.Uri {
  53. return this.originalContext.logUri
  54. }
  55. get extension(): vscode.Extension<any> | undefined {
  56. return this.originalContext.extension
  57. }
  58. get extensionMode(): vscode.ExtensionMode {
  59. return this.originalContext.extensionMode
  60. }
  61. getGlobalState<T>(key: string): T | undefined
  62. getGlobalState<T>(key: string, defaultValue: T): T
  63. getGlobalState<T>(key: string, defaultValue?: T): T | undefined {
  64. const value = this.stateCache.get(key) as T | undefined
  65. return value !== undefined ? value : (defaultValue as T | undefined)
  66. }
  67. updateGlobalState<T>(key: string, value: T): Thenable<void> {
  68. this.stateCache.set(key, value)
  69. return this.originalContext.globalState.update(key, value)
  70. }
  71. getSecret(key: string): string | undefined {
  72. return this.secretCache.get(key)
  73. }
  74. storeSecret(key: string, value?: string): Thenable<void> {
  75. // Update cache
  76. this.secretCache.set(key, value)
  77. // Write directly to context
  78. if (value === undefined) {
  79. return this.originalContext.secrets.delete(key)
  80. } else {
  81. return this.originalContext.secrets.store(key, value)
  82. }
  83. }
  84. }