using System; using System.Runtime.CompilerServices; using System.Threading.Tasks; using Chartboost.Logging; using UnityEngine; namespace Chartboost { public class AwaitableAndroidJavaProxy : AndroidJavaProxy { public TaskAwaiter GetAwaiter() { if (_taskCompletionSource != null) return _taskCompletionSource.Task.GetAwaiter(); _taskCompletionSource = new TaskCompletionSource(); if (_isComplete) _setResult(); else DidComplete += result => _setResult(); return _taskCompletionSource.Task.GetAwaiter(); } protected AwaitableAndroidJavaProxy(string nativeInterface) : base(nativeInterface) { } protected void _complete(TResult result) { if (_isComplete) return; _result = result; var toComplete = DidComplete; DidComplete = null; _isComplete = true; toComplete?.Invoke(_result); } private void _setResult() { try { _taskCompletionSource.TrySetResult(_result); } catch (ObjectDisposedException exception) { LogController.LogException(exception); } } private TaskCompletionSource _taskCompletionSource; private event Action DidComplete; private TResult _result; private bool _isComplete; } }