Last active
February 5, 2019 10:44
-
-
Save lonewolfwilliams/bf3f2390f15cab478158e8efd77518cc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections; | |
| using System.Collections.Generic; | |
| using UnityEngine; | |
| public class Observable:MonoBehaviour | |
| { | |
| //stub... | |
| } | |
| public interface ITuple //like a maybe | |
| { | |
| ITuple Add<T>(T item); | |
| } | |
| public class Tuple : ITuple //like a nothing | |
| { | |
| public ITuple Add<T>(T item) | |
| { | |
| return new Tuple<T>(item); | |
| } | |
| } | |
| public class Tuple<A> : ITuple //like a something | |
| { | |
| public A itemOne; | |
| public Tuple(A item) | |
| { | |
| itemOne = item; | |
| } | |
| public ITuple Add<B>(B item) | |
| { | |
| return new Tuple<A,B>(itemOne, item); | |
| } | |
| } | |
| public class Tuple<A,B> : ITuple | |
| { | |
| public A itemOne; | |
| public B itemTwo; | |
| public Tuple(A itemOne, B itemTwo) | |
| { | |
| this.itemOne = itemOne; | |
| this.itemTwo = itemTwo; | |
| } | |
| public ITuple Add<C>(C item) | |
| { | |
| return new Tuple<A,B,C>(itemOne,itemTwo,item); | |
| } | |
| } | |
| public class Tuple<A,B,C> : ITuple | |
| { | |
| public A itemOne; | |
| public B itemTwo; | |
| public C itemThree; | |
| public Tuple(A itemOne, B itemTwo, C itemThree) | |
| { | |
| this.itemOne = itemOne; | |
| this.itemTwo = itemTwo; | |
| this.itemThree = itemThree; | |
| } | |
| public ITuple Add<D>(D item) | |
| { | |
| return new Tuple<A,B,C,D>(itemOne,itemTwo,itemThree,item); | |
| } | |
| } | |
| public class Tuple<A,B,C,D> : ITuple | |
| { | |
| public A itemOne; | |
| public B itemTwo; | |
| public C itemThree; | |
| public D itemFour; | |
| public Tuple(A itemOne, B itemTwo, C itemThree, D itemFour) | |
| { | |
| this.itemOne = itemOne; | |
| this.itemTwo = itemTwo; | |
| this.itemThree = itemThree; | |
| } | |
| public ITuple Add<E>(E item) | |
| { | |
| return this; | |
| } | |
| } | |
| public class TestPromise : MonoBehaviour | |
| { | |
| void Start() | |
| { | |
| new CoroutineSequence(First).Then(Second).StartCoroutine(OnComplete); | |
| } | |
| private IEnumerator First(ITuple t, Action<ITuple> cc) | |
| { | |
| yield return new WaitForSeconds(1.0f); | |
| cc.Invoke(t.Add("first")); | |
| } | |
| private IEnumerator Second(ITuple t, Action<ITuple> cc) | |
| { | |
| yield return new WaitForSeconds(1.0f); | |
| cc.Invoke(t.Add("second")); | |
| } | |
| private void OnComplete(ITuple t) | |
| { | |
| var result = (t as Tuple<string,string>); | |
| Debug.LogFormat("{0} {1}", result.itemOne, result.itemTwo); | |
| } | |
| } | |
| public class CoroutineSequence | |
| { | |
| private CoroutineSequence next; | |
| private CoroutineSequence prev; | |
| private Func<ITuple, Action<ITuple>, IEnumerator> cr; | |
| private Observable o; | |
| public CoroutineSequence(Func<ITuple, Action<ITuple>, IEnumerator> cr) | |
| { | |
| this.o = new GameObject("[Promise]").AddComponent<Observable>(); | |
| this.cr = cr; | |
| } | |
| public void StartCoroutine(Action<ITuple> cc) | |
| { | |
| var first = this; | |
| while(first.prev != null) first = prev; | |
| first.resolve(cc, new Tuple()); | |
| } | |
| public CoroutineSequence Then(Func<ITuple, Action<ITuple>, IEnumerator> cr) | |
| { | |
| next = new CoroutineSequence(cr); | |
| next.prev = this; | |
| return this; | |
| } | |
| private void resolve(Action<ITuple> cc, ITuple result) | |
| { | |
| var en = cr.Invoke(result, (_t)=> | |
| { | |
| if(next != null) next.resolve(cc, _t); | |
| else cc.Invoke(_t); | |
| GameObject.Destroy(o.gameObject); | |
| }); | |
| o.StartCoroutine(en); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment