# HG changeset patch # User cin # Date 2018-01-26 08:19:38 # Node ID 5aa9cfbe56c3a87641fe3ec6864e0136753444e0 # Parent b904e0a3ba72e62c1ad4d493c6b9bc986cbf69fd missing files diff --git a/Implab/Deferred`1.cs b/Implab/Deferred`1.cs new file mode 100644 --- /dev/null +++ b/Implab/Deferred`1.cs @@ -0,0 +1,48 @@ +using System; +using System.Diagnostics; + +namespace Implab { + public class Deferred : IResolvable { + readonly AbstractPromise m_promise; + readonly IDispatcher m_dispatcher; + + internal Deferred(AbstractPromise promise, IDispatcher dispatcher) { + Debug.Assert(promise != null); + m_promise = promise; + m_dispatcher = dispatcher; + } + + public IPromise Promise { + get { return m_promise; } + } + + public void Reject(Exception error) { + m_promise.Reject(error); + } + + public void Resolve(T value) { + m_promise.Resolve(value); + } + + public void Resolve(IPromise thenable) { + if (thenable == null) + Reject(new Exception("The promise or task are expected")); + if (thenable == m_promise) + Reject(new Exception("The promise cannot be resolved with oneself")); + + else if (m_dispatcher != null) + // dispatch (see ecma-262/6.0: 25.4.1.3.2 Promise Resolve Functions) + m_dispatcher.Enqueue(() => Chain(thenable)); + else + Chain(thenable); + } + + void Chain(IPromise thenable) { + try { + thenable.Then(this); + } catch (Exception err) { + Reject(err); + } + } + } +} \ No newline at end of file diff --git a/Implab/IResolvable`1.cs b/Implab/IResolvable`1.cs new file mode 100644 --- /dev/null +++ b/Implab/IResolvable`1.cs @@ -0,0 +1,11 @@ +using System; + +namespace Implab { + + public interface IResolvable { + void Resolve(T value); + + void Reject(Exception reason); + + } +} \ No newline at end of file diff --git a/Implab/PromiseReaction.cs b/Implab/PromiseReaction.cs new file mode 100644 --- /dev/null +++ b/Implab/PromiseReaction.cs @@ -0,0 +1,35 @@ +using System; + +namespace Implab { + public class PromiseReaction : IResolvable { + IDispatcher m_dispatcher; + + Action m_onFulfilledJob; + + Action m_onRejectedJob; + + IResolvable m_next; + + public void Reject(Exception error) { + if (m_onRejectedJob != null) { + if (m_dispatcher != null) + m_dispatcher.Enqueue(() => m_onRejectedJob(error)); + else + m_onRejectedJob(error); + } else { + m_next.Reject(error); + } + } + + public void Resolve() { + if (m_onRejectedJob != null) { + if (m_dispatcher != null) + m_dispatcher.Enqueue( m_onFulfilledJob); + else + m_onFulfilledJob(); + } else { + m_next.Resolve(); + } + } + } +} \ No newline at end of file diff --git a/Implab/PromiseReactionJob.cs b/Implab/PromiseReactionJob.cs new file mode 100644 --- /dev/null +++ b/Implab/PromiseReactionJob.cs @@ -0,0 +1,103 @@ +using System; +using System.Diagnostics; + +namespace Implab +{ + class PromiseReactionJob { + public static Action Create(Action handler, Deferred next) { + Debug.Assert(handler != null); + + return (v) => { + try { + handler(v); + next.Resolve(); + } catch(Exception err) { + next.Reject(err); + } + }; + } + + public static Action Create(Func handler, Deferred next) { + Debug.Assert(handler != null); + + return (v) => { + try { + next.Resolve(handler(v)); + } catch(Exception err) { + next.Reject(err); + } + }; + } + + public static Action Create(Func handler, Deferred next) { + Debug.Assert(handler != null); + + return (v) => { + try { + next.Resolve(handler(v)); + } catch(Exception err) { + next.Reject(err); + } + }; + } + + public static Action Create(Func> handler, Deferred next) { + Debug.Assert(handler != null); + return (v) => { + try { + next.Resolve(handler(v)); + } catch(Exception err) { + next.Reject(err); + } + }; + } + + public static Action Create(Action handler, Deferred next) { + Debug.Assert(handler != null); + + return () => { + try { + handler(); + next.Resolve(); + } catch(Exception err) { + next.Reject(err); + } + }; + } + + public static Action Create(Func handler, Deferred next) { + Debug.Assert(handler != null); + + return () => { + try { + next.Resolve(handler()); + } catch(Exception err) { + next.Reject(err); + } + }; + } + + public static Action Create(Func handler, Deferred next) { + Debug.Assert(handler != null); + + return () => { + try { + next.Resolve(handler()); + } catch(Exception err) { + next.Reject(err); + } + }; + } + + public static Action Create(Func> handler, Deferred next) { + Debug.Assert(handler != null); + return () => { + try { + next.Resolve(handler()); + } catch(Exception err) { + next.Reject(err); + } + }; + } + } +} \ No newline at end of file diff --git a/Implab/PromiseReaction`1.cs b/Implab/PromiseReaction`1.cs new file mode 100644 --- /dev/null +++ b/Implab/PromiseReaction`1.cs @@ -0,0 +1,25 @@ +using System; + +namespace Implab { + public class PromiseReaction : IResolvable { + IDispatcher m_dispatcher; + + Action m_onFulfilledJob; + + Action m_onRejectedJob; + + public void Reject(Exception error) { + if (m_dispatcher != null) + m_dispatcher.Enqueue(() => m_onRejectedJob(error)); + else + m_onRejectedJob(error); + } + + public void Resolve(T result) { + if (m_dispatcher != null) + m_dispatcher.Enqueue(() => m_onFulfilledJob(result)); + else + m_onFulfilledJob(result); + } + } +} \ No newline at end of file