11import * as vscode from "vscode"
2+ import EventEmitter from "events"
23
34import type {
45 CloudUserInfo ,
@@ -10,7 +11,7 @@ import type {
1011} from "@roo-code/types"
1112import { TelemetryService } from "@roo-code/telemetry"
1213
13- import { CloudServiceCallbacks } from "./types"
14+ import { CloudServiceEvents } from "./types"
1415import type { AuthService } from "./auth"
1516import { WebAuthService , StaticTokenAuthService } from "./auth"
1617import type { SettingsService } from "./SettingsService"
@@ -19,25 +20,37 @@ import { StaticSettingsService } from "./StaticSettingsService"
1920import { TelemetryClient } from "./TelemetryClient"
2021import { ShareService , TaskNotFoundError } from "./ShareService"
2122
22- export class CloudService {
23+ type AuthStateChangedPayload = CloudServiceEvents [ "auth-state-changed" ] [ 0 ]
24+ type AuthUserInfoPayload = CloudServiceEvents [ "user-info" ] [ 0 ]
25+ type SettingsPayload = CloudServiceEvents [ "settings-updated" ] [ 0 ]
26+
27+ export class CloudService extends EventEmitter < CloudServiceEvents > implements vscode . Disposable {
2328 private static _instance : CloudService | null = null
2429
2530 private context : vscode . ExtensionContext
26- private callbacks : CloudServiceCallbacks
27- private authListener : ( ) => void
31+ private authStateListener : ( data : AuthStateChangedPayload ) => void
32+ private authUserInfoListener : ( data : AuthUserInfoPayload ) => void
2833 private authService : AuthService | null = null
34+ private settingsListener : ( data : SettingsPayload ) => void
2935 private settingsService : SettingsService | null = null
3036 private telemetryClient : TelemetryClient | null = null
3137 private shareService : ShareService | null = null
3238 private isInitialized = false
3339 private log : ( ...args : unknown [ ] ) => void
3440
35- private constructor ( context : vscode . ExtensionContext , callbacks : CloudServiceCallbacks ) {
41+ private constructor ( context : vscode . ExtensionContext , log ?: ( ...args : unknown [ ] ) => void ) {
42+ super ( )
43+
3644 this . context = context
37- this . callbacks = callbacks
38- this . log = callbacks . log || console . log
39- this . authListener = ( ) => {
40- this . callbacks . stateChanged ?.( )
45+ this . log = log || console . log
46+ this . authStateListener = ( data : AuthStateChangedPayload ) => {
47+ this . emit ( "auth-state-changed" , data )
48+ }
49+ this . authUserInfoListener = ( data : AuthUserInfoPayload ) => {
50+ this . emit ( "user-info" , data )
51+ }
52+ this . settingsListener = ( data : SettingsPayload ) => {
53+ this . emit ( "settings-updated" , data )
4154 }
4255 }
4356
@@ -57,26 +70,20 @@ export class CloudService {
5770
5871 await this . authService . initialize ( )
5972
60- this . authService . on ( "attempting-session" , this . authListener )
61- this . authService . on ( "inactive-session" , this . authListener )
62- this . authService . on ( "active-session" , this . authListener )
63- this . authService . on ( "logged-out" , this . authListener )
64- this . authService . on ( "user-info" , this . authListener )
73+ this . authService . on ( "auth-state-changed" , this . authStateListener )
74+ this . authService . on ( "user-info" , this . authUserInfoListener )
6575
6676 // Check for static settings environment variable.
6777 const staticOrgSettings = process . env . ROO_CODE_CLOUD_ORG_SETTINGS
6878
6979 if ( staticOrgSettings && staticOrgSettings . length > 0 ) {
7080 this . settingsService = new StaticSettingsService ( staticOrgSettings , this . log )
7181 } else {
72- const cloudSettingsService = new CloudSettingsService (
73- this . context ,
74- this . authService ,
75- ( ) => this . callbacks . stateChanged ?.( ) ,
76- this . log ,
77- )
78-
82+ const cloudSettingsService = new CloudSettingsService ( this . context , this . authService , this . log )
7983 cloudSettingsService . initialize ( )
84+
85+ cloudSettingsService . on ( "settings-updated" , this . settingsListener )
86+
8087 this . settingsService = cloudSettingsService
8188 }
8289
@@ -219,13 +226,13 @@ export class CloudService {
219226
220227 public dispose ( ) : void {
221228 if ( this . authService ) {
222- this . authService . off ( "attempting-session" , this . authListener )
223- this . authService . off ( "inactive-session" , this . authListener )
224- this . authService . off ( "active-session" , this . authListener )
225- this . authService . off ( "logged-out" , this . authListener )
226- this . authService . off ( "user-info" , this . authListener )
229+ this . authService . off ( "auth-state-changed" , this . authStateListener )
230+ this . authService . off ( "user-info" , this . authUserInfoListener )
227231 }
228232 if ( this . settingsService ) {
233+ if ( this . settingsService instanceof CloudSettingsService ) {
234+ this . settingsService . off ( "settings-updated" , this . settingsListener )
235+ }
229236 this . settingsService . dispose ( )
230237 }
231238
@@ -248,13 +255,13 @@ export class CloudService {
248255
249256 static async createInstance (
250257 context : vscode . ExtensionContext ,
251- callbacks : CloudServiceCallbacks = { } ,
258+ log ?: ( ... args : unknown [ ] ) => void ,
252259 ) : Promise < CloudService > {
253260 if ( this . _instance ) {
254261 throw new Error ( "CloudService instance already created" )
255262 }
256263
257- this . _instance = new CloudService ( context , callbacks )
264+ this . _instance = new CloudService ( context , log )
258265 await this . _instance . initialize ( )
259266 return this . _instance
260267 }
0 commit comments