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(); } } }