Skip to content

Commit 8d3f1fa

Browse files
committed
flush every x seconds on web + flush on application paused/unfocused
1 parent 1fc2ecc commit 8d3f1fa

File tree

6 files changed

+71
-28
lines changed

6 files changed

+71
-28
lines changed

Assets/Scripts/Events/TrackEvent.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public void OnButtonClick()
1414
Track();
1515
}
1616

17-
private void Track()
17+
private async void Track()
1818
{
1919
try
2020
{
@@ -24,7 +24,7 @@ private void Track()
2424

2525
if (flushImmediately)
2626
{
27-
Talo.Events.Flush();
27+
await Talo.Events.Flush();
2828

2929
ResponseMessage.SetText($"{eventName} tracked and events flushed");
3030
}

Packages/com.trytalo.talo/Runtime/EventsAPI.cs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,24 @@ public async Task Flush()
4545
Talo.IdentityCheck();
4646

4747
var eventsToSend = queue.ToArray();
48-
queue.Clear();
48+
49+
if (eventsToSend.Length > 0)
50+
{
51+
queue.Clear();
4952

50-
var uri = new Uri(baseUrl);
51-
var content = JsonUtility.ToJson(new EventsPostRequest(eventsToSend));
53+
var uri = new Uri(baseUrl);
54+
var content = JsonUtility.ToJson(new EventsPostRequest(eventsToSend));
5255

53-
try
54-
{
55-
await Call(uri, "POST", content);
56-
}
57-
catch (Exception err)
58-
{
59-
Debug.LogError(err.Message);
60-
queue.AddRange(eventsToSend);
56+
try
57+
{
58+
await Call(uri, "POST", content);
59+
manager.ResetFlushTimer();
60+
}
61+
catch (Exception err)
62+
{
63+
Debug.LogError(err.Message);
64+
queue.AddRange(eventsToSend);
65+
}
6166
}
6267
}
6368
}

Packages/com.trytalo.talo/Runtime/SavesAPI.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,12 @@ public async Task<GameSave[]> GetSaves(SaveMode mode = SaveMode.BOTH)
6464

6565
if (mode != SaveMode.OFFLINE_ONLY)
6666
{
67-
try
68-
{
69-
var uri = new Uri(baseUrl + $"?aliasId={Talo.CurrentAlias.id}");
67+
var uri = new Uri(baseUrl + $"?aliasId={Talo.CurrentAlias.id}");
7068

71-
var json = await Call(uri, "GET");
72-
var res = JsonUtility.FromJson<SavesIndexResponse>(json);
69+
var json = await Call(uri, "GET");
70+
var res = JsonUtility.FromJson<SavesIndexResponse>(json);
7371

74-
saves.AddRange(res.saves);
75-
} catch
76-
{
77-
Debug.LogWarning("Failed to load online saves");
78-
}
72+
saves.AddRange(res.saves);
7973
}
8074

8175
_allSaves = saves;

Packages/com.trytalo.talo/Runtime/Talo.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ static Talo()
6363
return;
6464
}
6565

66-
var go = new GameObject("Talo Manager");
67-
var manager = go.AddComponent<TaloManager>();
66+
var manager = new GameObject("Talo Manager").AddComponent<TaloManager>();
6867
manager.settings = settings;
6968

7069
_events = new EventsAPI(manager);
@@ -74,9 +73,14 @@ static Talo()
7473
_stats = new StatsAPI(manager);
7574
}
7675

76+
public static bool HasIdentity()
77+
{
78+
return CurrentAlias != null;
79+
}
80+
7781
public static void IdentityCheck()
7882
{
79-
if (CurrentAlias == null)
83+
if (!HasIdentity())
8084
{
8185
throw new Exception("You need to identify a player using Talo.Identify() before doing this.");
8286
}

Packages/com.trytalo.talo/Runtime/TaloManager.cs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,52 @@ public class TaloManager : MonoBehaviour
66
{
77
public TaloSettings settings;
88

9+
private float tmrFlush;
10+
911
private void Awake()
1012
{
1113
DontDestroyOnLoad(gameObject);
1214
}
1315

14-
private async void OnApplicationQuit()
16+
private void OnApplicationQuit()
17+
{
18+
DoFlush();
19+
}
20+
21+
private void OnApplicationFocus(bool hasFocus)
22+
{
23+
if (!hasFocus) DoFlush();
24+
}
25+
26+
private void OnApplicationPause(bool isPaused)
27+
{
28+
if (isPaused) DoFlush();
29+
}
30+
31+
private async void DoFlush()
32+
{
33+
if (Talo.HasIdentity())
34+
{
35+
await Talo.Events.Flush();
36+
}
37+
}
38+
39+
private void Update()
40+
{
41+
if (Application.platform == RuntimePlatform.WebGLPlayer)
42+
{
43+
tmrFlush += Time.deltaTime;
44+
if (tmrFlush >= settings.webGLEventFlushRate)
45+
{
46+
DoFlush();
47+
tmrFlush = 0;
48+
}
49+
}
50+
}
51+
52+
public void ResetFlushTimer()
1553
{
16-
await Talo.Events.Flush();
54+
tmrFlush = 0;
1755
}
1856
}
1957
}

Packages/com.trytalo.talo/Runtime/TaloSettings.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ public class TaloSettings : ScriptableObject
77
{
88
public string accessKey;
99
public string apiUrl = "https://api.trytalo.com/v1";
10+
[Tooltip("How often in seconds events are flushed in a WebGL build, see the docs for more info")]
11+
public float webGLEventFlushRate = 30f;
1012
}
1113
}

0 commit comments

Comments
 (0)