# HG changeset patch # User cin # Date 1516954778 -10800 # Node ID 5aa9cfbe56c3a87641fe3ec6864e0136753444e0 # Parent b904e0a3ba72e62c1ad4d493c6b9bc986cbf69fd missing files diff -r b904e0a3ba72 -r 5aa9cfbe56c3 Implab/Deferred`1.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implab/Deferred`1.cs Fri Jan 26 11:19:38 2018 +0300 @@ -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 -r b904e0a3ba72 -r 5aa9cfbe56c3 Implab/IResolvable`1.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implab/IResolvable`1.cs Fri Jan 26 11:19:38 2018 +0300 @@ -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 -r b904e0a3ba72 -r 5aa9cfbe56c3 Implab/PromiseReaction.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implab/PromiseReaction.cs Fri Jan 26 11:19:38 2018 +0300 @@ -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 -r b904e0a3ba72 -r 5aa9cfbe56c3 Implab/PromiseReactionJob.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implab/PromiseReactionJob.cs Fri Jan 26 11:19:38 2018 +0300 @@ -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 -r b904e0a3ba72 -r 5aa9cfbe56c3 Implab/PromiseReaction`1.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implab/PromiseReaction`1.cs Fri Jan 26 11:19:38 2018 +0300 @@ -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