@@ -62,7 +62,10 @@ export async function activate(context: vscode.ExtensionContext) {
6262 // Function to manage WebSocket connection
6363 async function connectToTraceWebSocket ( vaultSecretKey : string ) {
6464 // Close existing connection if any
65- closeWebSocketConnection ( ) ;
65+ if ( wsConnection ) {
66+ wsConnection . close ( ) ;
67+ wsConnection = null ;
68+ }
6669
6770 const wsUrl = apiUrl . replace ( / ^ h t t p / , 'ws' ) ;
6871 const fullWsUrl = `${ wsUrl } /vaults/traces/${ vaultSecretKey } /stream` ;
@@ -74,19 +77,26 @@ export async function activate(context: vscode.ExtensionContext) {
7477 console . log ( 'WebSocket connection established' ) ;
7578 } ) ;
7679
80+ let isFirst = true ;
81+
7782 wsConnection . on ( 'message' , ( data : Buffer ) => {
7883 try {
7984 const parsedData = JSON . parse ( data . toString ( ) ) ;
8085 if ( Array . isArray ( parsedData ) ) {
8186 // Initial batch of traces
82- console . log ( `Received ${ parsedData . length } traces from WebSocket` ) ;
83- tracesData = parsedData ;
87+ if ( isFirst ) {
88+ console . log ( `Received ${ parsedData . length } initial traces from WebSocket` ) ;
89+ tracesData = parsedData ;
90+ } else {
91+ console . log ( `Received ${ parsedData . length } new traces from WebSocket` ) ;
92+ parsedData . forEach ( pd => tracesData . push ( pd ) )
93+ }
8494 if ( showTraces && vscode . window . activeTextEditor ) {
8595 declareTracesUpdate ( vscode . window . activeTextEditor ) ;
8696 }
8797 } else {
8898 // Single new trace
89- console . log ( 'Received new trace from WebSocket' ) ;
99+ console . log ( 'Received exactly one new trace from WebSocket' ) ;
90100 tracesData . push ( parsedData ) ;
91101
92102 // If the file this trace belongs to is currently focused, update highlights
@@ -97,6 +107,7 @@ export async function activate(context: vscode.ExtensionContext) {
97107 }
98108 }
99109 }
110+ isFirst = false ;
100111 } catch ( error ) {
101112 console . error ( 'Error processing WebSocket message:' , error ) ;
102113 }
@@ -121,13 +132,6 @@ export async function activate(context: vscode.ExtensionContext) {
121132 } ) ;
122133 }
123134
124- function closeWebSocketConnection ( ) {
125- if ( wsConnection ) {
126- wsConnection . close ( ) ;
127- wsConnection = null ;
128- }
129- }
130-
131135 // Function to start monitoring vault key changes
132136 function startVaultKeyMonitoring ( ) {
133137 // Stop existing monitoring if any
@@ -158,8 +162,6 @@ export async function activate(context: vscode.ExtensionContext) {
158162 const vaultSecretKey = await vaultManager . getVaultKey ( vscode . window . activeTextEditor . document . uri . fsPath ) ;
159163
160164 if ( ! vaultSecretKey ) {
161- closeWebSocketConnection ( ) ;
162- currentVaultSecretKey = null ;
163165 return ;
164166 }
165167
@@ -225,10 +227,14 @@ export async function activate(context: vscode.ExtensionContext) {
225227 showTraces = ! showTraces ;
226228
227229 if ( showTraces ) {
230+ console . log ( "showing traces now" ) ;
228231 startVaultKeyMonitoring ( ) ;
229232 } else {
233+ console . log ( "hiding traces now" ) ;
230234 stopVaultKeyMonitoring ( ) ;
231- closeWebSocketConnection ( ) ;
235+ wsConnection ?. close ( ) ;
236+ wsConnection = null ;
237+ tracesData = [ ]
232238 unhighlightTraces ( ) ;
233239 clearHoverTraces ( ) ;
234240 }
@@ -268,6 +274,7 @@ export async function activate(context: vscode.ExtensionContext) {
268274 if ( ! editor ) {
269275 return ;
270276 }
277+ console . log ( "highlight requested of " + tracesData . length + " traces" ) ;
271278 const regions = tracesToRegions ( tracesData . filter ( trace =>
272279 formatUriForDB ( editor . document . uri ) === trace . start_pos . filepath
273280 ) ) ;
@@ -282,7 +289,11 @@ export async function activate(context: vscode.ExtensionContext) {
282289 tracesUpdates . pop ( ) ;
283290 }
284291 if ( tracesUpdates . length > 0 && tracesUpdates [ tracesUpdates . length - 1 ] === currentEditor ) {
285- highlightTraces ( currentEditor ) ;
292+ console . log ( tracesUpdates . length + " relevant trace update received now" ) ;
293+ if ( showTraces ) {
294+ console . log ( "update triggers highlight because we show traces" ) ;
295+ highlightTraces ( currentEditor ) ;
296+ }
286297 tracesUpdates . pop ( ) ;
287298 }
288299 }
@@ -295,6 +306,7 @@ export async function activate(context: vscode.ExtensionContext) {
295306 if ( ! editor ) {
296307 return ;
297308 }
309+ console . log ( "unhighlighting traces now" ) ;
298310 clearDecorations ( editor , decoratedRanges ) ;
299311 }
300312
@@ -304,6 +316,7 @@ export async function activate(context: vscode.ExtensionContext) {
304316 return ;
305317 }
306318 if ( tracesHoverDisposable ) {
319+ console . log ( "clearing hovers" )
307320 tracesHoverDisposable . dispose ( ) ;
308321 tracesHoverDisposable = undefined ;
309322 }
@@ -335,11 +348,10 @@ export async function activate(context: vscode.ExtensionContext) {
335348
336349 context . subscriptions . push ( disposable ) ;
337350
338- // Make sure to clean up resources when deactivated
339351 context . subscriptions . push ( {
340352 dispose : ( ) => {
341353 stopVaultKeyMonitoring ( ) ;
342- closeWebSocketConnection ( ) ;
354+ wsConnection ?. close ( )
343355 }
344356 } ) ;
345357}
0 commit comments