Last active
September 12, 2017 12:42
-
-
Save lonewolfwilliams/13cbf8bbb628d698eb38c625318b531a to your computer and use it in GitHub Desktop.
This file contains 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 UnityEngine; | |
using System.Collections; | |
using System; | |
using System.Collections.Generic; | |
/// <summary> | |
/// Gareth Williams - Lonewolfwilliams LTD | |
/// http://lww.io | |
/// </summary> | |
public class Promises : MonoBehaviour | |
{ | |
// Use this for initialization | |
IEnumerator Start () | |
{ | |
var result = new Promise((f,r)=> | |
{ | |
Debug.Log("one"); | |
StartCoroutine(DelayBySecs(1, f, "first")); | |
}) | |
.Then((f,r)=> | |
{ | |
Debug.Log("two"); | |
StartCoroutine(DelayBySecs(1, r)); | |
}) | |
.Then((f,r)=> | |
{ | |
Debug.Log("three"); | |
StartCoroutine(DelayBySecs(1, f, "third")); | |
}) | |
.Resolve(); | |
while(result.m_pending) yield return null; | |
for(int i=0; i<result.value.Length; i++) Debug.Log(result.value[i]); | |
} | |
private IEnumerator DelayBySecs<T>(float secs, Action<T> cc, T x) | |
{ | |
yield return new WaitForSeconds(secs); | |
cc.Invoke(x); | |
} | |
private IEnumerator DelayBySecs(float secs, Action cc) | |
{ | |
yield return new WaitForSeconds(secs); | |
cc.Invoke(); | |
} | |
} | |
public class Promise | |
{ | |
public Promise m_prev; | |
public bool m_pending; | |
public Action<object> m_fulfill; | |
public Action m_reject; | |
//public ITuple value = new Tuple(); | |
public object[] value = new object[0]; | |
public Action<Action<object>, Action> m_resolution; | |
public Promise(Action<Action<object>, Action> resolution) | |
{ | |
m_pending = true; | |
this.m_resolution = resolution; | |
} | |
public Promise Resolve() | |
{ | |
Promise final = null; | |
final = this.Then((f,r)=> | |
{ | |
Debug.Log("done"); | |
final.m_pending = false; | |
}); | |
Promise first = final.First(); | |
first.m_resolution.Invoke(first.m_fulfill, first.m_reject); | |
return final; | |
} | |
public Promise First() | |
{ | |
Promise first = this; | |
while(first.m_prev != null) first = first.m_prev; | |
return first; | |
} | |
} | |
public static class Extensions | |
{ | |
public static Promise Then(this Promise prev, Action<Action<object>, Action> resolution) | |
{ | |
Promise current = new Promise(resolution); | |
prev.m_fulfill = (x) => | |
{ | |
prev.m_pending = false; | |
List<object> concat = new List<object>(prev.value); | |
concat.Add(x); | |
current.value = concat.ToArray(); | |
foreach(var s in current.value) Debug.Log("->"+s); | |
current.m_resolution.Invoke(current.m_fulfill, current.m_reject); | |
}; | |
prev.m_reject = () => | |
{ | |
current.value = prev.value; | |
Debug.LogError("promise rejected"); | |
current.m_resolution.Invoke(current.m_fulfill, current.m_reject); | |
}; | |
//link list | |
current.m_prev = prev; | |
return current; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Beautiful!