Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 36 additions & 14 deletions Assets/Talo Game Services/Talo/Runtime/APIs/EventsAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ public class EventsAPI : BaseAPI
{
public event Action OnFlushed;

private List<Event> queue = new List<Event>();
private List<Event> queue = new ();
private readonly int minQueueSize = 10;

private List<Event> eventsToFlush = new ();
private bool lockFlushes;
private bool flushAttemptedDuringLock;

public EventsAPI() : base("v1/events") { }

private string GetWindowMode()
Expand Down Expand Up @@ -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();
}
}
}
Expand Down