@@ -255,7 +255,6 @@ internal static void SetToolBarIcon()
255255
256256 internal static void SetStatusBarDocType ( string str )
257257 {
258-
259258 //IntPtr pStr = Marshal.AllocHGlobal(Marshal.SizeOf(str));
260259 //Marshal.StructureToPtr(str, pStr, false);
261260 Win32 . SendMessage ( PluginBase . nppData . _nppHandle , ( uint ) NppMsg . NPPM_SETSTATUSBAR , ( IntPtr ) NppMsg . STATUSBAR_DOC_TYPE , str ) ;
@@ -308,7 +307,7 @@ public static void OnNotification(ScNotification notification)
308307
309308 if ( notification . Header . Code == ( uint ) SciMsg . SCN_CHARADDED ) // our best bet
310309 {
311- Logger . Debug ( "[notification] SCN_CHARADDED" ) ;
310+ if ( Debug ) Logger . Debug ( "[notification] SCN_CHARADDED" ) ;
312311 HandleActicity ( ) ;
313312 if ( Debug ) Logger . Debug ( "SCN_CHARADDED - File: " + GetCurrentFile ( ) + ", char: " + ( char ) notification . character + " (" + notification . character + "), lang: " + GetCurrentLanguage ( ) ) ;
314313 }
@@ -343,7 +342,9 @@ public static void UpdateStatusbar()
343342 string newStatusBarDocTypeText = @"✎C::S " + currentCount . ToString ( ) + append ;
344343 if ( _lastStatusBarDocTypeText != newStatusBarDocTypeText ) // Let's not call it when it's not needed (nothing changed), we will earn some performance hopefully
345344 {
345+ if ( Debug ) Logger . Debug ( "Setting new statusbar text to: " + newStatusBarDocTypeText ) ;
346346 SetStatusBarDocType ( newStatusBarDocTypeText ) ;
347+ if ( Debug ) Logger . Debug ( "New statusbar text was set." ) ;
347348 }
348349 _lastStatusBarDocTypeText = newStatusBarDocTypeText ;
349350 }
@@ -365,6 +366,16 @@ public static void HandleActicity()
365366 _lastActivity = DateTime . Now ;
366367 }
367368
369+ private static void FlushCurrentPulseIfNeeded ( )
370+ {
371+ if ( pulseQueue != null && currentPulse != null && ! currentPulse . isEmpty ( ) )
372+ {
373+ pulseQueue . Enqueue ( currentPulse ) ;
374+ currentPulse = new Pulse ( ) ;
375+ currentCount = 0 ;
376+ }
377+ }
378+
368379 private static void ProcessPulses ( object sender , ElapsedEventArgs e )
369380 {
370381 Task . Run ( ( ) =>
@@ -373,19 +384,15 @@ private static void ProcessPulses(object sender, ElapsedEventArgs e)
373384 {
374385 if ( pulseQueue != null && ( ( currentPulse != null && ! currentPulse . isEmpty ( ) ) || ! pulseQueue . IsEmpty ) && EnoughTimePassed ( DateTime . Now ) )
375386 {
376- if ( currentPulse != null && ! currentPulse . isEmpty ( ) )
377- {
378- pulseQueue . Enqueue ( currentPulse ) ;
379- currentPulse = new Pulse ( ) ;
380- currentCount = 0 ;
381- }
387+ // if current pulse is not empty, add it to the queue now
388+ FlushCurrentPulseIfNeeded ( ) ;
382389
383390 // run only if the task didn't run yet, or the previous one already finished
384391 if ( pulseProcessor == null || pulseProcessor . IsCompleted || pulseProcessor . IsFaulted ) // don't start if it's cancelled
385392 {
386393 try
387394 {
388- pulseProcessor = ProcessPulses ( pulseProcessor_tokensource ) ;
395+ pulseProcessor = ProcessPendingPulses ( pulseProcessor_tokensource ) ;
389396 }
390397 catch ( OperationCanceledException ) { }
391398 }
@@ -401,19 +408,12 @@ private static void ProcessPulses(object sender, ElapsedEventArgs e)
401408 } ) ;
402409 }
403410
404- private static Task ProcessPulses ( CancellationTokenSource tokenSource )
411+ private static Task ProcessPendingPulses ( CancellationTokenSource tokenSource )
405412 {
406413 return Task . Run ( async ( ) =>
407414 {
408415 if ( pulseQueue != null && ( ( currentPulse != null && ! currentPulse . isEmpty ( ) ) || ! pulseQueue . IsEmpty ) && EnoughTimePassed ( DateTime . Now ) )
409416 {
410- if ( currentPulse != null && ! currentPulse . isEmpty ( ) )
411- {
412- pulseQueue . Enqueue ( currentPulse ) ;
413- currentPulse = new Pulse ( ) ;
414- currentCount = 0 ;
415- }
416-
417417 if ( String . IsNullOrWhiteSpace ( ApiKey ) )
418418 {
419419 Logger . Warning ( "No API token - cannot pulse!" ) ;
@@ -427,7 +427,6 @@ private static Task ProcessPulses(CancellationTokenSource tokenSource)
427427 pulseProcessor_client = new HttpClient ( pulseProcessor_httpClientHandler ) ;
428428 proxyChangePending = false ;
429429 }
430- //var client = new WebClient { Proxy = CodeStatsPackage.GetProxy() };
431430 var jsonSerializer = new JavaScriptSerializer ( ) ;
432431
433432 string URL ;
@@ -443,10 +442,6 @@ private static Task ProcessPulses(CancellationTokenSource tokenSource)
443442 }
444443 if ( ! URL . EndsWith ( "my/pulses" ) )
445444 URL += "my/pulses" ;
446- /*client.Headers["User-Agent"] = Constants.PluginUserAgent;
447- client.Headers["Content-Type"] = "application/json";
448- client.Headers["Accept"] = "* /*";
449- client.Headers["X-API-Token"] = ApiKey;*/
450445
451446 Pulse result ;
452447 while ( pulseQueue . TryDequeue ( out result ) )
@@ -468,6 +463,7 @@ private static Task ProcessPulses(CancellationTokenSource tokenSource)
468463 { "Accept" , "*/*" } ,
469464 { "X-API-Token" , ApiKey }
470465 } ,
466+ // it will set Content-Type header for us
471467 Content = new StringContent ( json = jsonSerializer . Serialize ( result ) , Encoding . UTF8 , "application/json" )
472468 } ;
473469
@@ -476,7 +472,7 @@ private static Task ProcessPulses(CancellationTokenSource tokenSource)
476472 response . EnsureSuccessStatusCode ( ) ;
477473 string JsonResult = await response . Content . ReadAsStringAsync ( ) ;
478474 _lastPulse = DateTime . Now ;
479- if ( ! JsonResult . Contains ( @"""ok""" ) || ! JsonResult . Contains ( @"success" ) )
475+ if ( ! JsonResult . Contains ( @"""ok""" ) && ! JsonResult . Contains ( @"success" ) )
480476 {
481477 error = true ;
482478 Logger . Error ( @"Error pulsing, response does not contain ""ok"" or ""success"": " + JsonResult ) ;
@@ -719,8 +715,7 @@ public static void GetSettings(bool skipRead = false)
719715 private static void PromptApiKey ( )
720716 {
721717 Logger . Info ( "Please input your API token into the Code::Stats window." ) ;
722- /*var form*/
723- _apikeyForm = new CodeStats . Forms . ApiKeyForm ( ) ;
718+ /*var form*/ _apikeyForm = new CodeStats . Forms . ApiKeyForm ( ) ;
724719 _apikeyForm . ShowDialog ( ) ;
725720 }
726721
@@ -731,7 +726,7 @@ private static void SettingsPopup()
731726 _settingsForm = new CodeStats . Forms . SettingsForm ( ) ;
732727 _settingsForm . OnConfigSaved += SettingsFormOnConfigSaved ;
733728 }
734- // _settingsForm.Visible = false; // ?
729+ _settingsForm . Visible = false ; // ?
735730 _settingsForm . ShowDialog ( ) ;
736731 }
737732
@@ -854,58 +849,63 @@ internal static void PluginCleanUp()
854849 {
855850 nppStarted = false ;
856851
857- Logger . Info ( "Plugin cleanup on shutdown..." ) ;
858-
859- // Flush the current pulse
860- Logger . Debug ( "Flushing the current pulse..." ) ;
861- if ( pulseQueue != null && currentPulse != null && ! currentPulse . isEmpty ( ) )
852+ try
862853 {
863- pulseQueue . Enqueue ( currentPulse ) ;
864- currentPulse = new Pulse ( ) ;
865- currentCount = 0 ;
866- }
854+ Logger . Info ( "Plugin cleanup on shutdown..." ) ;
867855
868- Logger . Debug ( "Cancelling pulse processing..." ) ;
869- pulseProcessor_tokensource . Cancel ( ) ;
856+ // Flush the current pulse
857+ Logger . Debug ( "Flushing the current pulse..." ) ;
858+ FlushCurrentPulseIfNeeded ( ) ;
870859
871- if ( timer != null )
872- {
873- Logger . Debug ( "Stopping timer..." ) ;
874- timer . Stop ( ) ;
875- timer . Elapsed -= ProcessPulses ;
876- timer . Dispose ( ) ;
877- timer = null ;
878-
879- // make sure the queue is empty
880- //ProcessPulses();
881- }
860+ Logger . Debug ( "Cancelling pulse processing..." ) ;
861+ pulseProcessor_tokensource . Cancel ( ) ;
882862
883- // test if we can cancel and dump pulses
884- Logger . Debug ( "Waiting for pulse processor to be cancelled..." ) ;
885- try
886- {
887- if ( pulseProcessor != null )
888- pulseProcessor . Wait ( ) ;
889- }
890- catch ( Exception ex )
891- {
892- Logger . Error ( "Exception while waiting for pulse processor to be cancelled" , ex ) ;
893- }
863+ if ( timer != null )
864+ {
865+ Logger . Debug ( "Stopping timer..." ) ;
866+ timer . Stop ( ) ;
867+ timer . Elapsed -= ProcessPulses ;
868+ timer . Dispose ( ) ;
869+ timer = null ;
870+
871+ // make sure the queue is empty
872+ //ProcessPulses( );
873+ }
894874
895- Logger . Debug ( "Dequeueing remaining queued pulses..." ) ;
896- var jsonSerializer = new JavaScriptSerializer ( ) ;
897- Pulse result ;
898- while ( pulseQueue . TryDequeue ( out result ) )
899- {
900- if ( ! result . isEmpty ( ) )
875+ // test if we can cancel and dump pulses
876+ try
901877 {
902- string json = jsonSerializer . Serialize ( result ) ;
903- Logger . Debug ( "Unsaved pulse: " + json ) ;
878+ if ( pulseProcessor != null )
879+ {
880+ Logger . Debug ( "Waiting for pulse processor to be cancelled..." ) ;
881+ pulseProcessor . Wait ( ) ;
882+ }
883+ }
884+ catch ( Exception ex )
885+ {
886+ Logger . Error ( "Exception while waiting for pulse processor to be cancelled" , ex ) ;
904887 }
905- }
906888
907- Logger . Info ( "Plugin cleanup finished" ) ;
908- }
889+ Logger . Debug ( "Dequeueing remaining queued pulses..." ) ;
890+ var jsonSerializer = new JavaScriptSerializer ( ) ;
891+ Pulse result ;
892+ while ( pulseQueue . TryDequeue ( out result ) )
893+ {
894+ if ( ! result . isEmpty ( ) )
895+ {
896+ string json = jsonSerializer . Serialize ( result ) ;
897+ Logger . Debug ( "Unsaved pulse: " + json ) ;
898+ }
899+ }
909900
901+ Logger . Info ( "Plugin cleanup finished" ) ;
902+ Logger . FlushEverything ( ) ;
903+ }
904+ catch ( Exception ex )
905+ {
906+ Logger . Error ( "Exception while performing plugin shutdown cleanup" , ex ) ;
907+ Logger . FlushEverything ( ) ;
908+ }
909+ }
910910 }
911911}
0 commit comments