Ensure peerId is known for all node events

This commit is contained in:
benbierens 2024-07-29 14:17:54 +02:00
parent 9d9f65c5a3
commit ec99f5f8aa
No known key found for this signature in database
GPG Key ID: 877D2C2E09A22F3A
1 changed files with 32 additions and 1 deletions

View File

@ -84,6 +84,7 @@ namespace CodexPlugin.OverwatchSupport
private readonly NameIdMap nameIdMap;
private readonly string name;
private string peerId = string.Empty;
private readonly List<(DateTime, OverwatchCodexEvent)> pendingEvents = new List<(DateTime, OverwatchCodexEvent)>();
public CodexNodeTranscriptWriter(ITranscriptWriter writer, NameIdMap nameIdMap, string name)
{
@ -160,9 +161,39 @@ namespace CodexPlugin.OverwatchSupport
Name = name,
PeerId = peerId
};
action(e);
e.Write(utc, writer);
if (string.IsNullOrEmpty(peerId))
{
// If we don't know our peerId, don't write the events yet.
AddToCache(utc, e);
}
else
{
e.Write(utc, writer);
// Write any events that we cached when we didn't have our peerId yet.
WriteAndClearCache();
}
}
private void AddToCache(DateTime utc, OverwatchCodexEvent e)
{
pendingEvents.Add((utc, e));
}
private void WriteAndClearCache()
{
if (pendingEvents.Any())
{
foreach (var pair in pendingEvents)
{
pair.Item2.PeerId = peerId;
pair.Item2.Write(pair.Item1, writer);
}
pendingEvents.Clear();
}
}
}
}