protected IEnumerator TestInterceptException() { ProgressResult <float, bool> result = new ProgressResult <float, bool>(true); /* Register a callback */ result.Callbackable().OnProgressCallback(p => Debug.LogFormat("Progress:{0}%", p * 100)); result.Callbackable().OnCallback((r) => { Debug.LogFormat("The task is finished. IsCancelled:{0} Result:{1} Exception:{2}", r.IsCancelled, r.Result, r.Exception); }); InterceptableEnumerator routine = new InterceptableEnumerator(DoTask(result)); routine.RegisterCatchBlock((e) => { Debug.LogError(e); }); routine.RegisterFinallyBlock(() => { Debug.Log("this is a finally block."); }); /* Start the task */ this.StartCoroutine(routine); yield break; }
protected IEnumerator TestInterceptMoveNextMethod() { ProgressResult <float, bool> result = new ProgressResult <float, bool>(true); /* Register a callback */ result.Callbackable().OnProgressCallback(p => Debug.LogFormat("Progress:{0}%", p * 100)); result.Callbackable().OnCallback((r) => { Debug.LogFormat("The task is finished. IsCancelled:{0} Result:{1} Exception:{2}", r.IsCancelled, r.Result, r.Exception); }); InterceptableEnumerator routine = new InterceptableEnumerator(DoTask(result)); /* if result.IsCancellationRequested == true ,the task will be cancelled. */ routine.RegisterConditionBlock(() => !(result.IsCancellationRequested)); routine.RegisterFinallyBlock(() => { Debug.Log("this is a finally block."); }); /* Start the task */ this.StartCoroutine(routine); yield return(new WaitForSeconds(0.5f)); result.Cancel(); }
protected IEnumerator Wrap(IEnumerator task) { this.Retain(); InterceptableEnumerator enumerator = new InterceptableEnumerator(task); enumerator.RegisterFinallyBlock(() => { this.Release(); }); return(enumerator); }
protected IEnumerator Wrap(IEnumerator task, IPromise promise) { this.Retain(); InterceptableEnumerator enumerator = new InterceptableEnumerator(task); enumerator.RegisterCatchBlock(e => { promise.SetException(e); }); enumerator.RegisterFinallyBlock(() => { this.Release(); }); return(enumerator); }
private static void RunOnCoroutine(IEnumerator routine, Action <Exception> onFinished) { InterceptableEnumerator enumerator = routine is InterceptableEnumerator ? (InterceptableEnumerator)routine : new InterceptableEnumerator(routine); Exception exception = null; enumerator.RegisterCatchBlock(e => { exception = e; }); enumerator.RegisterFinallyBlock(() => { onFinished(exception); }); Executors.RunOnCoroutineNoReturn(enumerator); }
private static CoroutineAwaiter <TResult> RunOnCoroutine <TResult>(IEnumerator routine, Func <TResult> getter) { CoroutineAwaiter <TResult> awaiter = new CoroutineAwaiter <TResult>(); InterceptableEnumerator enumerator = routine is InterceptableEnumerator ? (InterceptableEnumerator)routine : new InterceptableEnumerator(routine); enumerator.RegisterCatchBlock(e => { awaiter.SetResult(default(TResult), e); }); enumerator.RegisterFinallyBlock(() => { if (!awaiter.IsCompleted) { awaiter.SetResult(getter(), null); } }); Executors.RunOnCoroutineNoReturn(enumerator); return(awaiter); }
private static CoroutineAwaiter RunOnCoroutine(IEnumerator routine) { CoroutineAwaiter awaiter = new CoroutineAwaiter(); InterceptableEnumerator enumerator = routine is InterceptableEnumerator ? (InterceptableEnumerator)routine : new InterceptableEnumerator(routine); enumerator.RegisterCatchBlock(e => { awaiter.SetResult(e); }); enumerator.RegisterFinallyBlock(() => { if (!awaiter.IsCompleted) { awaiter.SetResult(null); } }); Executors.RunOnCoroutineNoReturn(enumerator); return(awaiter); }
protected IEnumerator TestInterceptException() { // var routine = new InterceptableEnumerator(DoTask()); var routine = new InterceptableEnumerator(DoTaskHasException()); routine.RegisterCatchBlock((e) => { Debug.LogError($"Exeption is Catched in Coroutine ! {e.ToString()}"); }); routine.RegisterFinallyBlock(() => { Debug.Log($"Finally block for Coroutine !"); }); StartCoroutine(routine); yield break; }
public virtual ISceneLoadingResult <Scene> LoadSceneAsync(string path, LoadSceneMode mode = LoadSceneMode.Single) { SceneLoadingResult <Scene> result = new SceneLoadingResult <Scene>(); try { if (string.IsNullOrEmpty(path)) { throw new System.ArgumentNullException("path", "The path is null or empty!"); } InterceptableEnumerator enumerator = new InterceptableEnumerator(DoLoadSceneAsync(result, path, mode)); enumerator.RegisterCatchBlock(e => { result.SetException(e); if (log.IsErrorEnabled) { log.Error(e); } }); enumerator.RegisterFinallyBlock(() => { if (!result.IsDone) { result.SetException(new System.Exception("No value given the Result")); } }); Executors.RunOnCoroutineNoReturn(enumerator); } catch (System.Exception e) { result.Progress = 0f; result.SetException(e); } return(result); }