Skip to content

Commit

Permalink
[HB-6718] Provide Task Continuation Extension for Main Thread (#201)
Browse files Browse the repository at this point in the history
* Adding Task Continuation Extensions on Mediation
* Fixing WinningBidInfo Price on Android
  • Loading branch information
SCastanedaMunoz authored Nov 2, 2023
1 parent d9fc78b commit 2b3fc79
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
39 changes: 38 additions & 1 deletion com.chartboost.mediation/Runtime/Events/EventProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Chartboost.AdFormats.Banner;
using Chartboost.Utilities;
using UnityEditor;
Expand Down Expand Up @@ -31,6 +32,7 @@ internal enum BannerAdEvents
}

private static SynchronizationContext _context;
private static TaskScheduler _unityScheduler;

/// <summary>
/// Called when an unexpected system error occurred.
Expand All @@ -45,7 +47,42 @@ internal enum BannerAdEvents
[InitializeOnLoadMethod]
#endif
[RuntimeInitializeOnLoadMethod]
private static void Initialize()=> _context ??= SynchronizationContext.Current;
private static void Initialize()
{
_context ??= SynchronizationContext.Current;
_unityScheduler ??= TaskScheduler.FromCurrentSynchronizationContext();
}

/// <summary>
/// Creates a continuation that executes asynchronously, on the Unity main thread, when the target <see cref="Task{T}"/> completes.
/// </summary>
/// <param name="task">Target <see cref="Task"/>.</param>
/// <param name="continuation">An action to run when the <see cref="Task"/> completes. </param>
/// <typeparam name="T">The type of the result produced by the <see cref="Task"/>.</typeparam>
/// <returns>A new continuation <see cref="Task"/>.</returns>
public static Task ContinueWithOnMainThread<T>(this Task<T> task, Action<Task<T>> continuation)
{
var ret = task.ContinueWith(continuation, CancellationToken.None, TaskContinuationOptions.None, _unityScheduler);
ret.AppendExceptionLogging();
return ret;
}

/// <summary>
/// Creates a continuation that executes asynchronously, on the Unity main thread, when the target <see cref="Task"/> completes.
/// </summary>
/// <param name="task">Target <see cref="Task"/>.</param>
/// <param name="continuation">An action to run when the <see cref="Task"/> completes. </param>
/// <typeparam name="T">The type of the result produced by the <see cref="Task"/>.</typeparam>
/// <returns>A new continuation <see cref="Task"/>.</returns>
public static Task ContinueWithOnMainThread(this Task task, Action<Task> continuation)
{
var ret = task.ContinueWith(continuation, CancellationToken.None, TaskContinuationOptions.None, _unityScheduler);
ret.AppendExceptionLogging();
return ret;
}

private static void AppendExceptionLogging(this Task inputTask)
=> inputTask.ContinueWith(faultedTask => Debug.LogException(faultedTask.Exception), TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.ExecuteSynchronously);

public static void ProcessEventWithILRD(string dataString, ChartboostMediationILRDEvent ilrdEvent)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ internal static class AndroidConstants
internal const string PropertyAuctionId = "auction-id";
internal const string PropertyLineItemId = "line_item_id";
internal const string PropertyLineItemName = "line_item_name";
internal const string PropertyPrice = "line_item_name";
internal const string PropertyPrice = "price";
internal const string PropertyName = "name";
internal const string PropertyWidth = "width";
internal const string PropertyHeight = "height";
Expand Down

0 comments on commit 2b3fc79

Please sign in to comment.