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,29 +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 > {
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
29- private settingsListener : ( ) => void
34+ private settingsListener : ( data : SettingsPayload ) => void
3035 private settingsService : SettingsService | null = null
3136 private telemetryClient : TelemetryClient | null = null
3237 private shareService : ShareService | null = null
3338 private isInitialized = false
3439 private log : ( ...args : unknown [ ] ) => void
3540
36- private constructor ( context : vscode . ExtensionContext , callbacks : CloudServiceCallbacks ) {
41+ private constructor ( context : vscode . ExtensionContext , log ?: ( ...args : unknown [ ] ) => void ) {
42+ super ( )
43+
3744 this . context = context
38- this . callbacks = callbacks
39- this . log = callbacks . log || console . log
40- this . authListener = ( ) => {
41- 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 )
4251 }
43- this . settingsListener = ( ) => {
44- this . callbacks . stateChanged ?. ( )
52+ this . settingsListener = ( data : SettingsPayload ) => {
53+ this . emit ( "settings-updated" , data )
4554 }
4655 }
4756
@@ -61,8 +70,8 @@ export class CloudService {
6170
6271 await this . authService . initialize ( )
6372
64- this . authService . on ( "auth-state-changed" , this . authListener )
65- this . authService . on ( "user-info" , this . authListener )
73+ this . authService . on ( "auth-state-changed" , this . authStateListener )
74+ this . authService . on ( "user-info" , this . authUserInfoListener )
6675
6776 // Check for static settings environment variable.
6877 const staticOrgSettings = process . env . ROO_CODE_CLOUD_ORG_SETTINGS
@@ -217,8 +226,8 @@ export class CloudService {
217226
218227 public dispose ( ) : void {
219228 if ( this . authService ) {
220- this . authService . off ( "auth-state-changed" , this . authListener )
221- this . authService . off ( "user-info" , this . authListener )
229+ this . authService . off ( "auth-state-changed" , this . authStateListener )
230+ this . authService . off ( "user-info" , this . authUserInfoListener )
222231 }
223232 if ( this . settingsService ) {
224233 if ( this . settingsService instanceof CloudSettingsService ) {
@@ -246,13 +255,13 @@ export class CloudService {
246255
247256 static async createInstance (
248257 context : vscode . ExtensionContext ,
249- callbacks : CloudServiceCallbacks = { } ,
258+ log ?: ( ... args : unknown [ ] ) => void ,
250259 ) : Promise < CloudService > {
251260 if ( this . _instance ) {
252261 throw new Error ( "CloudService instance already created" )
253262 }
254263
255- this . _instance = new CloudService ( context , callbacks )
264+ this . _instance = new CloudService ( context , log )
256265 await this . _instance . initialize ( )
257266 return this . _instance
258267 }
0 commit comments