Skip to content

Commit 30500cb

Browse files
committed
PR feedback
1 parent cbce5aa commit 30500cb

File tree

1 file changed

+58
-28
lines changed

1 file changed

+58
-28
lines changed

src/extension.ts

Lines changed: 58 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ try {
1212
console.warn("Failed to load environment variables:", e)
1313
}
1414

15-
import { CloudService, ExtensionBridgeService } from "@roo-code/cloud"
15+
import { CloudService, ExtensionBridgeService, type CloudUserInfo } from "@roo-code/cloud"
1616
import { TelemetryService, PostHogTelemetryClient } from "@roo-code/telemetry"
1717

1818
import "./utils/path" // Necessary to have access to String.prototype.toPosix.
@@ -51,6 +51,11 @@ import { initializeI18n } from "./i18n"
5151

5252
let outputChannel: vscode.OutputChannel
5353
let extensionContext: vscode.ExtensionContext
54+
let cloudService: CloudService | undefined
55+
56+
let authStateChangedHandler: (() => void) | undefined
57+
let settingsUpdatedHandler: (() => void) | undefined
58+
let userInfoHandler: ((data: { userInfo: CloudUserInfo }) => Promise<void>) | undefined
5459

5560
// This method is called when your extension is activated.
5661
// Your extension is activated the very first time the command is executed.
@@ -120,36 +125,39 @@ export async function activate(context: vscode.ExtensionContext) {
120125
// Initialize the provider *before* the Roo Code Cloud service.
121126
const provider = new ClineProvider(context, outputChannel, "sidebar", contextProxy, mdmService)
122127

128+
// Initialize Roo Code Cloud service.
123129
const postStateListener = () => ClineProvider.getVisibleInstance()?.postStateToWebview()
130+
authStateChangedHandler = postStateListener
131+
settingsUpdatedHandler = postStateListener
124132

125-
// Initialize Roo Code Cloud service.
126-
const cloudService = await CloudService.createInstance(context, cloudLogger, {
127-
"auth-state-changed": postStateListener,
128-
"settings-updated": postStateListener,
129-
"user-info": async ({ userInfo }) => {
130-
postStateListener()
131-
132-
if (!CloudService.instance.cloudAPI) {
133-
cloudLogger("[CloudService] CloudAPI is not initialized")
134-
return
135-
}
133+
userInfoHandler = async ({ userInfo }: { userInfo: any }) => {
134+
postStateListener()
136135

137-
try {
138-
const config = await CloudService.instance.cloudAPI.bridgeConfig()
139-
cloudLogger(`[CloudService] bridgeConfig -> ${JSON.stringify(config)}`)
140-
141-
ExtensionBridgeService.handleRemoteControlState(
142-
userInfo,
143-
contextProxy.getValue("remoteControlEnabled"),
144-
{ ...config, provider, sessionId: vscode.env.sessionId },
145-
(message: string) => outputChannel.appendLine(message),
146-
)
147-
} catch (error) {
148-
cloudLogger(
149-
`[CloudService] Failed to fetch bridgeConfig: ${error instanceof Error ? error.message : String(error)}`,
150-
)
151-
}
152-
},
136+
if (!CloudService.instance.cloudAPI) {
137+
cloudLogger("[CloudService] CloudAPI is not initialized")
138+
return
139+
}
140+
141+
try {
142+
const config = await CloudService.instance.cloudAPI.bridgeConfig()
143+
144+
ExtensionBridgeService.handleRemoteControlState(
145+
userInfo,
146+
contextProxy.getValue("remoteControlEnabled"),
147+
{ ...config, provider, sessionId: vscode.env.sessionId },
148+
(message: string) => outputChannel.appendLine(message),
149+
)
150+
} catch (error) {
151+
cloudLogger(
152+
`[CloudService] Failed to fetch bridgeConfig: ${error instanceof Error ? error.message : String(error)}`,
153+
)
154+
}
155+
}
156+
157+
cloudService = await CloudService.createInstance(context, cloudLogger, {
158+
"auth-state-changed": authStateChangedHandler,
159+
"settings-updated": settingsUpdatedHandler,
160+
"user-info": userInfoHandler,
153161
})
154162

155163
try {
@@ -293,6 +301,28 @@ export async function activate(context: vscode.ExtensionContext) {
293301
export async function deactivate() {
294302
outputChannel.appendLine(`${Package.name} extension deactivated`)
295303

304+
if (cloudService && CloudService.hasInstance()) {
305+
try {
306+
if (authStateChangedHandler) {
307+
CloudService.instance.off("auth-state-changed", authStateChangedHandler)
308+
}
309+
310+
if (settingsUpdatedHandler) {
311+
CloudService.instance.off("settings-updated", settingsUpdatedHandler)
312+
}
313+
314+
if (userInfoHandler) {
315+
CloudService.instance.off("user-info", userInfoHandler as any)
316+
}
317+
318+
outputChannel.appendLine("CloudService event handlers cleaned up")
319+
} catch (error) {
320+
outputChannel.appendLine(
321+
`Failed to clean up CloudService event handlers: ${error instanceof Error ? error.message : String(error)}`,
322+
)
323+
}
324+
}
325+
296326
const bridgeService = ExtensionBridgeService.getInstance()
297327

298328
if (bridgeService) {

0 commit comments

Comments
 (0)