Skip to content

Commit 143ce3f

Browse files
committed
Dump unsent pulses on quit and restore on next launch (closes #24)
1 parent 71fc062 commit 143ce3f

4 files changed

Lines changed: 60 additions & 1 deletion

File tree

CodeStats/CodeStatsPackage.cs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,34 @@ private static void InitializeAsync()
165165
ReportStats();
166166
}
167167

168+
// Restore pulses previously dumped to file (if we had to shut down before being able to pulse them)
169+
Task.Run(() =>
170+
{
171+
try
172+
{
173+
var path = ConfigFile.GetUnsavedPulsesFilePath();
174+
if (File.Exists(path))
175+
{
176+
var json = File.ReadAllText(path);
177+
178+
var serializer = new JavaScriptSerializer();
179+
var pulses = serializer.Deserialize<List<Pulse>>(json);
180+
foreach (var pulse in pulses)
181+
{
182+
Logger.Info("Restoring dumped pulse: " + serializer.Serialize(pulse));
183+
pulseQueue.Enqueue(pulse);
184+
}
185+
186+
UpdateStatusbar();
187+
File.Delete(path);
188+
}
189+
}
190+
catch (Exception ex)
191+
{
192+
Logger.Error("Error trying to restore dumped pulses from file!", ex);
193+
}
194+
});
195+
168196
Logger.Info(string.Format("Finished initializing Code::Stats v{0}", Constants.PluginVersion));
169197
}
170198
catch (Exception ex)
@@ -976,15 +1004,24 @@ internal static void PluginCleanUp()
9761004
Logger.Debug("Dequeueing remaining queued pulses...");
9771005
var jsonSerializer = new JavaScriptSerializer();
9781006
Pulse result;
1007+
var unsavedPulses = new List<Pulse>();
9791008
while (pulseQueue.TryDequeue(out result))
9801009
{
9811010
if (!result.isEmpty())
9821011
{
1012+
unsavedPulses.Add(result);
9831013
string json = jsonSerializer.Serialize(result);
9841014
Logger.Info("Unsaved pulse: " + json);
985-
// TODO: dump them to file and restore
9861015
}
9871016
}
1017+
if (unsavedPulses.Count > 0)
1018+
{
1019+
// dump them to file
1020+
var path = ConfigFile.GetUnsavedPulsesFilePath();
1021+
Logger.Info("Dumping " + unsavedPulses.Count + " unsaved pulses to file at: " + path);
1022+
string json = jsonSerializer.Serialize(unsavedPulses);
1023+
File.WriteAllText(path, json);
1024+
}
9881025

9891026
Logger.Info("Plugin cleanup on shutdown finished");
9901027
Logger.FlushEverything();

CodeStats/ConfigFile.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,5 +181,14 @@ public static string GetCustomExtensionMappingFilePath()
181181

182182
return Path.Combine(configFilePath, "CodeStatsCustomExtensionMapping.json");
183183
}
184+
185+
public static string GetUnsavedPulsesFilePath()
186+
{
187+
StringBuilder sbConfigFilePath = new StringBuilder(Win32.MAX_PATH);
188+
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint)NppMsg.NPPM_GETPLUGINSCONFIGDIR, Win32.MAX_PATH, sbConfigFilePath);
189+
string configFilePath = sbConfigFilePath.ToString();
190+
191+
return Path.Combine(configFilePath, "CodeStatsPendingPulses.json");
192+
}
184193
}
185194
}

CodeStats/Pulse.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ internal class XpObj
88
public string language { get; set; }
99
public int xp { get; set; }
1010

11+
public XpObj()
12+
{
13+
language = "Plain text";
14+
xp = 0;
15+
}
16+
1117
public XpObj(string lang)
1218
{
1319
language = lang;

HISTORY.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ History
33
-------
44

55

6+
1.1.1 (2022-11-01)
7+
++++++++++++++++++
8+
9+
- Fixed version check message claiming your version is outdated if it was in fact newer (it was checking for simple equality)
10+
- Pulses that were not possible to be sent (for example due to connection issues) will now be dumped to disk on Notepad++ close and later restored, instead of being discarded
11+
12+
613
1.1.0 (2022-10-20)
714
++++++++++++++++++
815

0 commit comments

Comments
 (0)