Skip to content

Commit 241b3e1

Browse files
committed
add events flush lock to prevent duplicates
1 parent ce06c30 commit 241b3e1

File tree

1 file changed

+36
-14
lines changed

1 file changed

+36
-14
lines changed

Assets/Talo Game Services/Talo/Runtime/APIs/EventsAPI.cs

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@ public class EventsAPI : BaseAPI
1010
{
1111
public event Action OnFlushed;
1212

13-
private List<Event> queue = new List<Event>();
13+
private List<Event> queue = new ();
1414
private readonly int minQueueSize = 10;
1515

16+
private List<Event> eventsToFlush = new ();
17+
private bool lockFlushes;
18+
private bool flushAttemptedDuringLock;
19+
1620
public EventsAPI() : base("v1/events") { }
1721

1822
private string GetWindowMode()
@@ -77,23 +81,41 @@ public async Task Flush()
7781
Talo.IdentityCheck();
7882

7983
var eventsToSend = queue.ToArray();
80-
81-
if (eventsToSend.Length > 0)
84+
if (eventsToSend.Length == 0)
85+
{
86+
return;
87+
}
88+
89+
if (lockFlushes)
90+
{
91+
flushAttemptedDuringLock = true;
92+
return;
93+
}
94+
95+
var uri = new Uri(baseUrl);
96+
var content = JsonUtility.ToJson(new EventsPostRequest(eventsToSend));
97+
98+
try
8299
{
100+
lockFlushes = true;
101+
eventsToFlush.AddRange(eventsToSend);
83102
queue.Clear();
84103

85-
var uri = new Uri(baseUrl);
86-
var content = JsonUtility.ToJson(new EventsPostRequest(eventsToSend));
104+
await Call(uri, "POST", content);
105+
OnFlushed.Invoke();
87106

88-
try
89-
{
90-
await Call(uri, "POST", content);
91-
OnFlushed.Invoke();
92-
}
93-
catch (Exception err)
94-
{
95-
Debug.LogError(err.Message);
96-
}
107+
eventsToFlush.Clear();
108+
lockFlushes = false;
109+
}
110+
catch (Exception err)
111+
{
112+
Debug.LogError(err.Message);
113+
}
114+
115+
if (flushAttemptedDuringLock)
116+
{
117+
flushAttemptedDuringLock = false;
118+
await Flush();
97119
}
98120
}
99121
}

0 commit comments

Comments
 (0)