@@ -19,6 +19,8 @@ import { CloudSettingsService } from "./CloudSettingsService"
1919import { StaticSettingsService } from "./StaticSettingsService"
2020import { TelemetryClient } from "./TelemetryClient"
2121import { ShareService , TaskNotFoundError } from "./ShareService"
22+ import { ConnectionMonitor } from "./ConnectionMonitor"
23+ import { TelemetryQueueManager } from "./TelemetryQueueManager"
2224
2325type AuthStateChangedPayload = CloudServiceEvents [ "auth-state-changed" ] [ 0 ]
2426type AuthUserInfoPayload = CloudServiceEvents [ "user-info" ] [ 0 ]
@@ -35,6 +37,8 @@ export class CloudService extends EventEmitter<CloudServiceEvents> implements vs
3537 private settingsService : SettingsService | null = null
3638 private telemetryClient : TelemetryClient | null = null
3739 private shareService : ShareService | null = null
40+ private connectionMonitor : ConnectionMonitor | null = null
41+ private queueManager : TelemetryQueueManager | null = null
3842 private isInitialized = false
3943 private log : ( ...args : unknown [ ] ) => void
4044
@@ -87,9 +91,60 @@ export class CloudService extends EventEmitter<CloudServiceEvents> implements vs
8791 this . settingsService = cloudSettingsService
8892 }
8993
90- this . telemetryClient = new TelemetryClient ( this . authService , this . settingsService )
94+ this . telemetryClient = new TelemetryClient ( this . authService , this . settingsService , false , this . log )
9195 this . shareService = new ShareService ( this . authService , this . settingsService , this . log )
9296
97+ // Initialize connection monitor and queue manager
98+ this . connectionMonitor = new ConnectionMonitor ( )
99+ this . queueManager = TelemetryQueueManager . getInstance ( )
100+
101+ // Check if telemetry queue is enabled
102+ let isQueueEnabled = true
103+ try {
104+ const { ContextProxy } = await import ( "../../../src/core/config/ContextProxy" )
105+ isQueueEnabled = ContextProxy . instance . getValue ( "telemetryQueueEnabled" ) ?? true
106+ } catch ( _error ) {
107+ // Default to enabled if we can't access settings
108+ this . log ( "[CloudService] Could not access telemetryQueueEnabled setting, defaulting to enabled" )
109+ }
110+
111+ if ( isQueueEnabled ) {
112+ // Set up connection monitoring with debouncing
113+ let connectionRestoredDebounceTimer : NodeJS . Timeout | null = null
114+ const connectionRestoredDebounceDelay = 3000 // 3 seconds
115+
116+ this . connectionMonitor . onConnectionRestored ( ( ) => {
117+ this . log ( "[CloudService] Connection restored, scheduling queue processing" )
118+
119+ // Clear any existing timer
120+ if ( connectionRestoredDebounceTimer ) {
121+ clearTimeout ( connectionRestoredDebounceTimer )
122+ }
123+
124+ // Schedule queue processing with debounce
125+ connectionRestoredDebounceTimer = setTimeout ( ( ) => {
126+ this . queueManager
127+ ?. processQueue ( )
128+ . then ( ( ) => {
129+ this . log (
130+ "[CloudService] Successfully processed queued events after connection restored" ,
131+ )
132+ } )
133+ . catch ( ( error ) => {
134+ this . log ( "[CloudService] Error processing queue after connection restored:" , error )
135+ // Could implement retry logic here if needed in the future
136+ } )
137+ } , connectionRestoredDebounceDelay )
138+ } )
139+
140+ // Start monitoring if authenticated
141+ if ( this . authService . isAuthenticated ( ) ) {
142+ this . connectionMonitor . startMonitoring ( )
143+ }
144+ } else {
145+ this . log ( "[CloudService] Telemetry queue is disabled" )
146+ }
147+
93148 try {
94149 TelemetryService . instance . register ( this . telemetryClient )
95150 } catch ( error ) {
@@ -222,6 +277,34 @@ export class CloudService extends EventEmitter<CloudServiceEvents> implements vs
222277 return this . shareService ! . canShareTask ( )
223278 }
224279
280+ // Connection Status
281+
282+ public isOnline ( ) : boolean {
283+ this . ensureInitialized ( )
284+ return this . connectionMonitor ?. getConnectionStatus ( ) ?? true
285+ }
286+
287+ public onConnectionRestored ( callback : ( ) => void ) : void {
288+ this . ensureInitialized ( )
289+ if ( this . connectionMonitor ) {
290+ this . connectionMonitor . onConnectionRestored ( callback )
291+ }
292+ }
293+
294+ public onConnectionLost ( callback : ( ) => void ) : void {
295+ this . ensureInitialized ( )
296+ if ( this . connectionMonitor ) {
297+ this . connectionMonitor . onConnectionLost ( callback )
298+ }
299+ }
300+
301+ public removeConnectionListener ( event : "connection-restored" | "connection-lost" , callback : ( ) => void ) : void {
302+ this . ensureInitialized ( )
303+ if ( this . connectionMonitor ) {
304+ this . connectionMonitor . removeListener ( event , callback )
305+ }
306+ }
307+
225308 // Lifecycle
226309
227310 public dispose ( ) : void {
@@ -235,6 +318,9 @@ export class CloudService extends EventEmitter<CloudServiceEvents> implements vs
235318 }
236319 this . settingsService . dispose ( )
237320 }
321+ if ( this . connectionMonitor ) {
322+ this . connectionMonitor . dispose ( )
323+ }
238324
239325 this . isInitialized = false
240326 }
0 commit comments