public override bool MoveNext() { if (this._coroutine == null) return (false); if (this._coroutine.MoveNext()) return (true); if (this._coroutine.Exception != null) { this.AppendError(this._coroutine.Exception); this.Resume(); return (false); } if (this._coroutine.Token.IsCanceled) { this.Token.CancelError(this._coroutine.Token.CancelException); this.Resume(); return (false); } if (this._continuation != null) { this.DisposeCurrent(); this._coroutine = this._continuation(); this._continuation = null; return (true); } this.Resume(); return (false); }
public void Error02() { int x = 15; var y = x * x; var s = y.ToString(); int l = s.Length; Corout.Create <int>(co => Routines.Failing(x, co)) .OnCatch <NotSupportedException>((ex, co) => { Debug.Log("Catch error."); co.Abort(); }) .OnSucceed(co => { Debug.Log("first on succeed."); Assert.AreEqual(co.Result, y); }) .ContinueWith <string>(Routines.GetIntString) .OnSucceed(co => { Debug.Log("second on succeed should not happen."); Assert.AreEqual(co.Result, s); }) .ContinueWith <int>(Routines.GetLength) .OnSucceed(co => { Debug.Log("third on succeed should not happen."); Assert.AreEqual(co.Result, l); }) .Start(); }
public void Abort() { var step = 100; var corout = Corout.Create(Routines.NStepVerbose(step)) .OnSucceed(co => Debug.Log(string.Format("corout complete in {0} steps", step))) .OnCancel(co => Debug.Log("'OnCancel' never called")) .OnFinally(co => Debug.Log("'OnFinally'.")); corout.Start(); Corout.WaitThen(10, () => { corout.Abort(); Debug.Log("Aborted"); }); // OR /* * Corout.WaitThen(0.5f, () => * { * corout.Abort(); * Debug.Log("Aborted"); * }); */ }
public void InterlaceError() { var x = 3; var y = 4.57f; var z = 1425.67f; var xx = x * x; var yy = y * y; var zz = z.ToString(); var cw = string.Join(" ", new string[] { xx.ToString(), yy.ToString(), zz }); Corout.WhenAll <int, float, string>(ac => Routines.Failing(x, ac), ac => Routines.GetFloatSquareRoot(y, ac), ac => Routines.GetFloatString(z, ac)) .OnCatch <Exception>((ex, co) => { Debug.Log("catch error"); //throw ex; co.Abort(); }) .ContinueWith(() => Routines.NStepVerbose(10)) //.ContinueWith<string>(Routines.JoinString) //.OnComplete(co => //{ //Debug.Log(co.Result); //Assert.AreEqual(cw, co.Result); //}) .Start(); }
internal CoroutInterlace(Corout[] coroutines) { this._queues = new Queue<Corout>(); for (int i = 0; i < coroutines.Length; i++) this._queues.Enqueue(coroutines[i]); }
public IEnumerator Communicate(MonoBehaviour owner, WWWForm form, string url, Action <object> callback) { Corout connexion = new Corout(owner, www(url, form)); yield return(connexion.coroutine); callback(connexion.result); }
public void Simple() { var x = 7.525f; var y = x * x; Corout.Create <float>(co => Routines.GetFloatSquareRoot(x, co)) .OnSucceed(co => { Assert.AreEqual(co.Result, y); Assert.IsTrue(co.Succeeded); }) .Start(); }
public IEnumerator Communicate(MonoBehaviour owner, string url, Action <object> callback) { Corout connexion = new Corout(owner, www(url)); yield return(connexion.coroutine); string result = null; if (www(url) != www(url)) { result = "Failed to load resource: the server responded with a status of 404 (Not Found)"; } callback(connexion.result); }
public void ContinueSimple() { var x = 7.525f; var y = x * x; var s = y.ToString(); int l = s.Length; Corout.Create <float>(co => Routines.GetFloatSquareRoot(x, co)) .OnSucceed(co => Assert.AreEqual(co.Result, y)) .ContinueWith <string>(Routines.GetFloatString) .OnSucceed(co => Assert.AreEqual(co.Result, s)) .ContinueWith <int>(Routines.GetLength) .OnSucceed(co => Assert.AreEqual(co.Result, l)) .Start(); }
public void StepAndSecond() { int startStep = Scheduler.Instance.Step; int step = 6; Corout.Create(Routines.NStep(step)) .OnSucceed(co => Assert.AreEqual(Scheduler.Instance.Step, startStep + step)) .Start(); float startSecond = Scheduler.Instance.Second; float second = 0.5f; Corout.Create(Routines.NSecond(second)) .OnSucceed(co => Assert.GreaterOrEqual(Scheduler.Instance.Second, startSecond + second)) .Start(); }
public void CatchThrowFinally() { var x = 42; var y = x * x; Corout.Create <int>(co => Routines.Failing(x, co)) .OnCatch <NotSupportedException>((ex, co) => { Assert.IsInstanceOf <NotSupportedException>(ex); Debug.Log("Catch: 'NotSupportedException'."); }) .OnFinally(co => { Assert.IsTrue(co.Faulted); Debug.Log("Finally do some clean-up..."); }) .Start(); Corout.Create <int>(co => Routines.GetIntSquareRoot(x, co)) .OnFinally(co => { Debug.Log("'OnFinally' doesn't need 'OnCatch' and are always executed."); Assert.AreEqual(co.Result, y); Assert.IsTrue(co.Succeeded); }) .Start(); Corout.Create <int>(co => Routines.Failing(x, co)) .OnCatch <NotSupportedException>((ex, co) => { Assert.IsInstanceOf <NotSupportedException>(ex); Assert.IsTrue(co.Faulted); throw ex; }) .OnFinally(co => { Assert.IsTrue(co.Faulted); Debug.Log("Even if you throw exception 'OnFinally' is executed."); }) .Start(); }
public IEnumerator Communicate(MonoBehaviour owner, string url, Action <object> callback) { Corout connexion = new Corout(owner, www(url)); yield return(connexion.coroutine); bool error404 = Regex.IsMatch(connexion.result.ToString(), "Error 404"); if (error404) { callback("Failed to load resource: the server responded with a status of 404 (Not Found)"); } else if (!(connexion.result is string)) { callback("Connexion failed..."); } else { callback(connexion.result); } }
public void Interlace() { var x = 3; var y = 4.57f; var z = 1425.67f; var xx = x * x; var yy = y * y; var zz = z.ToString(); var cw = string.Join(" ", new string[] { xx.ToString(), yy.ToString(), zz }); Corout.WhenAll(() => Routines.NStepVerbose(5), () => Routines.NStepVerbose(10), () => Routines.NStepVerbose(15)) .OnSucceed(co => { Debug.Log("OnSucceed"); }) .Start(); Corout.WhenAll <int, float, string>(co => Routines.GetIntSquareRoot(x, co), co => Routines.GetFloatSquareRoot(y, co), co => Routines.GetFloatString(z, co)) .OnSucceed(co => { Debug.Log(co.Result.Item1); Debug.Log(co.Result.Item2); Debug.Log(co.Result.Item3); }) .ContinueWith <string>(Routines.JoinString) .OnSucceed(co => { Debug.Log(co.Result); Assert.AreEqual(cw, co.Result); }) .Start(); }
public void Cancel() { var x = 7.525f; Corout.Create <float>((co, tk) => Routines.Cancelable(x, co, tk)) .OnSucceed(co => Debug.Log("OnSucceed.")) .OnCancel(co => Debug.Log("Corout canceled.")) .Start(); var second = 1.15f; Corout.Create <float>((co, tk) => Routines.CancelableAfter(second, x, co, tk)) .OnCancel(co => Debug.Log(string.Format("Corout canceled after {0} second", second))) .OnSucceed(co => Debug.Log("OnSucceed.")) .Start(); var step = 15; Corout.Create <float>((co, tk) => Routines.CancelableAfter(step, x, co, tk)) .OnSucceed(co => Debug.Log("'OnSucceed' never call.")) .OnCancel(co => Debug.Log(string.Format("Corout canceled after {0} step", step))) .Start(); }
/// <summary></summary> /// <param name="coroutine"></param> public void Append(Corout coroutine) { if (coroutine == null) throw new ArgumentNullException("coroutine"); if (this._runningCorouts.Contains(coroutine)) throw new Exception("coroutine already scheduled."); if (coroutine.IsSynchronous) { this._runningCorouts.Insert(0, coroutine); this.Play(); this.Run(); } else { switch (coroutine.AsyncMode) { case EAsyncMode.Priorize: this._runningCorouts.Insert(0, coroutine); this.Play(); break; case EAsyncMode.Deferize: this._runningCorouts.Add(coroutine); this.Play(); break; case EAsyncMode.Normal: default: this._runningCorouts.Add(coroutine); this.Play(); break; } } }
internal CoroutRelay(Corout coroutine, Func<Corout> continuation) { this._coroutine = coroutine; this._continuation = continuation; }
/// <summary></summary> /// <param name="coroutine"></param> public bool Contains(Corout coroutine) { if (coroutine == null) throw new ArgumentNullException("coroutine"); if (this._runningCorouts != null) return (this._runningCorouts.Contains(coroutine)); return (false); }
public void Fence() { var first = 10; var second = 05; var third = 03; int startStep = Scheduler.Instance.Step; using (Corout.OpenFence(() => Debug.Log("fenced corout ends..."))) { Corout.Create(() => Routines.NStepVerbose(first)) .OnSucceed(co => { Debug.Log(string.Format("first corout complete in {0} steps", first)); Assert.AreEqual(Scheduler.Instance.Step, startStep + first); }) .Start(); Corout.Create(() => Routines.NStepVerbose(second)) .OnSucceed(co => { Debug.Log(string.Format("second corout Complete in {0} steps", second)); Assert.AreEqual(Scheduler.Instance.Step, startStep + second); }) .Start(); } Corout.Create(() => Routines.NStepVerbose(third)) .OnSucceed(co => { Debug.Log(string.Format("third corout complete in {0} steps", third)); Assert.AreEqual(Scheduler.Instance.Step, startStep + Math.Max(first, second) + third + 1); // 1 step for fence callback }) .Start(); // OR /* * var fence01 = Corout.OpenFence(() => Debug.Log("fenced corout ends...")); * * Corout.Create(() => Routines.NStepVerbose(first)) * .OnComplete(co => * { * Debug.Log(string.Format("first corout complete in {0} steps", first)); * Assert.AreEqual(Corout.CurrentStep, startStep + first); * }) * .Start(); * * Corout.Create(() => Routines.NStepVerbose(second)) * .OnComplete(co => * { * Debug.Log(string.Format("second corout Complete in {0} steps", second)); * Assert.AreEqual(Corout.CurrentStep, startStep + second); * }) * .Start(); * * fence01.Close(); * * Corout.Create(() => Routines.NStepVerbose(third)) * .OnComplete(co => * { * Debug.Log(string.Format("third corout complete in {0} steps", third)); * Assert.AreEqual(Corout.CurrentStep, startStep + Math.Max(first, second) + third + 1); // 1 step for fence callback * }) * .Start(); */ }