From 1e243100d95d8eb3ef45686164ac2ff89f16f9cc Mon Sep 17 00:00:00 2001 From: Felipe Cardozo Date: Tue, 2 May 2023 17:15:45 -0300 Subject: [PATCH] Use CoroutineUtils.Try in the WrappedRequest method --- Runtime/Scripts/Helpers/CoroutineRunner.cs | 27 +++++ ...rapper.cs.meta => CoroutineRunner.cs.meta} | 2 +- Runtime/Scripts/Helpers/CoroutineUtils.cs | 3 +- Runtime/Scripts/Helpers/CoroutineWrapper.cs | 99 ------------------- Runtime/Scripts/TezosAPI/HttpClient.cs | 7 +- 5 files changed, 32 insertions(+), 106 deletions(-) create mode 100644 Runtime/Scripts/Helpers/CoroutineRunner.cs rename Runtime/Scripts/Helpers/{CoroutineWrapper.cs.meta => CoroutineRunner.cs.meta} (83%) delete mode 100644 Runtime/Scripts/Helpers/CoroutineWrapper.cs diff --git a/Runtime/Scripts/Helpers/CoroutineRunner.cs b/Runtime/Scripts/Helpers/CoroutineRunner.cs new file mode 100644 index 00000000..93cfc967 --- /dev/null +++ b/Runtime/Scripts/Helpers/CoroutineRunner.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections; +using UnityEngine; + +/// +/// Helper class that will allow to run a coroutine +/// +public class CoroutineRunner : MonoBehaviour +{ + private static CoroutineRunner _instance; + + public static CoroutineRunner Instance + { + get + { + if (_instance == null) + _instance = (new GameObject("CoroutineRunner")).AddComponent(); + + return _instance; + } + } + + private void Awake() + { + DontDestroyOnLoad(gameObject); + } +} diff --git a/Runtime/Scripts/Helpers/CoroutineWrapper.cs.meta b/Runtime/Scripts/Helpers/CoroutineRunner.cs.meta similarity index 83% rename from Runtime/Scripts/Helpers/CoroutineWrapper.cs.meta rename to Runtime/Scripts/Helpers/CoroutineRunner.cs.meta index e602a490..8fa57ae7 100644 --- a/Runtime/Scripts/Helpers/CoroutineWrapper.cs.meta +++ b/Runtime/Scripts/Helpers/CoroutineRunner.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 39830bc66b1aa4e8ca77da851c0a25ff +guid: dd493b835c5e842799b48829edd361be MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Runtime/Scripts/Helpers/CoroutineUtils.cs b/Runtime/Scripts/Helpers/CoroutineUtils.cs index dc60ab8c..4ad36a89 100644 --- a/Runtime/Scripts/Helpers/CoroutineUtils.cs +++ b/Runtime/Scripts/Helpers/CoroutineUtils.cs @@ -41,6 +41,5 @@ public static IEnumerator Try( } yield return current; } - exceptionHandler?.Invoke(null); } -} \ No newline at end of file +} diff --git a/Runtime/Scripts/Helpers/CoroutineWrapper.cs b/Runtime/Scripts/Helpers/CoroutineWrapper.cs deleted file mode 100644 index 5a053885..00000000 --- a/Runtime/Scripts/Helpers/CoroutineWrapper.cs +++ /dev/null @@ -1,99 +0,0 @@ -using System; -using System.Collections; -using UnityEngine; - -/// -/// Wraps a coroutine allowing to extract the result when it's completed -/// -/// Type of result expected -public class CoroutineWrapper : IEnumerator -{ - /// - /// Event raised when the coroutine is complete - /// - public readonly Action Completed; - - private readonly IEnumerator _targetCoroutine; - - /// - /// Exception triggered during the execution of the coroutine. - /// - public Exception Exception { get; private set; } - - /// - public object Current { get; private set; } - - /// - /// Result extracted from the coroutine when it's complete - /// - public T Result { get; private set; } - - /// - /// Create an instance of the wrapper - /// - /// Coroutine that will be executed - /// Callback that will be called when the coroutine is complete - public CoroutineWrapper(IEnumerator coroutine, Action callback = null) - { - _targetCoroutine = coroutine; - if (callback != null) - { - Completed += callback; - } - } - - /// - public bool MoveNext() - { - try - { - if (_targetCoroutine.MoveNext()) - { - Current = _targetCoroutine.Current; - return true; - } - - Result = (T)_targetCoroutine.Current; - Current = _targetCoroutine.Current; - Completed?.Invoke(Result); - return false; - } - catch (Exception e) - { - Debug.LogError("Exception " + e.Message); - Exception = e; - Completed?.Invoke(default); - return false; - } - } - - /// - public void Reset() - { - _targetCoroutine.Reset(); - } -} - -/// -/// Helper class that will allow to run a coroutine -/// -public class CoroutineRunner : MonoBehaviour -{ - private static CoroutineRunner _instance; - - public static CoroutineRunner Instance - { - get - { - if (_instance == null) - _instance = (new GameObject("CoroutineRunner")).AddComponent(); - - return _instance; - } - } - - private void Awake() - { - DontDestroyOnLoad(gameObject); - } -} \ No newline at end of file diff --git a/Runtime/Scripts/TezosAPI/HttpClient.cs b/Runtime/Scripts/TezosAPI/HttpClient.cs index d9262261..d20543f2 100644 --- a/Runtime/Scripts/TezosAPI/HttpClient.cs +++ b/Runtime/Scripts/TezosAPI/HttpClient.cs @@ -64,10 +64,9 @@ private UnityWebRequest GetUnityWebRequest(string method, string path) public static IEnumerator WrappedRequest(IEnumerator op, Action callback) { - var counterRoutine = new CoroutineWrapper(op); + var counterRoutine = CoroutineUtils.Try(op); yield return counterRoutine; - var counter = counterRoutine.Result; - callback?.Invoke(counter); + callback?.Invoke((T) counterRoutine.Current); } } @@ -77,4 +76,4 @@ internal static class HttpHeaders public static KeyValuePair Accept => new("Accept", "application/json"); public static KeyValuePair UserAgent => new("User-Agent", "tezos-unity-sdk"); } -} \ No newline at end of file +}