Auto status change to "Under Review"
@@ -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=" |
|
|
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,21 +1,39 | |||
|
1 |
using Implab |
|
|
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 |
|
|
|
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 | } |
@@ -8,51 +8,46 namespace Implab { | |||
|
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 |
|
|
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 |
@@ -4,46 +4,41 using System.Diagnostics; | |||
|
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 |
|
|
|
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 |
|
|
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) { |
@@ -3,16 +3,19 using System.Reflection; | |||
|
3 | 3 | using System.Runtime.ExceptionServices; |
|
4 | 4 | |
|
5 | 5 | namespace Implab { |
|
6 |
|
|
|
7 |
public static |
|
|
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 |
|
|
12 |
if (that |
|
|
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 |
|
|
|
18 | return new TargetInvocationException(that); | |
|
16 | 19 | } |
|
17 | 20 | } |
|
18 | 21 | } No newline at end of file |
@@ -169,8 +169,7 namespace Implab { | |||
|
169 | 169 | public static IPromise<T> Create<T>(PromiseExecutor<T> executor) { |
|
170 | 170 | Safe.ArgumentNotNull(executor, nameof(executor)); |
|
171 | 171 | |
|
172 |
var |
|
|
173 | var d = new Deferred<T>(p, DefaultDispatcher); | |
|
172 | var d = new Deferred<T>(); | |
|
174 | 173 | |
|
175 | 174 | try { |
|
176 | 175 | executor(d); |
@@ -182,7 +181,7 namespace Implab { | |||
|
182 | 181 | } |
|
183 | 182 | |
|
184 | 183 | public static IPromise All(IEnumerable<IPromise> promises) { |
|
185 |
var d = new Deferred( |
|
|
184 | var d = new Deferred(); | |
|
186 | 185 | var all = new PromiseAll(d); |
|
187 | 186 | foreach (var promise in promises) { |
|
188 | 187 | all.AddPromise(promise); |
@@ -193,8 +192,8 namespace Implab { | |||
|
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[]>( |
|
|
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); |
@@ -2,56 +2,91 using System; | |||
|
2 | 2 | using System.Diagnostics; |
|
3 | 3 | |
|
4 | 4 | namespace Implab { |
|
5 |
class PromiseActionReaction : |
|
|
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) |
|
|
14 |
m_next = ne |
|
|
15 |
|
|
|
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 ( |
|
|
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 |
@@ -2,55 +2,90 using System; | |||
|
2 | 2 | using System.Diagnostics; |
|
3 | 3 | |
|
4 | 4 | namespace Implab { |
|
5 |
class PromiseActionReaction<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) |
|
|
13 |
m_next = ne |
|
|
14 |
|
|
|
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 ( |
|
|
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 |
@@ -8,97 +8,145 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 = |
|
|
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 = |
|
|
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 = |
|
|
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 = |
|
|
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 = |
|
|
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 = |
|
|
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 = |
|
|
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 = |
|
|
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 = |
|
|
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 = |
|
|
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 = |
|
|
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 = |
|
|
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 = |
|
|
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 = |
|
|
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 = |
|
|
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 = |
|
|
149 | var reaction = PromiseFuncReaction<Tin, Tout>.Create(fulfilled, rejected, Promise.DefaultDispatcher); | |
|
102 | 150 | that.Then(reaction); |
|
103 | 151 | return reaction.Promise; |
|
104 | 152 | } |
@@ -126,6 +174,44 namespace Implab { | |||
|
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 |
@@ -2,55 +2,90 using System; | |||
|
2 | 2 | using System.Diagnostics; |
|
3 | 3 | |
|
4 | 4 | namespace Implab { |
|
5 |
class PromiseFuncReaction<TRet> : |
|
|
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( |
|
|
13 |
m_next = ne |
|
|
14 |
|
|
|
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 ( |
|
|
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) |
|
|
40 | m_next = new Deferred<TRet>(dispatcher); | |
|
41 | if (fulfilled != null) | |
|
42 |
|
|
|
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 |
@@ -2,55 +2,96 using System; | |||
|
2 | 2 | using System.Diagnostics; |
|
3 | 3 | |
|
4 | 4 | namespace Implab { |
|
5 |
class PromiseFuncReaction<TIn, TRet> : |
|
|
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( |
|
|
13 |
m_next = ne |
|
|
14 |
|
|
|
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 ( |
|
|
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, |
|
|
40 | m_next = new Deferred<TRet>(dispatcher); | |
|
41 | if (fulfilled != null) | |
|
42 |
|
|
|
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 |
@@ -3,10 +3,10 using System.Diagnostics; | |||
|
3 | 3 | |
|
4 | 4 | namespace Implab { |
|
5 | 5 | class PromiseHandler { |
|
6 |
public static Action<T> Create<T>(Action<T> handler |
|
|
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(); |
@@ -16,10 +16,10 namespace Implab { | |||
|
16 | 16 | }; |
|
17 | 17 | } |
|
18 | 18 | |
|
19 |
public static Action<T> Create<T>(Func<T, IPromise> handler |
|
|
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) { |
@@ -28,10 +28,10 namespace Implab { | |||
|
28 | 28 | }; |
|
29 | 29 | } |
|
30 | 30 | |
|
31 |
public static Action<T> Create<T, T2>(Func<T, T2> handler |
|
|
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) { |
@@ -40,9 +40,9 namespace Implab { | |||
|
40 | 40 | }; |
|
41 | 41 | } |
|
42 | 42 | |
|
43 |
public static Action<T> Create<T, T2>(Func<T, IPromise<T2>> handler |
|
|
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) { |
@@ -51,10 +51,10 namespace Implab { | |||
|
51 | 51 | }; |
|
52 | 52 | } |
|
53 | 53 | |
|
54 |
public static Action Create(Action handler |
|
|
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(); |
@@ -64,10 +64,10 namespace Implab { | |||
|
64 | 64 | }; |
|
65 | 65 | } |
|
66 | 66 | |
|
67 |
public static Action Create(Func<IPromise> handler |
|
|
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) { |
@@ -76,10 +76,10 namespace Implab { | |||
|
76 | 76 | }; |
|
77 | 77 | } |
|
78 | 78 | |
|
79 |
public static Action Create<T2>(Func<T2> handler |
|
|
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) { |
@@ -88,9 +88,9 namespace Implab { | |||
|
88 | 88 | }; |
|
89 | 89 | } |
|
90 | 90 | |
|
91 |
public static Action Create<T2>(Func<IPromise<T2>> handler |
|
|
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) { |
@@ -24,11 +24,11 namespace Implab | |||
|
24 | 24 | } |
|
25 | 25 | |
|
26 | 26 | public void Join() { |
|
27 |
m_reason. |
|
|
27 | throw m_reason.Wrap(); | |
|
28 | 28 | } |
|
29 | 29 | |
|
30 | 30 | public void Join(int timeout) { |
|
31 |
m_reason. |
|
|
31 | throw m_reason.Wrap(); | |
|
32 | 32 | } |
|
33 | 33 | |
|
34 | 34 | public void Then(IResolvable next) { |
@@ -24,21 +24,19 namespace Implab | |||
|
24 | 24 | } |
|
25 | 25 | |
|
26 | 26 | void IPromise.Join() { |
|
27 |
m_reason. |
|
|
27 | throw m_reason.Wrap(); | |
|
28 | 28 | } |
|
29 | 29 | |
|
30 | 30 | void IPromise.Join(int timeout) { |
|
31 |
m_reason. |
|
|
31 | throw m_reason.Wrap(); | |
|
32 | 32 | } |
|
33 | 33 | |
|
34 | 34 | public T Join() { |
|
35 |
m_reason. |
|
|
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. |
|
|
41 | throw new Exception(); // unreachable code | |
|
39 | throw m_reason.Wrap(); | |
|
42 | 40 | } |
|
43 | 41 | |
|
44 | 42 | public void Then(IResolvable next) { |
|
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
ok, latest stable version should be in default
You need to be logged in to leave comments.
Login now