##// END OF EJS Templates
Implab.Test moved to xunit...
cin -
r249:d82909310094 v3
parent child
Show More
@@ -0,0 +1,17
1 using System;
2 using System.Threading;
3 using Xunit;
4
5 namespace Implab.Test
6 {
7 public class UnitTest1
8 {
9 [Fact]
10 public void Test1()
11 {
12 using(var cts = new CancellationTokenSource(1000)) {
13 PromiseHelper.Sleep(10000, cts.Token).Join();
14 }
15 }
16 }
17 }
@@ -1,88 +1,16
1 <?xml version="1.0" encoding="utf-8"?>
2 <Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
1 <Project Sdk="Microsoft.NET.Sdk">
2
3 3 <PropertyGroup>
4 <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5 <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6 <ProductVersion>8.0.30703</ProductVersion>
7 <SchemaVersion>2.0</SchemaVersion>
8 <ProjectGuid>{63F92C0C-61BF-48C0-A377-8D67C3C661D0}</ProjectGuid>
9 <OutputType>Library</OutputType>
10 <AppDesignerFolder>Properties</AppDesignerFolder>
11 <RootNamespace>Implab.Test</RootNamespace>
12 <AssemblyName>Implab.Test</AssemblyName>
13 <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
14 <FileAlignment>512</FileAlignment>
15 <ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
16 <TargetFrameworkProfile />
17 </PropertyGroup>
18 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
19 <DebugSymbols>true</DebugSymbols>
20 <DebugType>full</DebugType>
21 <Optimize>false</Optimize>
22 <OutputPath>bin\Debug\</OutputPath>
23 <DefineConstants>DEBUG;TRACE</DefineConstants>
24 <ErrorReport>prompt</ErrorReport>
25 <WarningLevel>4</WarningLevel>
26 <Prefer32Bit>false</Prefer32Bit>
27 </PropertyGroup>
28 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
29 <DebugType>pdbonly</DebugType>
30 <Optimize>true</Optimize>
31 <OutputPath>bin\Release\</OutputPath>
32 <DefineConstants>TRACE</DefineConstants>
33 <ErrorReport>prompt</ErrorReport>
34 <WarningLevel>4</WarningLevel>
35 <Prefer32Bit>false</Prefer32Bit>
36 </PropertyGroup>
37 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug 4.5|AnyCPU' ">
38 <DebugSymbols>true</DebugSymbols>
39 <DebugType>full</DebugType>
40 <Optimize>false</Optimize>
41 <OutputPath>bin\Debug\</OutputPath>
42 <DefineConstants>DEBUG;TRACE</DefineConstants>
43 <ErrorReport>prompt</ErrorReport>
44 <WarningLevel>4</WarningLevel>
45 <Prefer32Bit>false</Prefer32Bit>
46 </PropertyGroup>
47 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release 4.5|AnyCPU' ">
48 <DebugType>pdbonly</DebugType>
49 <Optimize>true</Optimize>
50 <OutputPath>bin\Release\</OutputPath>
51 <DefineConstants>TRACE</DefineConstants>
52 <ErrorReport>prompt</ErrorReport>
53 <WarningLevel>4</WarningLevel>
54 <Prefer32Bit>false</Prefer32Bit>
4 <TargetFramework>netcoreapp2.0</TargetFramework>
5
6 <IsPackable>false</IsPackable>
55 7 </PropertyGroup>
56 8 <ItemGroup>
57 <Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
58 <Reference Include="System" />
59 <Reference Include="System.Core">
60 <RequiredTargetFramework>3.5</RequiredTargetFramework>
61 </Reference>
62 </ItemGroup>
63 <ItemGroup>
64 <Compile Include="AsyncTests.cs" />
65 <Compile Include="CancelationTests.cs" />
66 <Compile Include="Mock\MockPollingComponent.cs" />
67 <Compile Include="Mock\MockRunnableComponent.cs" />
68 <Compile Include="PollingComponentTests.cs" />
69 <Compile Include="PromiseHelper.cs" />
70 <Compile Include="Properties\AssemblyInfo.cs" />
71 <Compile Include="RunnableComponentTests.cs" />
9 <ProjectReference Include="../Implab/Implab.csproj"/>
10 <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
11 <PackageReference Include="xunit" Version="2.3.1" />
12 <PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
13 <DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
72 14 </ItemGroup>
73 <ItemGroup>
74 <ProjectReference Include="..\Implab\Implab.csproj">
75 <Project>{99B95D0D-9CF9-4F70-8ADF-F4D0AA5CB0D9}</Project>
76 <Name>Implab</Name>
77 </ProjectReference>
78 </ItemGroup>
79 <ItemGroup />
80 <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
81 <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
82 Other similar extension points exist, see Microsoft.Common.targets.
83 <Target Name="BeforeBuild">
84 </Target>
85 <Target Name="AfterBuild">
86 </Target>
87 -->
88 </Project> No newline at end of file
15
16 </Project>
@@ -1,22 +1,40
1 using Implab.Parallels;
1 using Implab;
2 using System;
2 3 using System.Threading;
3 4
4 5 namespace Implab.Test {
5 6 static class PromiseHelper {
6 public static IPromise<T> Sleep<T>(int timeout, T retVal) {
7 return AsyncPool.Invoke((ct) => {
8 ct.CancellationRequested(ct.CancelOperation);
9 Thread.Sleep(timeout);
10 return retVal;
7 public static IPromise<T> Sleep<T>(int timeout, T retVal, CancellationToken ct = default(CancellationToken)) {
8
9 Timer timer = null;
10
11 return Promise.Create<T>((d) => {
12 timer = new Timer(x => {
13 d.Resolve(retVal);
14 }, null, timeout, Timeout.Infinite);
15
16 if(ct.CanBeCanceled)
17 ct.Register(d.Cancel);
18
19 }).Finally(() => {
20 Safe.Dispose(timer);
11 21 });
12 22 }
13 23
14 public static IPromise Sleep(int timeout) {
15 return AsyncPool.Invoke((ct) => {
16 ct.CancellationRequested(ct.CancelOperation);
17 Thread.Sleep(timeout);
18 return 0;
24 public static IPromise Sleep(int timeout, CancellationToken ct = default(CancellationToken)) {
25 Timer timer = null;
26
27 return Promise.Create((d) => {
28 timer = new Timer(x => {
29 d.Resolve();
30 }, null, timeout, Timeout.Infinite);
31
32 if(ct.CanBeCanceled)
33 ct.Register(d.Cancel);
34
35 }).Finally(() => {
36 Safe.Dispose(timer);
19 37 });
20 38 }
21 39 }
22 40 }
@@ -1,58 +1,53
1 1 using System;
2 2 using System.Diagnostics;
3 3
4 4 namespace Implab {
5 5 /// <summary>
6 6 /// This class is responsible for the promise resolution, dispatching and chaining
7 7 /// </summary>
8 8 public class Deferred : IResolvable {
9 9
10 10 readonly Promise m_promise;
11 readonly IDispatcher m_dispatcher;
12
13 internal Deferred(IDispatcher dispatcher) : this(new Promise(), dispatcher) {
11 internal Deferred() {
12 m_promise = new Promise();
14 13 }
15 14
16 15 internal Deferred(Promise promise, IDispatcher dispatcher) {
17 16 Debug.Assert(promise != null);
18 17 m_promise = promise;
19 m_dispatcher = dispatcher;
20 18 }
21 19
22 20 public IPromise Promise {
23 21 get { return m_promise; }
24 22 }
25 23
26 public void Reject(Exception error) {
24 public void Cancel() {
25 Reject(new OperationCanceledException());
26 }
27
28 public virtual void Reject(Exception error) {
27 29 if (error is PromiseTransientException)
28 30 error = ((PromiseTransientException)error).InnerException;
29 31
30 32 m_promise.RejectPromise(error);
31 33 }
32 34
33 public void Resolve() {
35 public virtual void Resolve() {
34 36 m_promise.ResolvePromise();
35 37 }
36 38
37 public void Resolve(IPromise thenable) {
39 public virtual void Resolve(IPromise thenable) {
38 40 if (thenable == null)
39 41 Reject(new Exception("The promise or task are expected"));
40 42 if (thenable == m_promise)
41 43 Reject(new Exception("The promise cannot be resolved with oneself"));
42 44
43 else if (m_dispatcher != null)
44 // dispatch (see ecma-262/6.0: 25.4.1.3.2 Promise Resolve Functions)
45 m_dispatcher.Enqueue(Chain, thenable);
46 else
47 Chain(thenable);
48 }
49
50 void Chain(IPromise thenable) {
51 45 try {
52 46 thenable.Then(this);
53 47 } catch (Exception err) {
54 48 Reject(err);
55 49 }
56 50 }
51
57 52 }
58 53 } No newline at end of file
@@ -1,54 +1,49
1 1 using System;
2 2 using System.Diagnostics;
3 3
4 4 namespace Implab {
5 5 public class Deferred<T> : IResolvable<T> {
6 6 readonly Promise<T> m_promise;
7 readonly IDispatcher m_dispatcher;
8 7
9 internal Deferred(IDispatcher dispatcher) : this(new Promise<T>(), dispatcher) {
8 internal Deferred() {
9 m_promise = new Promise<T>();
10 10 }
11 11
12 internal Deferred(Promise<T> promise, IDispatcher dispatcher) {
12 protected Deferred(Promise<T> promise) {
13 13 Debug.Assert(promise != null);
14 14 m_promise = promise;
15 m_dispatcher = dispatcher;
16 15 }
17 16
18 17 public IPromise<T> Promise {
19 18 get { return m_promise; }
20 19 }
21 20
22 public void Reject(Exception error) {
21 public void Cancel() {
22 Reject(new OperationCanceledException());
23 }
24
25 public virtual void Reject(Exception error) {
23 26 if (error is PromiseTransientException)
24 27 error = ((PromiseTransientException)error).InnerException;
25 28
26 29 m_promise.RejectPromise(error);
27 30 }
28 31
29 public void Resolve(T value) {
32 public virtual void Resolve(T value) {
30 33 m_promise.ResolvePromise(value);
31 34 }
32 35
33 public void Resolve(IPromise<T> thenable) {
36 public virtual void Resolve(IPromise<T> thenable) {
34 37 if (thenable == null)
35 38 Reject(new Exception("The promise or task are expected"));
36 39 if (thenable == m_promise)
37 40 Reject(new Exception("The promise cannot be resolved with oneself"));
38 41
39 else if (m_dispatcher != null)
40 // dispatch (see ecma-262/6.0: 25.4.1.3.2 Promise Resolve Functions)
41 m_dispatcher.Enqueue(Chain, thenable);
42 else
43 Chain(thenable);
44 }
45
46 void Chain(IPromise<T> thenable) {
47 42 try {
48 43 thenable.Then(this);
49 44 } catch (Exception err) {
50 45 Reject(err);
51 46 }
52 47 }
53 48 }
54 49 } No newline at end of file
@@ -1,18 +1,21
1 1 using System;
2 2 using System.Reflection;
3 3 using System.Runtime.ExceptionServices;
4 4
5 5 namespace Implab {
6 public static class ExceptionHelpers {
7 public static void Rethrow(this Exception that) {
6 static class ExceptionHelpers {
7 public static Exception Rethrow(this Exception that) {
8 8 ExceptionDispatchInfo.Capture(that).Throw();
9 return new TargetInvocationException(that);
9 10 }
10 11
11 public static void ThrowInvocationException(this Exception that) {
12 if (that is OperationCanceledException)
13 throw new OperationCanceledException("Operation cancelled", that);
12 public static Exception Wrap(this Exception that) {
13 if (that == null)
14 return new Exception();
15 else if (that is OperationCanceledException)
16 return new OperationCanceledException("The operation has been cancelled", that);
14 17 else
15 throw new TargetInvocationException(that);
18 return new TargetInvocationException(that);
16 19 }
17 20 }
18 21 } No newline at end of file
@@ -1,209 +1,208
1 1 using System;
2 2 using System.Collections.Generic;
3 3 using System.Diagnostics;
4 4 using System.Reflection;
5 5 using System.Threading.Tasks;
6 6 using Implab.Parallels;
7 7
8 8 namespace Implab {
9 9 public class Promise : AbstractEvent<IResolvable>, IPromise {
10 10 public static IDispatcher DefaultDispatcher {
11 11 get {
12 12 return ThreadPoolDispatcher.Instance;
13 13 }
14 14 }
15 15
16 16 class ResolvableSignal : IResolvable {
17 17 public Signal Signal { get; private set; }
18 18 public ResolvableSignal() {
19 19 Signal = new Signal();
20 20 }
21 21
22 22
23 23 public void Reject(Exception error) {
24 24 Signal.Set();
25 25 }
26 26
27 27 public void Resolve() {
28 28 Signal.Set();
29 29 }
30 30 }
31 31
32 32 PromiseState m_state;
33 33
34 34 Exception m_error;
35 35
36 36 public bool IsRejected {
37 37 get {
38 38 return m_state == PromiseState.Rejected;
39 39 }
40 40 }
41 41
42 42 public bool IsFulfilled {
43 43 get {
44 44 return m_state == PromiseState.Fulfilled;
45 45 }
46 46 }
47 47
48 48 public Exception RejectReason {
49 49 get {
50 50 return m_error;
51 51 }
52 52 }
53 53
54 54 internal Promise() {
55 55
56 56 }
57 57
58 58 internal void ResolvePromise() {
59 59 if (BeginTransit()) {
60 60 m_state = PromiseState.Fulfilled;
61 61 CompleteTransit();
62 62 }
63 63 }
64 64
65 65 internal void RejectPromise(Exception reason) {
66 66 if (BeginTransit()) {
67 67 m_error = reason;
68 68 m_state = PromiseState.Rejected;
69 69 CompleteTransit();
70 70 }
71 71 }
72 72
73 73
74 74 #region implemented abstract members of AbstractPromise
75 75
76 76 protected override void SignalHandler(IResolvable handler) {
77 77 switch (m_state) {
78 78 case PromiseState.Fulfilled:
79 79 handler.Resolve();
80 80 break;
81 81 case PromiseState.Rejected:
82 82 handler.Reject(RejectReason);
83 83 break;
84 84 default:
85 85 throw new InvalidOperationException(String.Format("Invalid promise signal: {0}", m_state));
86 86 }
87 87 }
88 88
89 89 protected void WaitResult(int timeout) {
90 90 if (!(IsResolved || GetFulfillSignal().Wait(timeout)))
91 91 throw new TimeoutException();
92 92 }
93 93
94 94 protected Signal GetFulfillSignal() {
95 95 var next = new ResolvableSignal();
96 96 Then(next);
97 97 return next.Signal;
98 98 }
99 99
100 100 #endregion
101 101
102 102
103 103 public Type ResultType {
104 104 get {
105 105 return typeof(void);
106 106 }
107 107 }
108 108
109 109
110 110 protected void Rethrow() {
111 111 Debug.Assert(m_error != null);
112 112 if (m_error is OperationCanceledException)
113 113 throw new OperationCanceledException("Operation cancelled", m_error);
114 114 else
115 115 throw new TargetInvocationException(m_error);
116 116 }
117 117
118 118 public void Then(IResolvable next) {
119 119 AddHandler(next);
120 120 }
121 121
122 122 public IPromise<T> Cast<T>() {
123 123 throw new InvalidCastException();
124 124 }
125 125
126 126 public void Join() {
127 127 WaitResult(-1);
128 128 if (IsRejected)
129 129 Rethrow();
130 130 }
131 131
132 132 public void Join(int timeout) {
133 133 WaitResult(timeout);
134 134 if (IsRejected)
135 135 Rethrow();
136 136 }
137 137
138 138 public static ResolvedPromise Resolve() {
139 139 return new ResolvedPromise();
140 140 }
141 141
142 142 public static ResolvedPromise<T> Resolve<T>(T result) {
143 143 return new ResolvedPromise<T>(result);
144 144 }
145 145
146 146 public static RejectedPromise Reject(Exception reason) {
147 147 return new RejectedPromise(reason);
148 148 }
149 149
150 150 public static RejectedPromise<T> Reject<T>(Exception reason) {
151 151 return new RejectedPromise<T>(reason);
152 152 }
153 153
154 154 public static IPromise Create(PromiseExecutor executor) {
155 155 Safe.ArgumentNotNull(executor, nameof(executor));
156 156
157 157 var p = new Promise();
158 158 var d = new Deferred(p, DefaultDispatcher);
159 159
160 160 try {
161 161 executor(d);
162 162 } catch (Exception e) {
163 163 d.Reject(e);
164 164 }
165 165
166 166 return d.Promise;
167 167 }
168 168
169 169 public static IPromise<T> Create<T>(PromiseExecutor<T> executor) {
170 170 Safe.ArgumentNotNull(executor, nameof(executor));
171 171
172 var p = new Promise<T>();
173 var d = new Deferred<T>(p, DefaultDispatcher);
172 var d = new Deferred<T>();
174 173
175 174 try {
176 175 executor(d);
177 176 } catch (Exception e) {
178 177 d.Reject(e);
179 178 }
180 179
181 180 return d.Promise;
182 181 }
183 182
184 183 public static IPromise All(IEnumerable<IPromise> promises) {
185 var d = new Deferred(DefaultDispatcher);
184 var d = new Deferred();
186 185 var all = new PromiseAll(d);
187 186 foreach (var promise in promises) {
188 187 all.AddPromise(promise);
189 188 if (all.Done)
190 189 break;
191 190 }
192 191 all.Complete();
193 192 return all.ResultPromise;
194 193 }
195 194
196 public static IPromise<T[]> All<T>(IEnumerable<IPromise<T>> promises, Func<T, IPromise> cleanup, Action cancel) {
197 var d = new Deferred<T[]>(DefaultDispatcher);
195 public static IPromise<T[]> All<T>(IEnumerable<IPromise<T>> promises, Func<T, IPromise> cleanup = null, Action cancel = null) {
196 var d = new Deferred<T[]>();
198 197 var all = new PromiseAll<T>(d, cleanup, cancel);
199 198 foreach (var promise in promises) {
200 199 all.AddPromise(promise);
201 200 if (all.Done)
202 201 break;
203 202 }
204 203 all.Complete();
205 204 return all.ResultPromise;
206 205 }
207 206 }
208 207 }
209 208
@@ -1,57 +1,92
1 1 using System;
2 2 using System.Diagnostics;
3 3
4 4 namespace Implab {
5 class PromiseActionReaction : PromiseReaction {
5 class PromiseActionReaction : IResolvable {
6 6
7 7 readonly Deferred m_next;
8 8
9 readonly IDispatcher m_dispatcher;
10
11 readonly Action<Deferred> m_fulfilled;
12
13 readonly Action<Exception, Deferred> m_rejected;
14
9 15 public IPromise Promise {
10 16 get { return m_next.Promise; }
11 17 }
12 18
13 public PromiseActionReaction(Action fulfilled, Action<Exception> rejected, IDispatcher dispatcher) : base(dispatcher) {
14 m_next = new Deferred(dispatcher);
15 if (fulfilled != null)
16 FulfilHandler = PromiseHandler.Create(fulfilled, m_next);
17
18 if (rejected != null)
19 RejectHandler = PromiseHandler.Create(rejected, m_next);
20 }
21
22 public PromiseActionReaction(Func<IPromise> fulfilled, Func<Exception, IPromise> rejected, IDispatcher dispatcher) : base(dispatcher) {
23 m_next = new Deferred(dispatcher);
24 if (fulfilled != null)
25 FulfilHandler = PromiseHandler.Create(fulfilled, m_next);
26
27 if (rejected != null)
28 RejectHandler = PromiseHandler.Create(rejected, m_next);
19 public PromiseActionReaction(Action<Deferred> fulfilled, Action<Exception, Deferred> rejected, Deferred next, IDispatcher dispatcher) {
20 m_next = next;
21 m_fulfilled = fulfilled;
22 m_rejected = rejected;
23 m_dispatcher = dispatcher;
29 24 }
30 25
31 public PromiseActionReaction(Action fulfilled, Func<Exception, IPromise> rejected, IDispatcher dispatcher) : base(dispatcher) {
32 m_next = new Deferred(dispatcher);
33 if (fulfilled != null)
34 FulfilHandler = PromiseHandler.Create(fulfilled, m_next);
35
36 if (rejected != null)
37 RejectHandler = PromiseHandler.Create(rejected, m_next);
38 }
39
40 public PromiseActionReaction(Func<IPromise> fulfilled, Action<Exception> rejected, IDispatcher dispatcher) : base(dispatcher) {
41 m_next = new Deferred(dispatcher);
42 if (fulfilled != null)
43 FulfilHandler = PromiseHandler.Create(fulfilled, m_next);
44
45 if (rejected != null)
46 RejectHandler = PromiseHandler.Create(rejected, m_next);
47 }
48
49 protected override void DefaultReject(Exception reason) {
50 m_next.Reject(reason);
51 }
52
53 protected override void DefaultResolve() {
26 public void Resolve() {
27 if (m_fulfilled != null) {
28 if (m_dispatcher != null)
29 m_dispatcher.Enqueue(ResolveImpl);
30 else
31 ResolveImpl();
32 } else {
54 33 m_next.Resolve();
55 34 }
56 35 }
36
37 void ResolveImpl() {
38 m_fulfilled(m_next);
39 }
40
41 public void Reject(Exception error) {
42 if (m_fulfilled != null) {
43 if (m_dispatcher != null)
44 m_dispatcher.Enqueue(RejectImpl, error);
45 else
46 RejectImpl(error);
47 } else {
48 m_next.Reject(error);
49 }
50 }
51
52 void RejectImpl(Exception error) {
53 m_rejected(error, m_next);
54 }
55
56 public static PromiseActionReaction Create(Action fulfilled, Action<Exception> rejected, IDispatcher dispatcher) {
57 return new PromiseActionReaction(
58 fulfilled != null ? PromiseHandler.Create(fulfilled) : null,
59 rejected != null ? PromiseHandler.Create(rejected) : null,
60 new Deferred(),
61 dispatcher
62 );
63 }
64
65 public static PromiseActionReaction Create(Func<IPromise> fulfilled, Action<Exception> rejected, IDispatcher dispatcher) {
66 return new PromiseActionReaction(
67 fulfilled != null ? PromiseHandler.Create(fulfilled) : null,
68 rejected != null ? PromiseHandler.Create(rejected) : null,
69 new Deferred(),
70 dispatcher
71 );
72 }
73
74 public static PromiseActionReaction Create(Action fulfilled, Func<Exception, IPromise> rejected, IDispatcher dispatcher) {
75 return new PromiseActionReaction(
76 fulfilled != null ? PromiseHandler.Create(fulfilled) : null,
77 rejected != null ? PromiseHandler.Create(rejected) : null,
78 new Deferred(),
79 dispatcher
80 );
81 }
82
83 public static PromiseActionReaction Create(Func<IPromise> fulfilled, Func<Exception, IPromise> rejected, IDispatcher dispatcher) {
84 return new PromiseActionReaction(
85 fulfilled != null ? PromiseHandler.Create(fulfilled) : null,
86 rejected != null ? PromiseHandler.Create(rejected) : null,
87 new Deferred(),
88 dispatcher
89 );
90 }
91 }
57 92 } No newline at end of file
@@ -1,56 +1,91
1 1 using System;
2 2 using System.Diagnostics;
3 3
4 4 namespace Implab {
5 class PromiseActionReaction<T> : PromiseReaction<T> {
5 class PromiseActionReaction<T> : IResolvable<T> {
6 6 readonly Deferred m_next;
7 7
8 readonly IDispatcher m_dispatcher;
9
10 readonly Action<T, Deferred> m_fulfilled;
11
12 readonly Action<Exception, Deferred> m_rejected;
13
8 14 public IPromise Promise {
9 15 get { return m_next.Promise; }
10 16 }
11 17
12 public PromiseActionReaction(Action<T> fulfilled, Action<Exception> rejected, IDispatcher dispatcher) : base(dispatcher) {
13 m_next = new Deferred(dispatcher);
14 if (fulfilled != null)
15 FulfilHandler = PromiseHandler.Create(fulfilled, m_next);
16
17 if (rejected != null)
18 RejectHandler = PromiseHandler.Create(rejected, m_next);
19 }
20
21 public PromiseActionReaction(Func<T, IPromise> fulfilled, Func<Exception, IPromise> rejected, IDispatcher dispatcher) : base(dispatcher) {
22 m_next = new Deferred(dispatcher);
23 if (fulfilled != null)
24 FulfilHandler = PromiseHandler.Create(fulfilled, m_next);
25
26 if (rejected != null)
27 RejectHandler = PromiseHandler.Create(rejected, m_next);
18 public PromiseActionReaction(Action<T, Deferred> fulfilled, Action<Exception, Deferred> rejected, Deferred next, IDispatcher dispatcher) {
19 m_next = next;
20 m_fulfilled = fulfilled;
21 m_rejected = rejected;
22 m_dispatcher = dispatcher;
28 23 }
29 24
30 public PromiseActionReaction(Action<T> fulfilled, Func<Exception, IPromise> rejected, IDispatcher dispatcher) : base(dispatcher) {
31 m_next = new Deferred(dispatcher);
32 if (fulfilled != null)
33 FulfilHandler = PromiseHandler.Create(fulfilled, m_next);
34
35 if (rejected != null)
36 RejectHandler = PromiseHandler.Create(rejected, m_next);
37 }
38
39 public PromiseActionReaction(Func<T, IPromise> fulfilled, Action<Exception> rejected, IDispatcher dispatcher) : base(dispatcher) {
40 m_next = new Deferred(dispatcher);
41 if (fulfilled != null)
42 FulfilHandler = PromiseHandler.Create(fulfilled, m_next);
43
44 if (rejected != null)
45 RejectHandler = PromiseHandler.Create(rejected, m_next);
46 }
47
48 protected override void DefaultReject(Exception reason) {
49 m_next.Reject(reason);
50 }
51
52 protected override void DefaultResolve(T result) {
25 public void Resolve(T result) {
26 if (m_fulfilled != null) {
27 if (m_dispatcher != null)
28 m_dispatcher.Enqueue(ResolveImpl, result);
29 else
30 ResolveImpl(result);
31 } else {
53 32 m_next.Resolve();
54 33 }
55 34 }
35
36 void ResolveImpl (T result) {
37 m_fulfilled(result, m_next);
38 }
39
40 public void Reject(Exception error) {
41 if (m_fulfilled != null) {
42 if (m_dispatcher != null)
43 m_dispatcher.Enqueue(RejectImpl, error);
44 else
45 RejectImpl(error);
46 } else {
47 m_next.Reject(error);
48 }
49 }
50
51 void RejectImpl(Exception error) {
52 m_rejected(error, m_next);
53 }
54
55 public static PromiseActionReaction<T> Create(Action<T> fulfilled, Action<Exception> rejected, IDispatcher dispatcher) {
56 return new PromiseActionReaction<T>(
57 fulfilled != null ? PromiseHandler.Create(fulfilled) : null,
58 rejected != null ? PromiseHandler.Create(rejected) : null,
59 new Deferred(),
60 dispatcher
61 );
62 }
63
64 public static PromiseActionReaction<T> Create(Func<T,IPromise> fulfilled, Action<Exception> rejected, IDispatcher dispatcher) {
65 return new PromiseActionReaction<T>(
66 fulfilled != null ? PromiseHandler.Create(fulfilled) : null,
67 rejected != null ? PromiseHandler.Create(rejected) : null,
68 new Deferred(),
69 dispatcher
70 );
71 }
72
73 public static PromiseActionReaction<T> Create(Action<T> fulfilled, Func<Exception, IPromise> rejected, IDispatcher dispatcher) {
74 return new PromiseActionReaction<T>(
75 fulfilled != null ? PromiseHandler.Create(fulfilled) : null,
76 rejected != null ? PromiseHandler.Create(rejected) : null,
77 new Deferred(),
78 dispatcher
79 );
80 }
81
82 public static PromiseActionReaction<T> Create(Func<T,IPromise> fulfilled, Func<Exception, IPromise> rejected, IDispatcher dispatcher) {
83 return new PromiseActionReaction<T>(
84 fulfilled != null ? PromiseHandler.Create(fulfilled) : null,
85 rejected != null ? PromiseHandler.Create(rejected) : null,
86 new Deferred(),
87 dispatcher
88 );
89 }
90 }
56 91 } No newline at end of file
@@ -1,131 +1,217
1 1 using System.Threading;
2 2 using System;
3 3 using Implab.Diagnostics;
4 4 using System.Collections.Generic;
5 5 using System.Linq;
6 6
7 7 namespace Implab {
8 8 public static class PromiseExtensions {
9 9
10 10 public static IPromise Then(this IPromise that, Action fulfilled, Action<Exception> rejected) {
11 var reaction = new PromiseActionReaction(fulfilled, rejected, Promise.DefaultDispatcher);
11 var reaction = PromiseActionReaction.Create(fulfilled, rejected, Promise.DefaultDispatcher);
12 that.Then(reaction);
13 return reaction.Promise;
14 }
15
16 public static IPromise Then(this IPromise that, Action fulfilled) {
17 var reaction = PromiseActionReaction.Create(fulfilled, null, Promise.DefaultDispatcher);
12 18 that.Then(reaction);
13 19 return reaction.Promise;
14 20 }
15 21
16 22 public static IPromise Then(this IPromise that, Action fulfilled, Func<Exception, IPromise> rejected) {
17 var reaction = new PromiseActionReaction(fulfilled, rejected, Promise.DefaultDispatcher);
23 var reaction = PromiseActionReaction.Create(fulfilled, rejected, Promise.DefaultDispatcher);
18 24 that.Then(reaction);
19 25 return reaction.Promise;
20 26 }
21 27
22 28 public static IPromise Then(this IPromise that, Func<IPromise> fulfilled, Action<Exception> rejected) {
23 var reaction = new PromiseActionReaction(fulfilled, rejected, Promise.DefaultDispatcher);
29 var reaction = PromiseActionReaction.Create(fulfilled, rejected, Promise.DefaultDispatcher);
30 that.Then(reaction);
31 return reaction.Promise;
32 }
33
34 public static IPromise Then(this IPromise that, Func<IPromise> fulfilled) {
35 var reaction = PromiseActionReaction.Create(fulfilled, null, Promise.DefaultDispatcher);
24 36 that.Then(reaction);
25 37 return reaction.Promise;
26 38 }
27 39
28 40 public static IPromise Then(this IPromise that, Func<IPromise> fulfilled, Func<Exception, IPromise> rejected) {
29 var reaction = new PromiseActionReaction(fulfilled, rejected, Promise.DefaultDispatcher);
41 var reaction = PromiseActionReaction.Create(fulfilled, rejected, Promise.DefaultDispatcher);
30 42 that.Then(reaction);
31 43 return reaction.Promise;
32 44 }
33 45
34 46 public static IPromise Then<T>(this IPromise<T> that, Action<T> fulfilled, Action<Exception> rejected) {
35 var reaction = new PromiseActionReaction<T>(fulfilled, rejected, Promise.DefaultDispatcher);
47 var reaction = PromiseActionReaction<T>.Create(fulfilled, rejected, Promise.DefaultDispatcher);
48 that.Then(reaction);
49 return reaction.Promise;
50 }
51
52 public static IPromise Then<T>(this IPromise<T> that, Action<T> fulfilled) {
53 var reaction = PromiseActionReaction<T>.Create(fulfilled, null, Promise.DefaultDispatcher);
36 54 that.Then(reaction);
37 55 return reaction.Promise;
38 56 }
39 57
40 58 public static IPromise Then<T>(this IPromise<T> that, Action<T> fulfilled, Func<Exception, IPromise> rejected) {
41 var reaction = new PromiseActionReaction<T>(fulfilled, rejected, Promise.DefaultDispatcher);
59 var reaction = PromiseActionReaction<T>.Create(fulfilled, rejected, Promise.DefaultDispatcher);
42 60 that.Then(reaction);
43 61 return reaction.Promise;
44 62 }
45 63
46 64 public static IPromise Then<T>(this IPromise<T> that, Func<T, IPromise> fulfilled, Action<Exception> rejected) {
47 var reaction = new PromiseActionReaction<T>(fulfilled, rejected, Promise.DefaultDispatcher);
65 var reaction = PromiseActionReaction<T>.Create(fulfilled, rejected, Promise.DefaultDispatcher);
66 that.Then(reaction);
67 return reaction.Promise;
68 }
69
70 public static IPromise Then<T>(this IPromise<T> that, Func<T, IPromise> fulfilled) {
71 var reaction = PromiseActionReaction<T>.Create(fulfilled, null, Promise.DefaultDispatcher);
48 72 that.Then(reaction);
49 73 return reaction.Promise;
50 74 }
51 75
52 76 public static IPromise Then<T>(this IPromise<T> that, Func<T, IPromise> fulfilled, Func<Exception, IPromise> rejected) {
53 var reaction = new PromiseActionReaction<T>(fulfilled, rejected, Promise.DefaultDispatcher);
77 var reaction = PromiseActionReaction<T>.Create(fulfilled, rejected, Promise.DefaultDispatcher);
54 78 that.Then(reaction);
55 79 return reaction.Promise;
56 80 }
57 81
58 82 public static IPromise<Tout> Then<Tout>(this IPromise that, Func<Tout> fulfilled, Func<Exception, Tout> rejected) {
59 var reaction = new PromiseFuncReaction<Tout>(fulfilled, rejected, Promise.DefaultDispatcher);
83 var reaction = PromiseFuncReaction<Tout>.Create(fulfilled, rejected, Promise.DefaultDispatcher);
84 that.Then(reaction);
85 return reaction.Promise;
86 }
87
88 public static IPromise<Tout> Then<Tout>(this IPromise that, Func<Tout> fulfilled) {
89 var reaction = PromiseFuncReaction<Tout>.Create(fulfilled, (Func<Exception, Tout>)null, Promise.DefaultDispatcher);
60 90 that.Then(reaction);
61 91 return reaction.Promise;
62 92 }
63 93
64 94 public static IPromise<Tout> Then<Tout>(this IPromise that, Func<Tout> fulfilled, Func<Exception, IPromise<Tout>> rejected) {
65 var reaction = new PromiseFuncReaction<Tout>(fulfilled, rejected, Promise.DefaultDispatcher);
95 var reaction = PromiseFuncReaction<Tout>.Create(fulfilled, rejected, Promise.DefaultDispatcher);
66 96 that.Then(reaction);
67 97 return reaction.Promise;
68 98 }
69 99
70 100 public static IPromise<Tout> Then<Tout>(this IPromise that, Func<IPromise<Tout>> fulfilled, Func<Exception, Tout> rejected) {
71 var reaction = new PromiseFuncReaction<Tout>(fulfilled, rejected, Promise.DefaultDispatcher);
101 var reaction = PromiseFuncReaction<Tout>.Create(fulfilled, rejected, Promise.DefaultDispatcher);
102 that.Then(reaction);
103 return reaction.Promise;
104 }
105
106 public static IPromise<Tout> Then<Tout>(this IPromise that, Func<IPromise<Tout>> fulfilled) {
107 var reaction = PromiseFuncReaction<Tout>.Create(fulfilled, (Func<Exception, Tout>)null, Promise.DefaultDispatcher);
72 108 that.Then(reaction);
73 109 return reaction.Promise;
74 110 }
75 111
76 112 public static IPromise<Tout> Then<Tout>(this IPromise that, Func<IPromise<Tout>> fulfilled, Func<Exception, IPromise<Tout>> rejected) {
77 var reaction = new PromiseFuncReaction<Tout>(fulfilled, rejected, Promise.DefaultDispatcher);
113 var reaction = PromiseFuncReaction<Tout>.Create(fulfilled, rejected, Promise.DefaultDispatcher);
78 114 that.Then(reaction);
79 115 return reaction.Promise;
80 116 }
81 117
82 118 public static IPromise<Tout> Then<Tin, Tout>(this IPromise<Tin> that, Func<Tin, Tout> fulfilled, Func<Exception, Tout> rejected) {
83 var reaction = new PromiseFuncReaction<Tin, Tout>(fulfilled, rejected, Promise.DefaultDispatcher);
119 var reaction = PromiseFuncReaction<Tin, Tout>.Create(fulfilled, rejected, Promise.DefaultDispatcher);
120 that.Then(reaction);
121 return reaction.Promise;
122 }
123
124 public static IPromise<Tout> Then<Tin, Tout>(this IPromise<Tin> that, Func<Tin, Tout> fulfilled) {
125 var reaction = PromiseFuncReaction<Tin, Tout>.Create(fulfilled, (Func<Exception, Tout>)null, Promise.DefaultDispatcher);
84 126 that.Then(reaction);
85 127 return reaction.Promise;
86 128 }
87 129
88 130 public static IPromise<Tout> Then<Tin, Tout>(this IPromise<Tin> that, Func<Tin, Tout> fulfilled, Func<Exception, IPromise<Tout>> rejected) {
89 var reaction = new PromiseFuncReaction<Tin, Tout>(fulfilled, rejected, Promise.DefaultDispatcher);
131 var reaction = PromiseFuncReaction<Tin, Tout>.Create(fulfilled, rejected, Promise.DefaultDispatcher);
90 132 that.Then(reaction);
91 133 return reaction.Promise;
92 134 }
93 135
94 136 public static IPromise<Tout> Then<Tin, Tout>(this IPromise<Tin> that, Func<Tin, IPromise<Tout>> fulfilled, Func<Exception, Tout> rejected) {
95 var reaction = new PromiseFuncReaction<Tin, Tout>(fulfilled, rejected, Promise.DefaultDispatcher);
137 var reaction = PromiseFuncReaction<Tin, Tout>.Create(fulfilled, rejected, Promise.DefaultDispatcher);
138 that.Then(reaction);
139 return reaction.Promise;
140 }
141
142 public static IPromise<Tout> Then<Tin, Tout>(this IPromise<Tin> that, Func<Tin, IPromise<Tout>> fulfilled) {
143 var reaction = PromiseFuncReaction<Tin, Tout>.Create(fulfilled, (Func<Exception, Tout>)null, Promise.DefaultDispatcher);
96 144 that.Then(reaction);
97 145 return reaction.Promise;
98 146 }
99 147
100 148 public static IPromise<Tout> Then<Tin, Tout>(this IPromise<Tin> that, Func<Tin, IPromise<Tout>> fulfilled, Func<Exception, IPromise<Tout>> rejected) {
101 var reaction = new PromiseFuncReaction<Tin, Tout>(fulfilled, rejected, Promise.DefaultDispatcher);
149 var reaction = PromiseFuncReaction<Tin, Tout>.Create(fulfilled, rejected, Promise.DefaultDispatcher);
102 150 that.Then(reaction);
103 151 return reaction.Promise;
104 152 }
105 153
106 154 public static IPromise Catch(this IPromise that, Action<Exception> rejected) {
107 155 return Then(that, null, rejected);
108 156 }
109 157
110 158 public static IPromise Catch(this IPromise that, Func<Exception, IPromise> rejected) {
111 159 return Then(that, null, rejected);
112 160 }
113 161
114 162 public static IPromise<Tout> Catch<Tout>(this IPromise that, Func<Exception, Tout> rejected) {
115 163 return Then(that, (Func<Tout>)null, rejected);
116 164 }
117 165
118 166 public static IPromise<Tout> Catch<Tout>(this IPromise that, Func<Exception, IPromise<Tout>> rejected) {
119 167 return Then(that, (Func<Tout>)null, rejected);
120 168 }
121 169
122 170 public static IPromise<Tout> Catch<Tin, Tout>(this IPromise<Tin> that, Func<Exception, Tout> rejected) {
123 171 return Then(that, (Func<Tin, Tout>)null, rejected);
124 172 }
125 173
126 174 public static IPromise<Tout> Catch<Tin, Tout>(this IPromise<Tin> that, Func<Exception, IPromise<Tout>> rejected) {
127 175 return Then(that, (Func<Tin, Tout>)null, rejected);
128 176 }
177
178 public static IPromise Finally(this IPromise that, Action final) {
179 return Then(that, final, e => {
180 final();
181 throw e.Rethrow();
182 });
183 }
184
185 public static IPromise Finally(this IPromise that, Func<IPromise> final) {
186 return Then(that, final, e => {
187 final();
188 throw e.Rethrow();
189 });
190 }
191
192 public static IPromise<T> Finally<T>(this IPromise<T> that, Action final) {
193 return Then<T, T>(that, x => {
194 final();
195 return x;
196 }, new Func<Exception, T>(e => {
197 final();
198 throw e.Rethrow();
199 }));
200 }
201
202 public static IPromise<T> Finally<T>(this IPromise<T> that, Func<IPromise> final) {
203 return Then<T, T>(that, x => {
204 return final()
205 .Then(() => x);
206 }, new Func<Exception, IPromise<T>>(e => {
207 return final()
208 .Then(new Func<T>(() => {
209 throw e.Rethrow();
210 }));
211 }));
212 }
213
214
129 215 }
130 216 }
131 217
@@ -1,56 +1,91
1 1 using System;
2 2 using System.Diagnostics;
3 3
4 4 namespace Implab {
5 class PromiseFuncReaction<TRet> : PromiseReaction {
5 class PromiseFuncReaction<TRet> : IResolvable {
6 6 readonly Deferred<TRet> m_next;
7 7
8 readonly IDispatcher m_dispatcher;
9
10 readonly Action<Deferred<TRet>> m_fulfilled;
11
12 readonly Action<Exception, Deferred<TRet>> m_rejected;
13
8 14 public IPromise<TRet> Promise {
9 15 get { return m_next.Promise; }
10 16 }
11 17
12 public PromiseFuncReaction(Func<TRet> fulfilled, Func<Exception, TRet> rejected, IDispatcher dispatcher) : base(dispatcher) {
13 m_next = new Deferred<TRet>(dispatcher);
14 if (fulfilled != null)
15 FulfilHandler = PromiseHandler.Create(fulfilled, m_next);
16
17 if (rejected != null)
18 RejectHandler = PromiseHandler.Create(rejected, m_next);
18 public PromiseFuncReaction(Action<Deferred<TRet>> fulfilled, Action<Exception, Deferred<TRet>> rejected, Deferred<TRet> next, IDispatcher dispatcher) {
19 m_next = next;
20 m_fulfilled = fulfilled;
21 m_rejected = rejected;
22 m_dispatcher = dispatcher;
19 23 }
20 24
21 public PromiseFuncReaction(Func<IPromise<TRet>> fulfilled, Func<Exception, IPromise<TRet>> rejected, IDispatcher dispatcher) : base(dispatcher) {
22 m_next = new Deferred<TRet>(dispatcher);
23 if (fulfilled != null)
24 FulfilHandler = PromiseHandler.Create(fulfilled, m_next);
25 public void Resolve() {
26 if (m_fulfilled != null) {
27 if (m_dispatcher != null)
28 m_dispatcher.Enqueue(ResolveImpl);
29 else
30 ResolveImpl();
31 } else {
32 m_next.Resolve(default(TRet));
33 }
34 }
25 35
26 if (rejected != null)
27 RejectHandler = PromiseHandler.Create(rejected, m_next);
36 void ResolveImpl () {
37 m_fulfilled(m_next);
38 }
39
40 public void Reject(Exception error) {
41 if (m_fulfilled != null) {
42 if (m_dispatcher != null)
43 m_dispatcher.Enqueue(RejectImpl, error);
44 else
45 RejectImpl(error);
46 } else {
47 m_next.Reject(error);
48 }
28 49 }
29 50
30 public PromiseFuncReaction(Func<TRet> fulfilled, Func<Exception, IPromise<TRet>> rejected, IDispatcher dispatcher) : base(dispatcher) {
31 m_next = new Deferred<TRet>(dispatcher);
32 if (fulfilled != null)
33 FulfilHandler = PromiseHandler.Create(fulfilled, m_next);
51 void RejectImpl(Exception error) {
52 m_rejected(error, m_next);
53 }
34 54
35 if (rejected != null)
36 RejectHandler = PromiseHandler.Create(rejected, m_next);
55 public static PromiseFuncReaction<TRet> Create(Func<TRet> fulfilled, Func<Exception, TRet> rejected, IDispatcher dispatcher) {
56 return new PromiseFuncReaction<TRet>(
57 fulfilled != null ? PromiseHandler.Create(fulfilled) : null,
58 rejected != null ? PromiseHandler.Create(rejected) : null,
59 new Deferred<TRet>(),
60 dispatcher
61 );
37 62 }
38 63
39 public PromiseFuncReaction(Func<IPromise<TRet>> fulfilled, Func<Exception, TRet> rejected, IDispatcher dispatcher) : base(dispatcher) {
40 m_next = new Deferred<TRet>(dispatcher);
41 if (fulfilled != null)
42 FulfilHandler = PromiseHandler.Create(fulfilled, m_next);
43
44 if (rejected != null)
45 RejectHandler = PromiseHandler.Create(rejected, m_next);
64 public static PromiseFuncReaction<TRet> Create(Func<IPromise<TRet>> fulfilled, Func<Exception, TRet> rejected, IDispatcher dispatcher) {
65 return new PromiseFuncReaction<TRet>(
66 fulfilled != null ? PromiseHandler.Create(fulfilled) : null,
67 rejected != null ? PromiseHandler.Create(rejected) : null,
68 new Deferred<TRet>(),
69 dispatcher
70 );
46 71 }
47 72
48 protected override void DefaultReject(Exception reason) {
49 m_next.Reject(reason);
73 public static PromiseFuncReaction<TRet> Create(Func<TRet> fulfilled, Func<Exception, IPromise<TRet>> rejected, IDispatcher dispatcher) {
74 return new PromiseFuncReaction<TRet>(
75 fulfilled != null ? PromiseHandler.Create(fulfilled) : null,
76 rejected != null ? PromiseHandler.Create(rejected) : null,
77 new Deferred<TRet>(),
78 dispatcher
79 );
50 80 }
51 81
52 protected override void DefaultResolve() {
53 throw new NotImplementedException();
82 public static PromiseFuncReaction<TRet> Create(Func<IPromise<TRet>> fulfilled, Func<Exception, IPromise<TRet>> rejected, IDispatcher dispatcher) {
83 return new PromiseFuncReaction<TRet>(
84 fulfilled != null ? PromiseHandler.Create(fulfilled) : null,
85 rejected != null ? PromiseHandler.Create(rejected) : null,
86 new Deferred<TRet>(),
87 dispatcher
88 );
54 89 }
55 90 }
56 91 } No newline at end of file
@@ -1,56 +1,97
1 1 using System;
2 2 using System.Diagnostics;
3 3
4 4 namespace Implab {
5 class PromiseFuncReaction<TIn, TRet> : PromiseReaction<TIn> {
5 class PromiseFuncReaction<TIn, TRet> : IResolvable<TIn> {
6 6 readonly Deferred<TRet> m_next;
7 7
8 readonly IDispatcher m_dispatcher;
9
10 readonly Action<TIn, Deferred<TRet>> m_fulfilled;
11
12 readonly Action<Exception, Deferred<TRet>> m_rejected;
13
8 14 public IPromise<TRet> Promise {
9 15 get { return m_next.Promise; }
10 16 }
11 17
12 public PromiseFuncReaction(Func<TIn, TRet> fulfilled, Func<Exception, TRet> rejected, IDispatcher dispatcher) : base(dispatcher) {
13 m_next = new Deferred<TRet>(dispatcher);
14 if (fulfilled != null)
15 FulfilHandler = PromiseHandler.Create(fulfilled, m_next);
16
17 if (rejected != null)
18 RejectHandler = PromiseHandler.Create(rejected, m_next);
18 public PromiseFuncReaction(Action<TIn, Deferred<TRet>> fulfilled, Action<Exception, Deferred<TRet>> rejected, Deferred<TRet> next, IDispatcher dispatcher) {
19 m_next = next;
20 m_fulfilled = fulfilled;
21 m_rejected = rejected;
22 m_dispatcher = dispatcher;
19 23 }
20 24
21 public PromiseFuncReaction(Func<TIn, IPromise<TRet>> fulfilled, Func<Exception, IPromise<TRet>> rejected, IDispatcher dispatcher) : base(dispatcher) {
22 m_next = new Deferred<TRet>(dispatcher);
23 if (fulfilled != null)
24 FulfilHandler = PromiseHandler.Create(fulfilled, m_next);
25 public void Resolve(TIn result) {
26 if (m_fulfilled != null) {
27 if (m_dispatcher != null)
28 m_dispatcher.Enqueue(ResolveImpl, result);
29 else
30 ResolveImpl(result);
31 } else {
32 try {
33 m_next.Resolve((TRet)(object)result);
34 } catch(Exception error) {
35 // handle cast exceptions
36 m_next.Reject(error);
37 }
38 }
39 }
25 40
26 if (rejected != null)
27 RejectHandler = PromiseHandler.Create(rejected, m_next);
41 void ResolveImpl (TIn result) {
42 m_fulfilled(result, m_next);
43 }
44
45 public void Reject(Exception error) {
46 if (m_fulfilled != null) {
47 if (m_dispatcher != null)
48 m_dispatcher.Enqueue(RejectImpl, error);
49 else
50 RejectImpl(error);
51 } else {
52 m_next.Reject(error);
53 }
28 54 }
29 55
30 public PromiseFuncReaction(Func<TIn, TRet> fulfilled, Func<Exception, IPromise<TRet>> rejected, IDispatcher dispatcher) : base(dispatcher) {
31 m_next = new Deferred<TRet>(dispatcher);
32 if (fulfilled != null)
33 FulfilHandler = PromiseHandler.Create(fulfilled, m_next);
56 void RejectImpl(Exception error) {
57 m_rejected(error, m_next);
58 }
59
34 60
35 if (rejected != null)
36 RejectHandler = PromiseHandler.Create(rejected, m_next);
61 public static PromiseFuncReaction<TIn,TRet> Create(Func<TIn,TRet> fulfilled, Func<Exception, TRet> rejected, IDispatcher dispatcher) {
62 return new PromiseFuncReaction<TIn,TRet>(
63 fulfilled != null ? PromiseHandler.Create(fulfilled) : null,
64 rejected != null ? PromiseHandler.Create(rejected) : null,
65 new Deferred<TRet>(),
66 dispatcher
67 );
37 68 }
38 69
39 public PromiseFuncReaction(Func<TIn, IPromise<TRet>> fulfilled, Func<Exception, TRet> rejected, IDispatcher dispatcher) : base(dispatcher) {
40 m_next = new Deferred<TRet>(dispatcher);
41 if (fulfilled != null)
42 FulfilHandler = PromiseHandler.Create(fulfilled, m_next);
43
44 if (rejected != null)
45 RejectHandler = PromiseHandler.Create(rejected, m_next);
70 public static PromiseFuncReaction<TIn,TRet> Create(Func<TIn,IPromise<TRet>> fulfilled, Func<Exception, TRet> rejected, IDispatcher dispatcher) {
71 return new PromiseFuncReaction<TIn,TRet>(
72 fulfilled != null ? PromiseHandler.Create(fulfilled) : null,
73 rejected != null ? PromiseHandler.Create(rejected) : null,
74 new Deferred<TRet>(),
75 dispatcher
76 );
46 77 }
47 78
48 protected override void DefaultReject(Exception reason) {
49 m_next.Reject(reason);
79 public static PromiseFuncReaction<TIn,TRet> Create(Func<TIn,TRet> fulfilled, Func<Exception, IPromise<TRet>> rejected, IDispatcher dispatcher) {
80 return new PromiseFuncReaction<TIn,TRet>(
81 fulfilled != null ? PromiseHandler.Create(fulfilled) : null,
82 rejected != null ? PromiseHandler.Create(rejected) : null,
83 new Deferred<TRet>(),
84 dispatcher
85 );
50 86 }
51 87
52 protected override void DefaultResolve(TIn result) {
53 m_next.Resolve((TRet)(object)result);
88 public static PromiseFuncReaction<TIn,TRet> Create(Func<TIn,IPromise<TRet>> fulfilled, Func<Exception, IPromise<TRet>> rejected, IDispatcher dispatcher) {
89 return new PromiseFuncReaction<TIn,TRet>(
90 fulfilled != null ? PromiseHandler.Create(fulfilled) : null,
91 rejected != null ? PromiseHandler.Create(rejected) : null,
92 new Deferred<TRet>(),
93 dispatcher
94 );
54 95 }
55 96 }
56 97 } No newline at end of file
@@ -1,102 +1,102
1 1 using System;
2 2 using System.Diagnostics;
3 3
4 4 namespace Implab {
5 5 class PromiseHandler {
6 public static Action<T> Create<T>(Action<T> handler, Deferred next) {
6 public static Action<T, Deferred> Create<T>(Action<T> handler) {
7 7 Debug.Assert(handler != null);
8 8
9 return (v) => {
9 return (v, next) => {
10 10 try {
11 11 handler(v);
12 12 next.Resolve();
13 13 } catch (Exception err) {
14 14 next.Reject(err);
15 15 }
16 16 };
17 17 }
18 18
19 public static Action<T> Create<T>(Func<T, IPromise> handler, Deferred next) {
19 public static Action<T, Deferred> Create<T>(Func<T, IPromise> handler) {
20 20 Debug.Assert(handler != null);
21 21
22 return (v) => {
22 return (v, next) => {
23 23 try {
24 24 next.Resolve(handler(v));
25 25 } catch (Exception err) {
26 26 next.Reject(err);
27 27 }
28 28 };
29 29 }
30 30
31 public static Action<T> Create<T, T2>(Func<T, T2> handler, Deferred<T2> next) {
31 public static Action<T, Deferred<T2>> Create<T, T2>(Func<T, T2> handler) {
32 32 Debug.Assert(handler != null);
33 33
34 return (v) => {
34 return (v, next) => {
35 35 try {
36 36 next.Resolve(handler(v));
37 37 } catch (Exception err) {
38 38 next.Reject(err);
39 39 }
40 40 };
41 41 }
42 42
43 public static Action<T> Create<T, T2>(Func<T, IPromise<T2>> handler, Deferred<T2> next) {
43 public static Action<T, Deferred<T2>> Create<T, T2>(Func<T, IPromise<T2>> handler) {
44 44 Debug.Assert(handler != null);
45 return (v) => {
45 return (v, next) => {
46 46 try {
47 47 next.Resolve(handler(v));
48 48 } catch (Exception err) {
49 49 next.Reject(err);
50 50 }
51 51 };
52 52 }
53 53
54 public static Action Create(Action handler, Deferred next) {
54 public static Action<Deferred> Create(Action handler) {
55 55 Debug.Assert(handler != null);
56 56
57 return () => {
57 return (next) => {
58 58 try {
59 59 handler();
60 60 next.Resolve();
61 61 } catch (Exception err) {
62 62 next.Reject(err);
63 63 }
64 64 };
65 65 }
66 66
67 public static Action Create(Func<IPromise> handler, Deferred next) {
67 public static Action<Deferred> Create(Func<IPromise> handler) {
68 68 Debug.Assert(handler != null);
69 69
70 return () => {
70 return (next) => {
71 71 try {
72 72 next.Resolve(handler());
73 73 } catch (Exception err) {
74 74 next.Reject(err);
75 75 }
76 76 };
77 77 }
78 78
79 public static Action Create<T2>(Func<T2> handler, Deferred<T2> next) {
79 public static Action<Deferred<T2>> Create<T2>(Func<T2> handler) {
80 80 Debug.Assert(handler != null);
81 81
82 return () => {
82 return (next) => {
83 83 try {
84 84 next.Resolve(handler());
85 85 } catch (Exception err) {
86 86 next.Reject(err);
87 87 }
88 88 };
89 89 }
90 90
91 public static Action Create<T2>(Func<IPromise<T2>> handler, Deferred<T2> next) {
91 public static Action<Deferred<T2>> Create<T2>(Func<IPromise<T2>> handler) {
92 92 Debug.Assert(handler != null);
93 return () => {
93 return (next) => {
94 94 try {
95 95 next.Resolve(handler());
96 96 } catch (Exception err) {
97 97 next.Reject(err);
98 98 }
99 99 };
100 100 }
101 101 }
102 102 } No newline at end of file
@@ -1,38 +1,38
1 1 using System;
2 2
3 3 namespace Implab
4 4 {
5 5 public struct RejectedPromise : IPromise {
6 6 readonly Exception m_reason;
7 7
8 8 public Type ResultType => typeof(void);
9 9
10 10 public bool IsResolved => true;
11 11
12 12 public bool IsRejected => true;
13 13
14 14 public bool IsFulfilled => false;
15 15
16 16 public Exception RejectReason => m_reason;
17 17
18 18 public RejectedPromise(Exception reason) {
19 19 m_reason = reason;
20 20 }
21 21
22 22 public IPromise<T> Cast<T>() {
23 23 throw new InvalidCastException();
24 24 }
25 25
26 26 public void Join() {
27 m_reason.ThrowInvocationException();
27 throw m_reason.Wrap();
28 28 }
29 29
30 30 public void Join(int timeout) {
31 m_reason.ThrowInvocationException();
31 throw m_reason.Wrap();
32 32 }
33 33
34 34 public void Then(IResolvable next) {
35 35 next.Reject(m_reason);
36 36 }
37 37 }
38 38 } No newline at end of file
@@ -1,52 +1,50
1 1 using System;
2 2
3 3 namespace Implab
4 4 {
5 5 public struct RejectedPromise<T> : IPromise<T> {
6 6 readonly Exception m_reason;
7 7
8 8 public Type ResultType => typeof(void);
9 9
10 10 public bool IsResolved => true;
11 11
12 12 public bool IsRejected => true;
13 13
14 14 public bool IsFulfilled => false;
15 15
16 16 public Exception RejectReason => m_reason;
17 17
18 18 public RejectedPromise(Exception reason) {
19 19 m_reason = reason;
20 20 }
21 21
22 22 public IPromise<T2> Cast<T2>() {
23 23 return (IPromise<T2>)(IPromise<T>)this;
24 24 }
25 25
26 26 void IPromise.Join() {
27 m_reason.ThrowInvocationException();
27 throw m_reason.Wrap();
28 28 }
29 29
30 30 void IPromise.Join(int timeout) {
31 m_reason.ThrowInvocationException();
31 throw m_reason.Wrap();
32 32 }
33 33
34 34 public T Join() {
35 m_reason.ThrowInvocationException();
36 throw new Exception(); // unreachable code
35 throw m_reason.Wrap();
37 36 }
38 37
39 38 public T Join(int timeout) {
40 m_reason.ThrowInvocationException();
41 throw new Exception(); // unreachable code
39 throw m_reason.Wrap();
42 40 }
43 41
44 42 public void Then(IResolvable next) {
45 43 next.Reject(m_reason);
46 44 }
47 45
48 46 public void Then(IResolvable<T> next) {
49 47 next.Reject(m_reason);
50 48 }
51 49 }
52 50 } No newline at end of file
1 NO CONTENT: file was removed
This diff has been collapsed as it changes many lines, (878 lines changed) Show them Hide them
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
General Comments 3
Under Review
author

Auto status change to "Under Review"

Approved
author

ok, latest stable version should be in default

You need to be logged in to leave comments. Login now