Skip to content

Commit

Permalink
Dump unsent pulses on quit and restore on next launch (closes #24)
Browse files Browse the repository at this point in the history
  • Loading branch information
p0358 committed Nov 1, 2022
1 parent 71fc062 commit 143ce3f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
39 changes: 38 additions & 1 deletion CodeStats/CodeStatsPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,34 @@ private static void InitializeAsync()
ReportStats();
}

// Restore pulses previously dumped to file (if we had to shut down before being able to pulse them)
Task.Run(() =>
{
try
{
var path = ConfigFile.GetUnsavedPulsesFilePath();
if (File.Exists(path))
{
var json = File.ReadAllText(path);
var serializer = new JavaScriptSerializer();
var pulses = serializer.Deserialize<List<Pulse>>(json);
foreach (var pulse in pulses)
{
Logger.Info("Restoring dumped pulse: " + serializer.Serialize(pulse));
pulseQueue.Enqueue(pulse);
}
UpdateStatusbar();
File.Delete(path);
}
}
catch (Exception ex)
{
Logger.Error("Error trying to restore dumped pulses from file!", ex);
}
});

Logger.Info(string.Format("Finished initializing Code::Stats v{0}", Constants.PluginVersion));
}
catch (Exception ex)
Expand Down Expand Up @@ -976,15 +1004,24 @@ internal static void PluginCleanUp()
Logger.Debug("Dequeueing remaining queued pulses...");
var jsonSerializer = new JavaScriptSerializer();
Pulse result;
var unsavedPulses = new List<Pulse>();
while (pulseQueue.TryDequeue(out result))
{
if (!result.isEmpty())
{
unsavedPulses.Add(result);
string json = jsonSerializer.Serialize(result);
Logger.Info("Unsaved pulse: " + json);
// TODO: dump them to file and restore
}
}
if (unsavedPulses.Count > 0)
{
// dump them to file
var path = ConfigFile.GetUnsavedPulsesFilePath();
Logger.Info("Dumping " + unsavedPulses.Count + " unsaved pulses to file at: " + path);
string json = jsonSerializer.Serialize(unsavedPulses);
File.WriteAllText(path, json);
}

Logger.Info("Plugin cleanup on shutdown finished");
Logger.FlushEverything();
Expand Down
9 changes: 9 additions & 0 deletions CodeStats/ConfigFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,5 +181,14 @@ public static string GetCustomExtensionMappingFilePath()

return Path.Combine(configFilePath, "CodeStatsCustomExtensionMapping.json");
}

public static string GetUnsavedPulsesFilePath()
{
StringBuilder sbConfigFilePath = new StringBuilder(Win32.MAX_PATH);
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint)NppMsg.NPPM_GETPLUGINSCONFIGDIR, Win32.MAX_PATH, sbConfigFilePath);
string configFilePath = sbConfigFilePath.ToString();

return Path.Combine(configFilePath, "CodeStatsPendingPulses.json");
}
}
}
6 changes: 6 additions & 0 deletions CodeStats/Pulse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ internal class XpObj
public string language { get; set; }
public int xp { get; set; }

public XpObj()
{
language = "Plain text";
xp = 0;
}

public XpObj(string lang)
{
language = lang;
Expand Down
7 changes: 7 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ History
-------


1.1.1 (2022-11-01)
++++++++++++++++++

- Fixed version check message claiming your version is outdated if it was in fact newer (it was checking for simple equality)
- 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


1.1.0 (2022-10-20)
++++++++++++++++++

Expand Down

0 comments on commit 143ce3f

Please sign in to comment.