|
- using System;
- using System.Collections.Generic;
- using UnityEngine;
-
- namespace SUISS.Scheduling
- {
- public class CoTask<TResult, TError> : ICoroutineTask<TResult, TError>, IYieldStatement
- {
- public CoTask(IEnumerator<IYieldStatement> coroutine)
- {
- this._routine = this.InternalRoutine(coroutine);
- this._error = default(TError);
- this._result = default(TResult);
- this._running = false;
- this._paused = false;
- }
-
- public static CoTask<TResult, TError> CreateAndStart(IEnumerator<IYieldStatement> coroutine, IScheduler scheduler)
- {
- CoTask<TResult, TError> coTask = new CoTask<TResult, TError>(coroutine);
- coTask.Start(scheduler);
- return coTask;
- }
-
- [Obsolete("Use CoReturn.Error(value)")]
- public static IYieldStatement Return(TError value)
- {
- return new CoError<TError>(value);
- }
-
- [Obsolete("Use CoReturn.Result(value)")]
- public static IYieldStatement Return(TResult value)
- {
- return new CoResult<TResult>(value);
- }
-
- public TResult Result
- {
- get
- {
- if (this._running)
- {
- throw new Exception(string.Format("{0}.Result was read before return value was set!", this));
- }
- return this._result;
- }
- }
-
- public TError Error
- {
- get
- {
- if (this._running)
- {
- throw new Exception(string.Format("{0}.Error was read before return error was set!", this));
- }
- return this._error;
- }
- }
-
- public bool Running
- {
- get
- {
- return this._running;
- }
- }
-
- public bool IsDone
- {
- get
- {
- return !this._running;
- }
- }
-
- public void Stop()
- {
- this._running = false;
- if (this._onComplete != null)
- {
- this._onComplete.Stop();
- }
- }
-
- public void Start(IScheduler scheduler)
- {
- this._running = true;
- this._onComplete = scheduler.StartCoroutine(this._routine);
- }
-
- public void Pause()
- {
- this._paused = true;
- }
-
- public void Unpause()
- {
- this._paused = false;
- }
-
- public object UnityYieldable()
- {
- return this._onComplete.UnityYieldable();
- }
-
- public object UnityYieldable(MonoBehaviour runner)
- {
- return this._onComplete.UnityYieldable(runner);
- }
-
- public object SUISSYieldable()
- {
- return this._onComplete.SUISSYieldable();
- }
-
- private IEnumerator<IYieldStatement> InternalRoutine(IEnumerator<IYieldStatement> coroutine)
- {
- while (this._running)
- {
- while (this._paused)
- {
- yield return null;
- }
- if (!coroutine.MoveNext())
- {
- this._running = false;
- yield break;
- }
- CoError<TError> coError = coroutine.Current as CoError<TError>;
- CoResult<TResult> coResult = coroutine.Current as CoResult<TResult>;
- CoReturn coReturn = coroutine.Current as CoReturn;
- if (coError != null)
- {
- this._error = coError.Value;
- this._running = false;
- yield break;
- }
- if (coResult != null)
- {
- this._result = coResult.Value;
- this._running = false;
- yield break;
- }
- if (coReturn != null)
- {
- throw new Exception(string.Format("Type missmatch! {0} returned a wrong value {1} to {2}!", coroutine, coReturn, this));
- }
- yield return coroutine.Current;
- }
- yield break;
- }
-
- private bool _running;
-
- private bool _paused;
-
- private TError _error;
-
- private TResult _result;
-
- private IEnumerator<IYieldStatement> _routine;
-
- private ICoroutine _onComplete;
- }
- }
|