From 241b3e1550e05675d963914679c3abbabb81a7ae Mon Sep 17 00:00:00 2001 From: tudor <7089284+tudddorrr@users.noreply.github.com> Date: Thu, 19 Jun 2025 00:58:00 +0100 Subject: [PATCH] add events flush lock to prevent duplicates --- .../Talo/Runtime/APIs/EventsAPI.cs | 50 +++++++++++++------ 1 file changed, 36 insertions(+), 14 deletions(-) diff --git a/Assets/Talo Game Services/Talo/Runtime/APIs/EventsAPI.cs b/Assets/Talo Game Services/Talo/Runtime/APIs/EventsAPI.cs index 3541508..040212d 100644 --- a/Assets/Talo Game Services/Talo/Runtime/APIs/EventsAPI.cs +++ b/Assets/Talo Game Services/Talo/Runtime/APIs/EventsAPI.cs @@ -10,9 +10,13 @@ public class EventsAPI : BaseAPI { public event Action OnFlushed; - private List queue = new List(); + private List queue = new (); private readonly int minQueueSize = 10; + private List eventsToFlush = new (); + private bool lockFlushes; + private bool flushAttemptedDuringLock; + public EventsAPI() : base("v1/events") { } private string GetWindowMode() @@ -77,23 +81,41 @@ public async Task Flush() Talo.IdentityCheck(); var eventsToSend = queue.ToArray(); - - if (eventsToSend.Length > 0) + if (eventsToSend.Length == 0) + { + return; + } + + if (lockFlushes) + { + flushAttemptedDuringLock = true; + return; + } + + var uri = new Uri(baseUrl); + var content = JsonUtility.ToJson(new EventsPostRequest(eventsToSend)); + + try { + lockFlushes = true; + eventsToFlush.AddRange(eventsToSend); queue.Clear(); - var uri = new Uri(baseUrl); - var content = JsonUtility.ToJson(new EventsPostRequest(eventsToSend)); + await Call(uri, "POST", content); + OnFlushed.Invoke(); - try - { - await Call(uri, "POST", content); - OnFlushed.Invoke(); - } - catch (Exception err) - { - Debug.LogError(err.Message); - } + eventsToFlush.Clear(); + lockFlushes = false; + } + catch (Exception err) + { + Debug.LogError(err.Message); + } + + if (flushAttemptedDuringLock) + { + flushAttemptedDuringLock = false; + await Flush(); } } }