Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add onFlushComplete callback #168

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
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
38 changes: 32 additions & 6 deletions Mixpanel/Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,26 @@ private IEnumerator WaitAndFlush()
}
}

internal void DoFlush()
internal void DoFlush(Action<bool> onFlushComplete = null)
{
StartCoroutine(SendData(MixpanelStorage.FlushType.EVENTS));
StartCoroutine(SendData(MixpanelStorage.FlushType.PEOPLE));
int coroutinesCount = 2; // Number of coroutines to wait for
bool overallSuccess = true;

Action<bool> onComplete = onFlushComplete != null ? success =>
{
overallSuccess &= success;
CheckCompletion(onFlushComplete, ref coroutinesCount, overallSuccess);
}
: null;
StartCoroutine(SendData(MixpanelStorage.FlushType.EVENTS, onComplete));
StartCoroutine(SendData(MixpanelStorage.FlushType.PEOPLE, onComplete));
}

private IEnumerator SendData(MixpanelStorage.FlushType flushType)
private IEnumerator SendData(MixpanelStorage.FlushType flushType, Action<bool> onComplete)
{
if (_retryTime > DateTime.Now && _retryCount > 0) {
if (_retryTime > DateTime.Now && _retryCount > 0)
{
onComplete?.Invoke(false);
yield break;
}

Expand All @@ -176,17 +187,32 @@ private IEnumerator SendData(MixpanelStorage.FlushType flushType)
_retryTime = DateTime.Now;
_retryTime = _retryTime.AddSeconds(retryIn);
Mixpanel.Log("Retrying request in " + retryIn + " seconds (retryCount=" + _retryCount + ")");
onComplete?.Invoke(false);
yield break;
}
else
{
_retryCount = 0;
_retryCount = 0;
MixpanelStorage.DeleteBatchTrackingData(batch);
batch = MixpanelStorage.DequeueBatchTrackingData(flushType, Config.BatchSize);
Mixpanel.Log("Successfully posted to " + url);
}
}
}

onComplete?.Invoke(true);
}

private void CheckCompletion(Action<bool> onFlushComplete, ref int coroutinesCount, bool overallSuccess)
{
// Decrease the counter
coroutinesCount--;

// If all coroutines are finished, invoke the onFlushComplete callback
if (coroutinesCount == 0)
{
onFlushComplete?.Invoke(overallSuccess);
}
}

private IEnumerator SendHttpEvent(string eventName, string apiToken, string distinctId, string properties, bool updatePeople)
Expand Down
5 changes: 3 additions & 2 deletions Mixpanel/MixpanelAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,11 @@ public static void Unregister(string key)
/// <summary>
/// Flushes the queued data to Mixpanel
/// </summary>
public static void Flush()
/// <param name="onFlushComplete">callback to be called when the flush is complete. Returns true if overall success</param>
public static void Flush(Action<bool> onFlushComplete = null)
{
if (!IsInitialized()) return;
Controller.GetInstance().DoFlush();
Controller.GetInstance().DoFlush(onFlushComplete);
}

/// <summary>
Expand Down