Mercurial > pub > ImplabNet
changeset 246:5aa9cfbe56c3 v3
missing files
author | cin |
---|---|
date | Fri, 26 Jan 2018 11:19:38 +0300 |
parents | b904e0a3ba72 |
children | fb70574741a1 |
files | Implab/Deferred`1.cs Implab/IResolvable`1.cs Implab/PromiseReaction.cs Implab/PromiseReactionJob.cs Implab/PromiseReaction`1.cs |
diffstat | 5 files changed, 222 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /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<T> : IResolvable<T> { + readonly AbstractPromise<T> m_promise; + readonly IDispatcher m_dispatcher; + + internal Deferred(AbstractPromise<T> promise, IDispatcher dispatcher) { + Debug.Assert(promise != null); + m_promise = promise; + m_dispatcher = dispatcher; + } + + public IPromise<T> 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<T> 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<T> thenable) { + try { + thenable.Then(this); + } catch (Exception err) { + Reject(err); + } + } + } +} \ No newline at end of file
--- /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<T> { + void Resolve(T value); + + void Reject(Exception reason); + + } +} \ No newline at end of file
--- /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<Exception> 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
--- /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<T> Create<T>(Action<T> handler, Deferred next) { + Debug.Assert(handler != null); + + return (v) => { + try { + handler(v); + next.Resolve(); + } catch(Exception err) { + next.Reject(err); + } + }; + } + + public static Action<T> Create<T>(Func<T, IPromise> handler, Deferred next) { + Debug.Assert(handler != null); + + return (v) => { + try { + next.Resolve(handler(v)); + } catch(Exception err) { + next.Reject(err); + } + }; + } + + public static Action<T> Create<T,T2>(Func<T, T2> handler, Deferred<T2> next) { + Debug.Assert(handler != null); + + return (v) => { + try { + next.Resolve(handler(v)); + } catch(Exception err) { + next.Reject(err); + } + }; + } + + public static Action<T> Create<T,T2>(Func<T, IPromise<T2>> handler, Deferred<T2> 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<IPromise> handler, Deferred next) { + Debug.Assert(handler != null); + + return () => { + try { + next.Resolve(handler()); + } catch(Exception err) { + next.Reject(err); + } + }; + } + + public static Action Create<T2>(Func<T2> handler, Deferred<T2> next) { + Debug.Assert(handler != null); + + return () => { + try { + next.Resolve(handler()); + } catch(Exception err) { + next.Reject(err); + } + }; + } + + public static Action Create<T2>(Func<IPromise<T2>> handler, Deferred<T2> next) { + Debug.Assert(handler != null); + return () => { + try { + next.Resolve(handler()); + } catch(Exception err) { + next.Reject(err); + } + }; + } + } +} \ No newline at end of file
--- /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<T> : IResolvable<T> { + IDispatcher m_dispatcher; + + Action<T> m_onFulfilledJob; + + Action<Exception> 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