-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Cloud service cleanup callbacks / move to events #6519
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,4 +1,5 @@ | ||||||||||
| import * as vscode from "vscode" | ||||||||||
| import EventEmitter from "events" | ||||||||||
|
|
||||||||||
| import type { | ||||||||||
| CloudUserInfo, | ||||||||||
|
|
@@ -10,7 +11,7 @@ import type { | |||||||||
| } from "@roo-code/types" | ||||||||||
| import { TelemetryService } from "@roo-code/telemetry" | ||||||||||
|
|
||||||||||
| import { CloudServiceCallbacks } from "./types" | ||||||||||
| import { CloudServiceEvents } from "./types" | ||||||||||
| import type { AuthService } from "./auth" | ||||||||||
| import { WebAuthService, StaticTokenAuthService } from "./auth" | ||||||||||
| import type { SettingsService } from "./SettingsService" | ||||||||||
|
|
@@ -19,25 +20,37 @@ import { StaticSettingsService } from "./StaticSettingsService" | |||||||||
| import { TelemetryClient } from "./TelemetryClient" | ||||||||||
| import { ShareService, TaskNotFoundError } from "./ShareService" | ||||||||||
|
|
||||||||||
| export class CloudService { | ||||||||||
| type AuthStateChangedPayload = CloudServiceEvents["auth-state-changed"][0] | ||||||||||
| type AuthUserInfoPayload = CloudServiceEvents["user-info"][0] | ||||||||||
| type SettingsPayload = CloudServiceEvents["settings-updated"][0] | ||||||||||
|
|
||||||||||
| export class CloudService extends EventEmitter<CloudServiceEvents> implements vscode.Disposable { | ||||||||||
| private static _instance: CloudService | null = null | ||||||||||
|
|
||||||||||
| private context: vscode.ExtensionContext | ||||||||||
| private callbacks: CloudServiceCallbacks | ||||||||||
| private authListener: () => void | ||||||||||
| private authStateListener: (data: AuthStateChangedPayload) => void | ||||||||||
| private authUserInfoListener: (data: AuthUserInfoPayload) => void | ||||||||||
| private authService: AuthService | null = null | ||||||||||
| private settingsListener: (data: SettingsPayload) => void | ||||||||||
| private settingsService: SettingsService | null = null | ||||||||||
| private telemetryClient: TelemetryClient | null = null | ||||||||||
| private shareService: ShareService | null = null | ||||||||||
| private isInitialized = false | ||||||||||
| private log: (...args: unknown[]) => void | ||||||||||
|
|
||||||||||
| private constructor(context: vscode.ExtensionContext, callbacks: CloudServiceCallbacks) { | ||||||||||
| private constructor(context: vscode.ExtensionContext, log?: (...args: unknown[]) => void) { | ||||||||||
| super() | ||||||||||
|
|
||||||||||
| this.context = context | ||||||||||
| this.callbacks = callbacks | ||||||||||
| this.log = callbacks.log || console.log | ||||||||||
| this.authListener = () => { | ||||||||||
| this.callbacks.stateChanged?.() | ||||||||||
| this.log = log || console.log | ||||||||||
| this.authStateListener = (data: AuthStateChangedPayload) => { | ||||||||||
| this.emit("auth-state-changed", data) | ||||||||||
| } | ||||||||||
| this.authUserInfoListener = (data: AuthUserInfoPayload) => { | ||||||||||
| this.emit("user-info", data) | ||||||||||
| } | ||||||||||
| this.settingsListener = (data: SettingsPayload) => { | ||||||||||
| this.emit("settings-updated", data) | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
|
@@ -57,26 +70,20 @@ export class CloudService { | |||||||||
|
|
||||||||||
| await this.authService.initialize() | ||||||||||
|
|
||||||||||
| this.authService.on("attempting-session", this.authListener) | ||||||||||
| this.authService.on("inactive-session", this.authListener) | ||||||||||
| this.authService.on("active-session", this.authListener) | ||||||||||
| this.authService.on("logged-out", this.authListener) | ||||||||||
| this.authService.on("user-info", this.authListener) | ||||||||||
| this.authService.on("auth-state-changed", this.authStateListener) | ||||||||||
| this.authService.on("user-info", this.authUserInfoListener) | ||||||||||
|
|
||||||||||
| // Check for static settings environment variable. | ||||||||||
| const staticOrgSettings = process.env.ROO_CODE_CLOUD_ORG_SETTINGS | ||||||||||
|
|
||||||||||
| if (staticOrgSettings && staticOrgSettings.length > 0) { | ||||||||||
| this.settingsService = new StaticSettingsService(staticOrgSettings, this.log) | ||||||||||
| } else { | ||||||||||
| const cloudSettingsService = new CloudSettingsService( | ||||||||||
| this.context, | ||||||||||
| this.authService, | ||||||||||
| () => this.callbacks.stateChanged?.(), | ||||||||||
| this.log, | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| const cloudSettingsService = new CloudSettingsService(this.context, this.authService, this.log) | ||||||||||
| cloudSettingsService.initialize() | ||||||||||
|
|
||||||||||
| cloudSettingsService.on("settings-updated", this.settingsListener) | ||||||||||
|
|
||||||||||
| this.settingsService = cloudSettingsService | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
|
@@ -219,13 +226,13 @@ export class CloudService { | |||||||||
|
|
||||||||||
| public dispose(): void { | ||||||||||
| if (this.authService) { | ||||||||||
| this.authService.off("attempting-session", this.authListener) | ||||||||||
| this.authService.off("inactive-session", this.authListener) | ||||||||||
| this.authService.off("active-session", this.authListener) | ||||||||||
| this.authService.off("logged-out", this.authListener) | ||||||||||
| this.authService.off("user-info", this.authListener) | ||||||||||
| this.authService.off("auth-state-changed", this.authStateListener) | ||||||||||
| this.authService.off("user-info", this.authUserInfoListener) | ||||||||||
| } | ||||||||||
| if (this.settingsService) { | ||||||||||
| if (this.settingsService instanceof CloudSettingsService) { | ||||||||||
| this.settingsService.off("settings-updated", this.settingsListener) | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The instanceof check here could fail if the settings service is replaced with a different implementation during runtime. Consider always storing a reference to the actual service instance used, or checking if the service has an 'off' method:
Suggested change
|
||||||||||
| } | ||||||||||
| this.settingsService.dispose() | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
|
@@ -248,13 +255,13 @@ export class CloudService { | |||||||||
|
|
||||||||||
| static async createInstance( | ||||||||||
| context: vscode.ExtensionContext, | ||||||||||
| callbacks: CloudServiceCallbacks = {}, | ||||||||||
| log?: (...args: unknown[]) => void, | ||||||||||
| ): Promise<CloudService> { | ||||||||||
| if (this._instance) { | ||||||||||
| throw new Error("CloudService instance already created") | ||||||||||
| } | ||||||||||
|
|
||||||||||
| this._instance = new CloudService(context, callbacks) | ||||||||||
| this._instance = new CloudService(context, log) | ||||||||||
| await this._instance.initialize() | ||||||||||
| return this._instance | ||||||||||
| } | ||||||||||
|
|
||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These type aliases are helpful! Consider exporting them from the CloudService module so consumers can use them for type-safe event handling: