@@ -0,0 +1,19 | |||
|
1 | using System; | |
|
2 | ||
|
3 | namespace Implab { | |
|
4 | [Flags] | |
|
5 | public enum PromiseEventType { | |
|
6 | Success = 1, | |
|
7 | Error = 2, | |
|
8 | Cancelled = 4, | |
|
9 | /// <summary> | |
|
10 | /// ΠΠ°Π²Π΅ΡΡΠ΅Π½ΠΎ ΡΡΠΏΠ΅ΡΠ½ΠΎ, Π»ΠΈΠ±ΠΎ Π²ΠΎΠ·Π½ΠΈΠΊΠ»Π° ΠΎΡΠΈΠ±ΠΊΠ°, | |
|
11 | /// </summary> | |
|
12 | All = 7, | |
|
13 | /// <summary> | |
|
14 | /// ΠΠ°Π²ΡΠ΅ΡΠ΅Π½ΠΎ ΡΡΠΏΠ΅ΡΠ½ΠΎ, Π»ΠΈΠ±ΠΎ Π²ΠΎΠ·Π½ΠΈΠΊΠ»Π° ΠΎΡΠΈΠ±ΠΊΠ°. | |
|
15 | /// </summary> | |
|
16 | Complete = 3 | |
|
17 | } | |
|
18 | } | |
|
19 |
@@ -1,41 +1,41 | |||
|
1 | 1 | using System; |
|
2 | 2 | using System.Windows.Forms; |
|
3 | 3 | using System.Threading; |
|
4 | 4 | |
|
5 | 5 | namespace Implab.Fx |
|
6 | 6 | { |
|
7 | 7 | public static class PromiseHelpers |
|
8 | 8 | { |
|
9 | 9 | /// <summary> |
|
10 | 10 | /// ΠΠ΅ΡΠ΅Π½Π°ΠΏΡΠ°Π²Π»ΡΠ΅Ρ ΠΎΠ±ΡΠ°Π±ΠΎΡΠΊΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ Π² ΠΏΠΎΡΠΎΠΊ ΡΠΊΠ°Π·Π°Π½Π½ΠΎΠ³ΠΎ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ° ΡΠΏΡΠ°Π²Π»Π΅Π½ΠΈΡ. |
|
11 | 11 | /// </summary> |
|
12 | 12 | /// <typeparam name="T">Π’ΠΈΠΏ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ° ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ</typeparam> |
|
13 | 13 | /// <param name="that">ΠΡΡ ΠΎΠ΄Π½ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅</param> |
|
14 | 14 | /// <param name="ctl">ΠΠ»Π΅ΠΌΠ΅Π½Ρ ΡΠΏΡΠ°Π²Π»Π΅Π½ΠΈΡ</param> |
|
15 | 15 | /// <returns>ΠΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ ΠΊΠΎΡΠΎΡΠΎΠ³ΠΎ Π±ΡΠ΄ΡΡ Π²ΡΠΏΠΎΠ»Π½Π΅Π½Ρ Π² ΠΏΠΎΡΠΎΠΊΠ΅ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ° ΡΠΏΡΠ°Π²Π»Π΅Π½ΠΈΡ.</returns> |
|
16 | 16 | /// <exception cref="ArgumentNullException">ΠΠ°ΡΠ°ΠΌΠ΅ΡΡ Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ <c>null</c>.</exception> |
|
17 | 17 | /// <example> |
|
18 | 18 | /// client |
|
19 | 19 | /// .Get("description.txt") // returns a promise |
|
20 | 20 | /// .DispatchToControl(m_ctl) // handle the promise in the thread of the control |
|
21 | 21 | /// .Then( |
|
22 | 22 | /// description => m_ctl.Text = description // now it's safe |
|
23 | 23 | /// ) |
|
24 | 24 | /// </example> |
|
25 | 25 | public static IPromise<T> DispatchToControl<T>(this IPromise<T> that, Control ctl) |
|
26 | 26 | { |
|
27 | 27 | Safe.ArgumentNotNull(that, "that"); |
|
28 | 28 | Safe.ArgumentNotNull(ctl, "ctl"); |
|
29 | 29 | |
|
30 | 30 | var directed = new ControlBoundPromise<T>(ctl,that); |
|
31 | 31 | |
|
32 |
that. |
|
|
32 | that.On( | |
|
33 | 33 | directed.Resolve, |
|
34 | 34 | directed.Reject, |
|
35 | 35 | directed.Cancel |
|
36 | 36 | ); |
|
37 | 37 | |
|
38 | 38 | return directed; |
|
39 | 39 | } |
|
40 | 40 | } |
|
41 | 41 | } |
@@ -1,86 +1,87 | |||
|
1 | 1 | using System; |
|
2 | 2 | using System.Collections.Generic; |
|
3 | 3 | using System.Linq; |
|
4 | 4 | using System.Text; |
|
5 | 5 | |
|
6 | 6 | namespace Implab { |
|
7 | 7 | public interface IPromise: ICancellable { |
|
8 | 8 | /// <summary> |
|
9 | 9 | /// Check whereather the promise has no more than one dependent promise. |
|
10 | 10 | /// </summary> |
|
11 | 11 | bool IsExclusive { |
|
12 | 12 | get; |
|
13 | 13 | } |
|
14 | 14 | |
|
15 | 15 | /// <summary> |
|
16 | 16 | /// Π’ΠΈΠΏ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ°, ΠΏΠΎΠ»ΡΡΠ°Π΅ΠΌΠΎΠ³ΠΎ ΡΠ΅ΡΠ΅Π· Π΄Π°Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅. |
|
17 | 17 | /// </summary> |
|
18 | 18 | Type PromiseType { get; } |
|
19 | 19 | |
|
20 | 20 | /// <summary> |
|
21 | 21 | /// ΠΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΡΠ²Π»ΡΠ΅ΡΡΡ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΡΠΌ, Π»ΠΈΠ±ΠΎ ΡΡΠΏΠ΅ΡΠ½ΠΎ, Π»ΠΈΠ±ΠΎ Ρ ΠΎΡΠΈΠ±ΠΊΠΎΠΉ, Π»ΠΈΠ±ΠΎ ΠΎΡΠΌΠ΅Π½Π΅Π½ΠΎ. |
|
22 | 22 | /// </summary> |
|
23 | 23 | bool IsResolved { get; } |
|
24 | 24 | |
|
25 | 25 | /// <summary> |
|
26 | 26 | /// ΠΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π±ΡΠ»ΠΎ ΠΎΡΠΌΠ΅Π½Π΅Π½ΠΎ. |
|
27 | 27 | /// </summary> |
|
28 | 28 | bool IsCancelled { get; } |
|
29 | 29 | |
|
30 | 30 | IPromise Then(Action success, Action<Exception> error, Action cancel); |
|
31 | 31 | IPromise Then(Action success, Action<Exception> error); |
|
32 | 32 | IPromise Then(Action success); |
|
33 | 33 | |
|
34 | 34 | IPromise Chain(Func<IPromise> chained, Func<Exception, IPromise> error, Action cancel); |
|
35 | 35 | IPromise Chain(Func<IPromise> chained, Func<Exception, IPromise> error); |
|
36 | 36 | IPromise Chain(Func<IPromise> chained); |
|
37 | 37 | |
|
38 | 38 | /// <summary> |
|
39 | 39 | /// ΠΠΎΠ±Π°Π²Π»ΡΠ΅Ρ ΠΏΠΎΡΠ»Π΅Π΄Π½ΠΉ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ Π² ΡΠ΅ΠΏΠΎΡΠΊΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ, Π½Π΅ ΡΠΎΠ·Π΄Π°Π΅Ρ ΠΏΡΠΎΠΌΠ΅ΠΆΡΡΠΎΡΠ½ΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ. |
|
40 | 40 | /// </summary> |
|
41 | 41 | /// <param name="success">Success.</param> |
|
42 | 42 | /// <param name="error">Error.</param> |
|
43 | 43 | /// <param name="cancel">Cancel.</param> |
|
44 |
void |
|
|
45 |
void |
|
|
46 |
void |
|
|
44 | void On(Action success, Action<Exception> error, Action cancel); | |
|
45 | void On(Action success, Action<Exception> error); | |
|
46 | void On(Action success); | |
|
47 | void On(Action success, PromiseEventType events); | |
|
47 | 48 | |
|
48 | 49 | IPromise Error(Action<Exception> error); |
|
49 | 50 | /// <summary> |
|
50 | 51 | /// ΠΠ±ΡΠ°Π±Π°ΡΡΠ²Π°Π΅Ρ Π»ΠΈΠ±ΠΎ ΠΎΡΠΈΠ±ΠΊΡ, Π»ΠΈΠ±ΠΎ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ, Π»ΠΈΠ±ΠΎ ΠΎΡΠΌΠ΅Π½Ρ. |
|
51 | 52 | /// </summary> |
|
52 | 53 | /// <param name="handler">ΠΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ.</param> |
|
53 | 54 | /// <remarks>ΠΠΎΡΠ»Π΅ ΠΎΠ±ΡΠ°Π±ΠΎΡΠΊΠ΅ ΠΎΡΠΈΠ±ΠΊΠΈ, ΠΎΠ½Π° ΠΏΠ΅ΡΠ΅Π΄Π°Π΅ΡΡΡ Π΄Π°Π»ΡΡΠ΅.</remarks> |
|
54 | 55 | /// <summary> |
|
55 | 56 | /// ΠΠ±ΡΠ°Π±Π°ΡΡΠ²Π°Π΅Ρ Π»ΠΈΠ±ΠΎ ΠΎΡΠΈΠ±ΠΊΡ, Π»ΠΈΠ±ΠΎ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ, Π»ΠΈΠ±ΠΎ ΠΎΡΠΌΠ΅Π½Ρ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ. |
|
56 | 57 | /// </summary> |
|
57 | 58 | /// <param name="handler">ΠΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ.</param> |
|
58 | 59 | /// <remarks>ΠΠΎΡΠ»Π΅ ΠΎΠ±ΡΠ°Π±ΠΎΡΠΊΠ΅ ΠΎΡΠΈΠ±ΠΊΠΈ, ΠΎΠ½Π° ΠΏΠ΅ΡΠ΅Π΄Π°Π΅ΡΡΡ Π΄Π°Π»ΡΡΠ΅.</remarks> |
|
59 | 60 | IPromise Anyway(Action handler); |
|
60 | 61 | /// <summary> |
|
61 | 62 | /// ΠΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ Π΄Π»Ρ ΡΠ΅Π³ΠΈΡΡΡΠ°ΡΠΈΠΈ ΠΎΡΠΌΠ΅Π½Ρ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ. |
|
62 | 63 | /// </summary> |
|
63 | 64 | /// <returns>ΠΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΡΠ²ΡΠ·Π°Π½Π½ΠΎΠ΅ Ρ ΡΠ΅ΠΊΡΡΠΈΠΌ, Π²ΡΠΏΠΎΠ»Π½ΠΈΡΡΡ ΠΏΠΎΡΠ»Π΅ ΡΠΊΠ°Π·Π°Π½Π½ΠΎΠ³ΠΎ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠ°.</returns> |
|
64 | 65 | /// <param name="handler">ΠΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΡΠΎΠ±ΡΡΠΈΡ.</param> |
|
65 | 66 | /// <remarks>ΠΡΠ»ΠΈ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ Π²ΡΠ·ΡΠ²Π°Π΅Ρ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅, ΡΠΎ ΠΎΠ½ΠΎ ΠΏΠ΅ΡΠ΅Π΄Π°Π΅ΡΡΡ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΡ ΠΎΡΠΈΠ±ΠΊΠΈ, ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ ΡΠ°Π±ΠΎΡΡ |
|
66 | 67 | /// ΠΊΠΎΡΠΎΡΠΎΠ³ΠΎ Π±ΡΠ΄Π΅Ρ ΠΏΠ΅ΡΠ΅Π΄Π°Π½ ΡΠ²ΡΠ·Π°Π½Π½ΠΎΠΌΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ</remarks> |
|
67 | 68 | IPromise Cancelled(Action handler); |
|
68 | 69 | |
|
69 | 70 | /// <summary> |
|
70 | 71 | /// ΠΡΠ΅ΠΎΠ±ΡΠ°Π·ΡΠ΅Ρ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΊ Π·Π°Π΄Π°Π½Π½ΠΎΠΌΡ ΡΠΈΠΏΡ ΠΈ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅Ρ Π½ΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅. |
|
71 | 72 | /// </summary> |
|
72 | 73 | IPromise<T> Cast<T>(); |
|
73 | 74 | |
|
74 | 75 | /// <summary> |
|
75 | 76 | /// Π‘ΠΈΠ½Ρ ΡΠΎΠ½ΠΈΠ·ΠΈΡΡΠ΅Ρ ΡΠ΅ΠΊΡΡΠΈΠΉ ΠΏΠΎΡΠΎΠΊ Ρ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ΠΌ. |
|
76 | 77 | /// </summary> |
|
77 | 78 | void Join(); |
|
78 | 79 | /// <summary> |
|
79 | 80 | /// Π‘ΠΈΠ½Ρ ΡΠΎΠ½ΠΈΠ·ΠΈΡΡΠ΅Ρ ΡΠ΅ΠΊΡΡΠΈΠΉ ΠΏΠΎΡΠΎΠΊ Ρ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ΠΌ. |
|
80 | 81 | /// </summary> |
|
81 | 82 | /// <param name="timeout">ΠΡΠ΅ΠΌΡ ΠΎΠΆΠΈΠ΄Π°Π½ΠΈΡ, ΠΏΠΎ Π΅Π³ΠΎ ΠΈΡΡΠ΅ΡΠ΅Π½ΠΈΡ Π²ΠΎΠ·Π½ΠΈΠΊΠ½Π΅Ρ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅.</param> |
|
82 | 83 | /// <exception cref="TimeoutException">ΠΡΠ΅Π²ΡΡΠ΅Π½ΠΎ Π²ΡΠ΅ΠΌΡ ΠΎΠΆΠΈΠ΄Π°Π½ΠΈΡ.</exception> |
|
83 | 84 | void Join(int timeout); |
|
84 | 85 | |
|
85 | 86 | } |
|
86 | 87 | } |
@@ -1,40 +1,40 | |||
|
1 | 1 | using System; |
|
2 | 2 | |
|
3 | 3 | namespace Implab { |
|
4 | 4 | public interface IPromise<T> : IPromise { |
|
5 | 5 | |
|
6 | 6 | new T Join(); |
|
7 | 7 | |
|
8 | 8 | new T Join(int timeout); |
|
9 | 9 | |
|
10 |
void |
|
|
10 | void On(Action<T> success, Action<Exception> error, Action cancel); | |
|
11 | 11 | |
|
12 |
void |
|
|
12 | void On(Action<T> success, Action<Exception> error); | |
|
13 | 13 | |
|
14 |
void |
|
|
14 | void On(Action<T> success); | |
|
15 | 15 | |
|
16 | 16 | IPromise<T> Then(Action<T> success, Func<Exception,T> error, Action cancel); |
|
17 | 17 | |
|
18 | 18 | IPromise<T> Then(Action<T> success, Func<Exception,T> error); |
|
19 | 19 | |
|
20 | 20 | IPromise<T> Then(Action<T> success); |
|
21 | 21 | |
|
22 | 22 | IPromise<T2> Then<T2>(Func<T, T2> mapper, Func<Exception,T2> error, Action cancel); |
|
23 | 23 | |
|
24 | 24 | IPromise<T2> Then<T2>(Func<T, T2> mapper, Func<Exception,T2> error); |
|
25 | 25 | |
|
26 | 26 | IPromise<T2> Then<T2>(Func<T, T2> mapper); |
|
27 | 27 | |
|
28 | 28 | IPromise<T2> Chain<T2>(Func<T, IPromise<T2>> chained, Func<Exception,IPromise<T2>> error, Action cancel); |
|
29 | 29 | |
|
30 | 30 | IPromise<T2> Chain<T2>(Func<T, IPromise<T2>> chained, Func<Exception,IPromise<T2>> error); |
|
31 | 31 | |
|
32 | 32 | IPromise<T2> Chain<T2>(Func<T, IPromise<T2>> chained); |
|
33 | 33 | |
|
34 | 34 | IPromise<T> Error(Func<Exception,T> error); |
|
35 | 35 | |
|
36 | 36 | new IPromise<T> Cancelled(Action handler); |
|
37 | 37 | |
|
38 | 38 | new IPromise<T> Anyway(Action handler); |
|
39 | 39 | } |
|
40 | 40 | } |
@@ -1,222 +1,223 | |||
|
1 | 1 | ο»Ώ<?xml version="1.0" encoding="utf-8"?> |
|
2 | 2 | <Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> |
|
3 | 3 | <PropertyGroup> |
|
4 | 4 | <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> |
|
5 | 5 | <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> |
|
6 | 6 | <ProjectGuid>{F550F1F8-8746-4AD0-9614-855F4C4B7F05}</ProjectGuid> |
|
7 | 7 | <OutputType>Library</OutputType> |
|
8 | 8 | <RootNamespace>Implab</RootNamespace> |
|
9 | 9 | <AssemblyName>Implab</AssemblyName> |
|
10 | 10 | </PropertyGroup> |
|
11 | 11 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> |
|
12 | 12 | <DebugSymbols>true</DebugSymbols> |
|
13 | 13 | <DebugType>full</DebugType> |
|
14 | 14 | <Optimize>false</Optimize> |
|
15 | 15 | <OutputPath>bin\Debug</OutputPath> |
|
16 | 16 | <DefineConstants>TRACE;DEBUG;</DefineConstants> |
|
17 | 17 | <ErrorReport>prompt</ErrorReport> |
|
18 | 18 | <WarningLevel>4</WarningLevel> |
|
19 | 19 | <ConsolePause>false</ConsolePause> |
|
20 | 20 | <RunCodeAnalysis>true</RunCodeAnalysis> |
|
21 | 21 | </PropertyGroup> |
|
22 | 22 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> |
|
23 | 23 | <DebugType>full</DebugType> |
|
24 | 24 | <Optimize>true</Optimize> |
|
25 | 25 | <OutputPath>bin\Release</OutputPath> |
|
26 | 26 | <ErrorReport>prompt</ErrorReport> |
|
27 | 27 | <WarningLevel>4</WarningLevel> |
|
28 | 28 | <ConsolePause>false</ConsolePause> |
|
29 | 29 | </PropertyGroup> |
|
30 | 30 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug 4.5|AnyCPU' "> |
|
31 | 31 | <DebugSymbols>true</DebugSymbols> |
|
32 | 32 | <DebugType>full</DebugType> |
|
33 | 33 | <Optimize>false</Optimize> |
|
34 | 34 | <OutputPath>bin\Debug</OutputPath> |
|
35 | 35 | <DefineConstants>TRACE;DEBUG;NET_4_5</DefineConstants> |
|
36 | 36 | <ErrorReport>prompt</ErrorReport> |
|
37 | 37 | <WarningLevel>4</WarningLevel> |
|
38 | 38 | <RunCodeAnalysis>true</RunCodeAnalysis> |
|
39 | 39 | <ConsolePause>false</ConsolePause> |
|
40 | 40 | </PropertyGroup> |
|
41 | 41 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release 4.5|AnyCPU' "> |
|
42 | 42 | <Optimize>true</Optimize> |
|
43 | 43 | <OutputPath>bin\Release</OutputPath> |
|
44 | 44 | <ErrorReport>prompt</ErrorReport> |
|
45 | 45 | <WarningLevel>4</WarningLevel> |
|
46 | 46 | <ConsolePause>false</ConsolePause> |
|
47 | 47 | <DefineConstants>NET_4_5</DefineConstants> |
|
48 | 48 | </PropertyGroup> |
|
49 | 49 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'DebugMono|AnyCPU' "> |
|
50 | 50 | <DebugSymbols>true</DebugSymbols> |
|
51 | 51 | <DebugType>full</DebugType> |
|
52 | 52 | <Optimize>false</Optimize> |
|
53 | 53 | <OutputPath>bin\Debug</OutputPath> |
|
54 | 54 | <DefineConstants>TRACE;DEBUG;NET_4_5;MONO</DefineConstants> |
|
55 | 55 | <ErrorReport>prompt</ErrorReport> |
|
56 | 56 | <WarningLevel>4</WarningLevel> |
|
57 | 57 | <RunCodeAnalysis>true</RunCodeAnalysis> |
|
58 | 58 | <ConsolePause>false</ConsolePause> |
|
59 | 59 | </PropertyGroup> |
|
60 | 60 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'ReleaseMono|AnyCPU' "> |
|
61 | 61 | <Optimize>true</Optimize> |
|
62 | 62 | <OutputPath>bin\Release</OutputPath> |
|
63 | 63 | <DefineConstants>NET_4_5;MONO;</DefineConstants> |
|
64 | 64 | <ErrorReport>prompt</ErrorReport> |
|
65 | 65 | <WarningLevel>4</WarningLevel> |
|
66 | 66 | <ConsolePause>false</ConsolePause> |
|
67 | 67 | </PropertyGroup> |
|
68 | 68 | <ItemGroup> |
|
69 | 69 | <Reference Include="System" /> |
|
70 | 70 | <Reference Include="System.Xml" /> |
|
71 | 71 | </ItemGroup> |
|
72 | 72 | <ItemGroup> |
|
73 | 73 | <Compile Include="Component.cs" /> |
|
74 | 74 | <Compile Include="CustomEqualityComparer.cs" /> |
|
75 | 75 | <Compile Include="Diagnostics\ConsoleTraceListener.cs" /> |
|
76 | 76 | <Compile Include="Diagnostics\EventText.cs" /> |
|
77 | 77 | <Compile Include="Diagnostics\IEventTextFormatter.cs" /> |
|
78 | 78 | <Compile Include="Diagnostics\LogChannel.cs" /> |
|
79 | 79 | <Compile Include="Diagnostics\LogicalOperation.cs" /> |
|
80 | 80 | <Compile Include="Diagnostics\TextFileListener.cs" /> |
|
81 | 81 | <Compile Include="Diagnostics\TextListenerBase.cs" /> |
|
82 | 82 | <Compile Include="Diagnostics\TraceLog.cs" /> |
|
83 | 83 | <Compile Include="Diagnostics\TraceEvent.cs" /> |
|
84 | 84 | <Compile Include="Diagnostics\TraceEventType.cs" /> |
|
85 | 85 | <Compile Include="Disposable.cs" /> |
|
86 | 86 | <Compile Include="ICancellable.cs" /> |
|
87 | 87 | <Compile Include="IProgressHandler.cs" /> |
|
88 | 88 | <Compile Include="IProgressNotifier.cs" /> |
|
89 | 89 | <Compile Include="IPromiseT.cs" /> |
|
90 | 90 | <Compile Include="IPromise.cs" /> |
|
91 | 91 | <Compile Include="IServiceLocator.cs" /> |
|
92 | 92 | <Compile Include="ITaskController.cs" /> |
|
93 | 93 | <Compile Include="JSON\JSONElementContext.cs" /> |
|
94 | 94 | <Compile Include="JSON\JSONElementType.cs" /> |
|
95 | 95 | <Compile Include="JSON\JSONGrammar.cs" /> |
|
96 | 96 | <Compile Include="JSON\JSONParser.cs" /> |
|
97 | 97 | <Compile Include="JSON\JSONScanner.cs" /> |
|
98 | 98 | <Compile Include="JSON\JsonTokenType.cs" /> |
|
99 | 99 | <Compile Include="JSON\JSONWriter.cs" /> |
|
100 | 100 | <Compile Include="JSON\JSONXmlReader.cs" /> |
|
101 | 101 | <Compile Include="JSON\JSONXmlReaderOptions.cs" /> |
|
102 | 102 | <Compile Include="JSON\StringTranslator.cs" /> |
|
103 | 103 | <Compile Include="Parallels\DispatchPool.cs" /> |
|
104 | 104 | <Compile Include="Parallels\ArrayTraits.cs" /> |
|
105 | 105 | <Compile Include="Parallels\MTQueue.cs" /> |
|
106 | 106 | <Compile Include="Parallels\WorkerPool.cs" /> |
|
107 | 107 | <Compile Include="Parsing\Alphabet.cs" /> |
|
108 | 108 | <Compile Include="Parsing\AlphabetBase.cs" /> |
|
109 | 109 | <Compile Include="Parsing\AltToken.cs" /> |
|
110 | 110 | <Compile Include="Parsing\BinaryToken.cs" /> |
|
111 | 111 | <Compile Include="Parsing\CatToken.cs" /> |
|
112 | 112 | <Compile Include="Parsing\CDFADefinition.cs" /> |
|
113 | 113 | <Compile Include="Parsing\DFABuilder.cs" /> |
|
114 | 114 | <Compile Include="Parsing\DFADefinitionBase.cs" /> |
|
115 | 115 | <Compile Include="Parsing\DFAStateDescriptor.cs" /> |
|
116 | 116 | <Compile Include="Parsing\DFAutomaton.cs" /> |
|
117 | 117 | <Compile Include="Parsing\EDFADefinition.cs" /> |
|
118 | 118 | <Compile Include="Parsing\EmptyToken.cs" /> |
|
119 | 119 | <Compile Include="Parsing\EndToken.cs" /> |
|
120 | 120 | <Compile Include="Parsing\EnumAlphabet.cs" /> |
|
121 | 121 | <Compile Include="Parsing\Grammar.cs" /> |
|
122 | 122 | <Compile Include="Parsing\IAlphabet.cs" /> |
|
123 | 123 | <Compile Include="Parsing\IDFADefinition.cs" /> |
|
124 | 124 | <Compile Include="Parsing\IVisitor.cs" /> |
|
125 | 125 | <Compile Include="Parsing\ParserException.cs" /> |
|
126 | 126 | <Compile Include="Parsing\Scanner.cs" /> |
|
127 | 127 | <Compile Include="Parsing\StarToken.cs" /> |
|
128 | 128 | <Compile Include="Parsing\SymbolToken.cs" /> |
|
129 | 129 | <Compile Include="Parsing\Token.cs" /> |
|
130 | 130 | <Compile Include="SafePool.cs" /> |
|
131 | 131 | <Compile Include="ServiceLocator.cs" /> |
|
132 | 132 | <Compile Include="TaskController.cs" /> |
|
133 | 133 | <Compile Include="ProgressInitEventArgs.cs" /> |
|
134 | 134 | <Compile Include="Properties\AssemblyInfo.cs" /> |
|
135 | 135 | <Compile Include="Promise.cs" /> |
|
136 | 136 | <Compile Include="Parallels\AsyncPool.cs" /> |
|
137 | 137 | <Compile Include="Safe.cs" /> |
|
138 | 138 | <Compile Include="ValueEventArgs.cs" /> |
|
139 | 139 | <Compile Include="PromiseExtensions.cs" /> |
|
140 | 140 | <Compile Include="TransientPromiseException.cs" /> |
|
141 | 141 | <Compile Include="SyncContextPromise.cs" /> |
|
142 | 142 | <Compile Include="ObjectPool.cs" /> |
|
143 | 143 | <Compile Include="Diagnostics\OperationContext.cs" /> |
|
144 | 144 | <Compile Include="Diagnostics\TraceContext.cs" /> |
|
145 | 145 | <Compile Include="Diagnostics\LogEventArgs.cs" /> |
|
146 | 146 | <Compile Include="Diagnostics\LogEventArgsT.cs" /> |
|
147 | 147 | <Compile Include="Diagnostics\Extensions.cs" /> |
|
148 | 148 | <Compile Include="IComponentContainer.cs" /> |
|
149 | 149 | <Compile Include="MTComponentContainer.cs" /> |
|
150 | <Compile Include="PromiseEventType.cs" /> | |
|
150 | 151 | </ItemGroup> |
|
151 | 152 | <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> |
|
152 | 153 | <ItemGroup /> |
|
153 | 154 | <ProjectExtensions> |
|
154 | 155 | <MonoDevelop> |
|
155 | 156 | <Properties> |
|
156 | 157 | <Policies> |
|
157 | 158 | <CSharpFormattingPolicy IndentSwitchBody="True" NamespaceBraceStyle="EndOfLine" ClassBraceStyle="EndOfLine" InterfaceBraceStyle="EndOfLine" StructBraceStyle="EndOfLine" EnumBraceStyle="EndOfLine" MethodBraceStyle="EndOfLine" ConstructorBraceStyle="EndOfLine" DestructorBraceStyle="EndOfLine" BeforeMethodDeclarationParentheses="False" BeforeMethodCallParentheses="False" BeforeConstructorDeclarationParentheses="False" NewLineBeforeConstructorInitializerColon="NewLine" NewLineAfterConstructorInitializerColon="SameLine" BeforeIndexerDeclarationBracket="False" BeforeDelegateDeclarationParentheses="False" NewParentheses="False" SpacesBeforeBrackets="False" inheritsSet="Mono" inheritsScope="text/x-csharp" scope="text/x-csharp" /> |
|
158 | 159 | <TextStylePolicy FileWidth="120" EolMarker="Unix" inheritsSet="VisualStudio" inheritsScope="text/plain" scope="text/x-csharp" /> |
|
159 | 160 | <DotNetNamingPolicy DirectoryNamespaceAssociation="PrefixedHierarchical" ResourceNamePolicy="MSBuild" /> |
|
160 | 161 | <TextStylePolicy FileWidth="120" TabsToSpaces="False" inheritsSet="VisualStudio" inheritsScope="text/plain" scope="application/xml" /> |
|
161 | 162 | <XmlFormattingPolicy inheritsSet="Mono" inheritsScope="application/xml" scope="application/xml" /> |
|
162 | 163 | <TextStylePolicy FileWidth="120" TabsToSpaces="False" inheritsSet="VisualStudio" inheritsScope="text/plain" scope="text/plain" /> |
|
163 | 164 | <NameConventionPolicy> |
|
164 | 165 | <Rules> |
|
165 | 166 | <NamingRule Name="Namespaces" AffectedEntity="Namespace" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
166 | 167 | <NamingRule Name="Types" AffectedEntity="Class, Struct, Enum, Delegate" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
167 | 168 | <NamingRule Name="Interfaces" AffectedEntity="Interface" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True"> |
|
168 | 169 | <RequiredPrefixes> |
|
169 | 170 | <String>I</String> |
|
170 | 171 | </RequiredPrefixes> |
|
171 | 172 | </NamingRule> |
|
172 | 173 | <NamingRule Name="Attributes" AffectedEntity="CustomAttributes" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True"> |
|
173 | 174 | <RequiredSuffixes> |
|
174 | 175 | <String>Attribute</String> |
|
175 | 176 | </RequiredSuffixes> |
|
176 | 177 | </NamingRule> |
|
177 | 178 | <NamingRule Name="Event Arguments" AffectedEntity="CustomEventArgs" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True"> |
|
178 | 179 | <RequiredSuffixes> |
|
179 | 180 | <String>EventArgs</String> |
|
180 | 181 | </RequiredSuffixes> |
|
181 | 182 | </NamingRule> |
|
182 | 183 | <NamingRule Name="Exceptions" AffectedEntity="CustomExceptions" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True"> |
|
183 | 184 | <RequiredSuffixes> |
|
184 | 185 | <String>Exception</String> |
|
185 | 186 | </RequiredSuffixes> |
|
186 | 187 | </NamingRule> |
|
187 | 188 | <NamingRule Name="Methods" AffectedEntity="Methods" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
188 | 189 | <NamingRule Name="Static Readonly Fields" AffectedEntity="ReadonlyField" VisibilityMask="Internal, Protected, Public" NamingStyle="CamelCase" IncludeInstanceMembers="False" IncludeStaticEntities="True" /> |
|
189 | 190 | <NamingRule Name="Fields (Non Private)" AffectedEntity="Field" VisibilityMask="Internal, Public" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
190 | 191 | <NamingRule Name="ReadOnly Fields (Non Private)" AffectedEntity="ReadonlyField" VisibilityMask="Internal, Public" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="False" /> |
|
191 | 192 | <NamingRule Name="Fields (Private)" AffectedEntity="Field, ReadonlyField" VisibilityMask="Private, Protected" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="False"> |
|
192 | 193 | <RequiredPrefixes> |
|
193 | 194 | <String>m_</String> |
|
194 | 195 | </RequiredPrefixes> |
|
195 | 196 | </NamingRule> |
|
196 | 197 | <NamingRule Name="Static Fields (Private)" AffectedEntity="Field" VisibilityMask="Private" NamingStyle="CamelCase" IncludeInstanceMembers="False" IncludeStaticEntities="True"> |
|
197 | 198 | <RequiredPrefixes> |
|
198 | 199 | <String>_</String> |
|
199 | 200 | </RequiredPrefixes> |
|
200 | 201 | </NamingRule> |
|
201 | 202 | <NamingRule Name="ReadOnly Fields (Private)" AffectedEntity="ReadonlyField" VisibilityMask="Private, Protected" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="False"> |
|
202 | 203 | <RequiredPrefixes> |
|
203 | 204 | <String>m_</String> |
|
204 | 205 | </RequiredPrefixes> |
|
205 | 206 | </NamingRule> |
|
206 | 207 | <NamingRule Name="Constant Fields" AffectedEntity="ConstantField" VisibilityMask="VisibilityMask" NamingStyle="AllUpper" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
207 | 208 | <NamingRule Name="Properties" AffectedEntity="Property" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
208 | 209 | <NamingRule Name="Events" AffectedEntity="Event" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
209 | 210 | <NamingRule Name="Enum Members" AffectedEntity="EnumMember" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
210 | 211 | <NamingRule Name="Parameters" AffectedEntity="Parameter, LocalVariable" VisibilityMask="VisibilityMask" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
211 | 212 | <NamingRule Name="Type Parameters" AffectedEntity="TypeParameter" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True"> |
|
212 | 213 | <RequiredPrefixes> |
|
213 | 214 | <String>T</String> |
|
214 | 215 | </RequiredPrefixes> |
|
215 | 216 | </NamingRule> |
|
216 | 217 | </Rules> |
|
217 | 218 | </NameConventionPolicy> |
|
218 | 219 | </Policies> |
|
219 | 220 | </Properties> |
|
220 | 221 | </MonoDevelop> |
|
221 | 222 | </ProjectExtensions> |
|
222 | 223 | </Project> No newline at end of file |
@@ -1,207 +1,207 | |||
|
1 | 1 | using Implab.Diagnostics; |
|
2 | 2 | using System; |
|
3 | 3 | using System.Diagnostics; |
|
4 | 4 | using System.Threading; |
|
5 | 5 | |
|
6 | 6 | namespace Implab.Parallels { |
|
7 | 7 | public static class ArrayTraits { |
|
8 | 8 | class ArrayIterator<TSrc> : DispatchPool<int> { |
|
9 | 9 | readonly Action<TSrc> m_action; |
|
10 | 10 | readonly TSrc[] m_source; |
|
11 | 11 | readonly Promise<int> m_promise = new Promise<int>(); |
|
12 | 12 | readonly LogicalOperation m_logicalOperation; |
|
13 | 13 | |
|
14 | 14 | int m_pending; |
|
15 | 15 | int m_next; |
|
16 | 16 | |
|
17 | 17 | public ArrayIterator(TSrc[] source, Action<TSrc> action, int threads) |
|
18 | 18 | : base(threads) { |
|
19 | 19 | |
|
20 | 20 | Debug.Assert(source != null); |
|
21 | 21 | Debug.Assert(action != null); |
|
22 | 22 | |
|
23 | 23 | m_logicalOperation = TraceContext.Instance.CurrentOperation; |
|
24 | 24 | m_next = 0; |
|
25 | 25 | m_source = source; |
|
26 | 26 | m_pending = source.Length; |
|
27 | 27 | m_action = action; |
|
28 | 28 | |
|
29 | 29 | m_promise.Anyway(Dispose); |
|
30 | 30 | |
|
31 | 31 | InitPool(); |
|
32 | 32 | } |
|
33 | 33 | |
|
34 | 34 | public Promise<int> Promise { |
|
35 | 35 | get { |
|
36 | 36 | return m_promise; |
|
37 | 37 | } |
|
38 | 38 | } |
|
39 | 39 | |
|
40 | 40 | protected override void Worker() { |
|
41 | 41 | TraceContext.Instance.EnterLogicalOperation(m_logicalOperation, false); |
|
42 | 42 | try { |
|
43 | 43 | base.Worker(); |
|
44 | 44 | } finally { |
|
45 | 45 | TraceContext.Instance.Leave(); |
|
46 | 46 | } |
|
47 | 47 | } |
|
48 | 48 | |
|
49 | 49 | protected override bool TryDequeue(out int unit) { |
|
50 | 50 | unit = Interlocked.Increment(ref m_next) - 1; |
|
51 | 51 | return unit < m_source.Length; |
|
52 | 52 | } |
|
53 | 53 | |
|
54 | 54 | protected override void InvokeUnit(int unit) { |
|
55 | 55 | try { |
|
56 | 56 | m_action(m_source[unit]); |
|
57 | 57 | var pending = Interlocked.Decrement(ref m_pending); |
|
58 | 58 | if (pending == 0) |
|
59 | 59 | m_promise.Resolve(m_source.Length); |
|
60 | 60 | } catch (Exception e) { |
|
61 | 61 | m_promise.Reject(e); |
|
62 | 62 | } |
|
63 | 63 | } |
|
64 | 64 | } |
|
65 | 65 | |
|
66 | 66 | class ArrayMapper<TSrc, TDst>: DispatchPool<int> { |
|
67 | 67 | readonly Func<TSrc, TDst> m_transform; |
|
68 | 68 | readonly TSrc[] m_source; |
|
69 | 69 | readonly TDst[] m_dest; |
|
70 | 70 | readonly Promise<TDst[]> m_promise = new Promise<TDst[]>(); |
|
71 | 71 | readonly LogicalOperation m_logicalOperation; |
|
72 | 72 | |
|
73 | 73 | int m_pending; |
|
74 | 74 | int m_next; |
|
75 | 75 | |
|
76 | 76 | public ArrayMapper(TSrc[] source, Func<TSrc, TDst> transform, int threads) |
|
77 | 77 | : base(threads) { |
|
78 | 78 | |
|
79 | 79 | Debug.Assert (source != null); |
|
80 | 80 | Debug.Assert( transform != null); |
|
81 | 81 | |
|
82 | 82 | m_next = 0; |
|
83 | 83 | m_source = source; |
|
84 | 84 | m_dest = new TDst[source.Length]; |
|
85 | 85 | m_pending = source.Length; |
|
86 | 86 | m_transform = transform; |
|
87 | 87 | m_logicalOperation = TraceContext.Instance.CurrentOperation; |
|
88 | 88 | |
|
89 | 89 | m_promise.Anyway(Dispose); |
|
90 | 90 | |
|
91 | 91 | InitPool(); |
|
92 | 92 | } |
|
93 | 93 | |
|
94 | 94 | public Promise<TDst[]> Promise { |
|
95 | 95 | get { |
|
96 | 96 | return m_promise; |
|
97 | 97 | } |
|
98 | 98 | } |
|
99 | 99 | |
|
100 | 100 | protected override void Worker() { |
|
101 | 101 | TraceContext.Instance.EnterLogicalOperation(m_logicalOperation,false); |
|
102 | 102 | try { |
|
103 | 103 | base.Worker(); |
|
104 | 104 | } finally { |
|
105 | 105 | TraceContext.Instance.Leave(); |
|
106 | 106 | } |
|
107 | 107 | } |
|
108 | 108 | |
|
109 | 109 | protected override bool TryDequeue(out int unit) { |
|
110 | 110 | unit = Interlocked.Increment(ref m_next) - 1; |
|
111 | 111 | return unit < m_source.Length; |
|
112 | 112 | } |
|
113 | 113 | |
|
114 | 114 | protected override void InvokeUnit(int unit) { |
|
115 | 115 | try { |
|
116 | 116 | m_dest[unit] = m_transform(m_source[unit]); |
|
117 | 117 | var pending = Interlocked.Decrement(ref m_pending); |
|
118 | 118 | if (pending == 0) |
|
119 | 119 | m_promise.Resolve(m_dest); |
|
120 | 120 | } catch (Exception e) { |
|
121 | 121 | m_promise.Reject(e); |
|
122 | 122 | } |
|
123 | 123 | } |
|
124 | 124 | } |
|
125 | 125 | |
|
126 | 126 | public static IPromise<TDst[]> ParallelMap<TSrc, TDst> (this TSrc[] source, Func<TSrc,TDst> transform, int threads) { |
|
127 | 127 | if (source == null) |
|
128 | 128 | throw new ArgumentNullException("source"); |
|
129 | 129 | if (transform == null) |
|
130 | 130 | throw new ArgumentNullException("transform"); |
|
131 | 131 | |
|
132 | 132 | var mapper = new ArrayMapper<TSrc, TDst>(source, transform, threads); |
|
133 | 133 | return mapper.Promise; |
|
134 | 134 | } |
|
135 | 135 | |
|
136 | 136 | public static IPromise<int> ParallelForEach<TSrc>(this TSrc[] source, Action<TSrc> action, int threads) { |
|
137 | 137 | if (source == null) |
|
138 | 138 | throw new ArgumentNullException("source"); |
|
139 | 139 | if (action == null) |
|
140 | 140 | throw new ArgumentNullException("action"); |
|
141 | 141 | |
|
142 | 142 | var iter = new ArrayIterator<TSrc>(source, action, threads); |
|
143 | 143 | return iter.Promise; |
|
144 | 144 | } |
|
145 | 145 | |
|
146 | 146 | public static IPromise<TDst[]> ChainedMap<TSrc, TDst>(this TSrc[] source, Func<TSrc, IPromise<TDst>> transform, int threads) { |
|
147 | 147 | if (source == null) |
|
148 | 148 | throw new ArgumentNullException("source"); |
|
149 | 149 | if (transform == null) |
|
150 | 150 | throw new ArgumentNullException("transform"); |
|
151 | 151 | if (threads <= 0) |
|
152 | 152 | throw new ArgumentOutOfRangeException("threads","Threads number must be greater then zero"); |
|
153 | 153 | |
|
154 | 154 | if (source.Length == 0) |
|
155 | 155 | return Promise<TDst[]>.ResultToPromise(new TDst[0]); |
|
156 | 156 | |
|
157 | 157 | var promise = new Promise<TDst[]>(); |
|
158 | 158 | var res = new TDst[source.Length]; |
|
159 | 159 | var pending = source.Length; |
|
160 | 160 | |
|
161 | 161 | object locker = new object(); |
|
162 | 162 | int slots = threads; |
|
163 | 163 | |
|
164 | 164 | // Analysis disable AccessToDisposedClosure |
|
165 | 165 | AsyncPool.InvokeNewThread(() => { |
|
166 | 166 | for (int i = 0; i < source.Length; i++) { |
|
167 | 167 | if(promise.IsResolved) |
|
168 | 168 | break; // stop processing in case of error or cancellation |
|
169 | 169 | var idx = i; |
|
170 | 170 | |
|
171 | 171 | if (Interlocked.Decrement(ref slots) < 0) { |
|
172 | 172 | lock(locker) { |
|
173 | 173 | while(slots < 0) |
|
174 | 174 | Monitor.Wait(locker); |
|
175 | 175 | } |
|
176 | 176 | } |
|
177 | 177 | |
|
178 | 178 | try { |
|
179 | 179 | transform(source[i]) |
|
180 | 180 | .Anyway(() => { |
|
181 | 181 | Interlocked.Increment(ref slots); |
|
182 | 182 | lock (locker) { |
|
183 | 183 | Monitor.Pulse(locker); |
|
184 | 184 | } |
|
185 | 185 | }) |
|
186 |
. |
|
|
186 | .On( | |
|
187 | 187 | x => { |
|
188 | 188 | res[idx] = x; |
|
189 | 189 | var left = Interlocked.Decrement(ref pending); |
|
190 | 190 | if (left == 0) |
|
191 | 191 | promise.Resolve(res); |
|
192 | 192 | }, |
|
193 | 193 | promise.Reject |
|
194 | 194 | ); |
|
195 | 195 | |
|
196 | 196 | } catch (Exception e) { |
|
197 | 197 | promise.Reject(e); |
|
198 | 198 | } |
|
199 | 199 | } |
|
200 | 200 | return 0; |
|
201 | 201 | }); |
|
202 | 202 | |
|
203 | 203 | return promise; |
|
204 | 204 | } |
|
205 | 205 | |
|
206 | 206 | } |
|
207 | 207 | } |
@@ -1,908 +1,932 | |||
|
1 | 1 | using System; |
|
2 | 2 | using System.Collections.Generic; |
|
3 | 3 | using System.Reflection; |
|
4 | 4 | using System.Threading; |
|
5 | 5 | using Implab.Parallels; |
|
6 | 6 | |
|
7 | 7 | namespace Implab { |
|
8 | 8 | |
|
9 | 9 | /// <summary> |
|
10 | 10 | /// ΠΠ»Π°ΡΡ Π΄Π»Ρ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΠΎΠ³ΠΎ ΠΏΠΎΠ»ΡΡΠ΅Π½ΠΈΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ². Π’Π°ΠΊ Π½Π°Π·ΡΠ²Π°Π΅ΠΌΠΎΠ΅ "ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅". |
|
11 | 11 | /// </summary> |
|
12 | 12 | /// <typeparam name="T">Π’ΠΈΠΏ ΠΏΠΎΠ»ΡΡΠ°Π΅ΠΌΠΎΠ³ΠΎ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ°</typeparam> |
|
13 | 13 | /// <remarks> |
|
14 | 14 | /// <para>Π‘Π΅ΡΠ²ΠΈΡ ΠΏΡΠΈ ΠΎΠ±ΡΠ°ΡΠ΅Π½ΠΈΠΈ ΠΊ Π΅Π³ΠΎ ΠΌΠ΅ΡΠΎΠ΄Ρ Π΄Π°Π΅Ρ ΠΎΠ±Π΅ΡΠ°ΠΈΠ½ΠΈΠ΅ ΠΎ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ, |
|
15 | 15 | /// ΠΊΠ»ΠΈΠ΅Π½Ρ ΠΏΠΎΠ»ΡΡΠΈΠ² ΡΠ°ΠΊΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ ΡΡΡΠ°Π½ΠΎΠ²ΠΈΡΡ ΡΡΠ΄ ΠΎΠ±ΡΠ°ΡΠ½ΡΡ Π²ΡΠ·ΠΎΠ²ΠΎ Π΄Π»Ρ ΠΏΠΎΠ»ΡΡΠ΅Π½ΠΈΡ |
|
16 | 16 | /// ΡΠΎΠ±ΡΡΠΈΠΉ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ, ΡΠΎΠ΅ΡΡΡ Π·Π°Π²Π΅ΡΡΠ΅Π½ΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΠΈ ΠΏΡΠ΅Π΄ΠΎΡΡΠ°Π²Π»Π΅Π½ΠΈΠΈ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ².</para> |
|
17 | 17 | /// <para> |
|
18 | 18 | /// ΠΠ±Π΅ΡΠ΅Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ ΠΊΠ°ΠΊ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ, ΡΠ°ΠΊ ΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ Ρ ΠΎΡΠΈΠ±ΠΊΠΎΠΉ. ΠΠ»Ρ ΠΏΠΎΠ΄ΠΏΠΈΡΠΊΠΈ Π½Π° |
|
19 | 19 | /// Π΄Π°Π½Π½ΡΠ΅ ΡΠΎΠ±ΡΡΠΈΡ ΠΊΠ»ΠΈΠ΅Π½Ρ Π΄ΠΎΠ»ΠΆΠ΅Π½ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ ΠΌΠ΅ΡΠΎΠ΄Ρ <c>Then</c>. |
|
20 | 20 | /// </para> |
|
21 | 21 | /// <para> |
|
22 | 22 | /// Π‘Π΅ΡΠ²ΠΈΡ, Π² ΡΠ²ΠΎΡ ΠΎΡΠ΅ΡΠ΅Π΄Ρ, ΠΏΠΎ ΠΎΠΊΠΎΠ½ΡΠ°Π½ΠΈΡ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ (Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ Ρ ΠΎΡΠΈΠ±ΠΊΠΎΠΉ), |
|
23 | 23 | /// ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅Ρ ΠΌΠ΅ΡΠΎΠ΄Ρ <c>Resolve</c> Π»ΠΈΠ±ΠΎ <c>Reject</c> Π΄Π»Ρ ΠΎΠΏΠΎΠ²Π΅ΡΠ΅Π½ΠΈΡ ΠΊΠ»ΠΈΠ΅ΡΠ½Π° ΠΎ |
|
24 | 24 | /// Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ. |
|
25 | 25 | /// </para> |
|
26 | 26 | /// <para> |
|
27 | 27 | /// ΠΡΠ»ΠΈ ΡΠ΅ΡΠ²Π΅Ρ ΡΡΠΏΠ΅Π» Π²ΡΠΏΠΎΠ»Π½ΠΈΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π΅ΡΠ΅ Π΄ΠΎ ΡΠΎΠ³ΠΎ, ΠΊΠ°ΠΊ ΠΊΠ»ΠΈΠ΅Π½Ρ Π½Π° Π½Π΅Π³ΠΎ ΠΏΠΎΠ΄ΠΏΠΈΡΠ°Π»ΡΡ, |
|
28 | 28 | /// ΡΠΎ Π² ΠΌΠΎΠΌΠ΅Π½Ρ ΠΏΠΎΠ΄ΠΏΠΈΡΠΊΠΈ ΠΊΠ»ΠΈΠ΅Π½ΡΠ° Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ ΡΠΎΠΎΡΠ²Π΅ΡΡΠ²ΡΡΡΠΈΠ΅ ΡΠΎΠ±ΡΡΠΈΡ Π² ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΠΎΠΌ |
|
29 | 29 | /// ΡΠ΅ΠΆΠΈΠΌΠ΅ ΠΈ ΠΊΠ»ΠΈΠ΅Π½Ρ Π±ΡΠ΄Π΅Ρ ΠΎΠΏΠΎΠ²Π΅ΡΠ΅Π½ Π² Π»ΡΠ±ΠΎΠΌ ΡΠ»ΡΡΠ°Π΅. ΠΠ½Π°ΡΠ΅, ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ Π΄ΠΎΠ±Π°Π²Π»ΡΡΡΡΡ Π² |
|
30 | 30 | /// ΡΠΏΠΈΡΠΎΠΊ Π² ΠΏΠΎΡΡΠ΄ΠΊΠ΅ ΠΏΠΎΠ΄ΠΏΠΈΡΠ°Π½ΠΈΡ ΠΈ Π² ΡΡΠΎΠΌ ΠΆΠ΅ ΠΏΠΎΡΡΠ΄ΠΊΠ΅ ΠΎΠ½ΠΈ Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ |
|
31 | 31 | /// ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ. |
|
32 | 32 | /// </para> |
|
33 | 33 | /// <para> |
|
34 | 34 | /// ΠΠ±ΡΠ°Π±Π°ΡΡΠ²Π°Ρ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΌΠΎΠΆΠ½ΠΎ ΠΏΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²ΡΠ²Π°ΡΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ Π»ΠΈΠ±ΠΎ ΠΈΠ½ΠΈΡΠΈΠΈΡΠΎΠ²Π°ΡΡ |
|
35 | 35 | /// ΡΠ²ΡΠ·Π°Π½Π½ΡΠ΅ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΡΠ΅ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ, ΠΊΠΎΡΠΎΡΡΠ΅ ΡΠ°ΠΊΠΆΠ΅ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°ΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ. ΠΠ»Ρ ΡΡΠΎΠ³ΠΎ ΡΠ»Π΅Π΄ΡΠ΅Ρ |
|
36 | 36 | /// ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ ΡΠΎΠΎΡΠ²Π΅ΡΡΡΠ²ΡΡΡΡΡ ΡΠΎΡΠΌΡ ΠΌΠ΅ΡΠΎΠ΄Π΅ <c>Then</c>. |
|
37 | 37 | /// </para> |
|
38 | 38 | /// <para> |
|
39 | 39 | /// Π’Π°ΠΊΠΆΠ΅ Ρ ΠΎΡΠΎΡΠΈΠΌ ΠΏΡΠ°Π²ΠΈΠ»ΠΎΠΌ ΡΠ²Π»ΡΠ΅ΡΡΡ ΡΠΎ, ΡΡΠΎ <c>Resolve</c> ΠΈ <c>Reject</c> Π΄ΠΎΠ»ΠΆΠ΅Π½ Π²ΡΠ·ΡΠ²Π°ΡΡ |
|
40 | 40 | /// ΡΠΎΠ»ΡΠΊΠΎ ΠΈΠ½ΠΈΡΠΈΠ°ΡΠΎΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΈΠ½Π°ΡΠ΅ ΠΌΠΎΠ³ΡΡ Π²ΠΎΠ·Π½ΠΈΠΊΠ½ΡΡΡ ΠΏΡΠΎΡΠΈΠ²ΠΎΡΠ΅ΡΠΈΡ. |
|
41 | 41 | /// </para> |
|
42 | 42 | /// </remarks> |
|
43 | 43 | public class Promise<T> : IPromise<T> { |
|
44 | 44 | |
|
45 | 45 | protected struct HandlerDescriptor { |
|
46 | 46 | public Action<T> resultHandler; |
|
47 | 47 | public Func<Exception,T> errorHandler; |
|
48 | 48 | public Action cancellHandler; |
|
49 | 49 | public Promise<T> medium; |
|
50 | 50 | |
|
51 | 51 | public void Resolve(T result) { |
|
52 | 52 | if (resultHandler != null) { |
|
53 | 53 | try { |
|
54 | 54 | resultHandler(result); |
|
55 | 55 | } catch (Exception e) { |
|
56 | 56 | Reject(e); |
|
57 | 57 | return; |
|
58 | 58 | } |
|
59 | 59 | } |
|
60 | 60 | if (medium != null) |
|
61 | 61 | medium.Resolve(result); |
|
62 | 62 | } |
|
63 | 63 | |
|
64 | 64 | public void Reject(Exception err) { |
|
65 | 65 | if (errorHandler != null) { |
|
66 | 66 | try { |
|
67 | 67 | var res = errorHandler(err); |
|
68 | 68 | if (medium != null) |
|
69 | 69 | medium.Resolve(res); |
|
70 | 70 | /*} catch (TransientPromiseException err2) { |
|
71 | 71 | if (medium != null) |
|
72 | 72 | medium.Reject(err2.InnerException);*/ |
|
73 | 73 | } catch (Exception err2) { |
|
74 | 74 | if (medium != null) |
|
75 | 75 | medium.Reject(err2); |
|
76 | 76 | } |
|
77 | 77 | } else if (medium != null) |
|
78 | 78 | medium.Reject(err); |
|
79 | 79 | } |
|
80 | 80 | |
|
81 | 81 | public void Cancel() { |
|
82 | 82 | if (cancellHandler != null) { |
|
83 | 83 | try { |
|
84 | 84 | cancellHandler(); |
|
85 | 85 | } catch (Exception err) { |
|
86 | 86 | Reject(err); |
|
87 | 87 | return; |
|
88 | 88 | } |
|
89 | 89 | } |
|
90 | 90 | if (medium != null) |
|
91 | 91 | medium.Cancel(); |
|
92 | 92 | } |
|
93 | 93 | } |
|
94 | 94 | |
|
95 | 95 | const int UNRESOLVED_SATE = 0; |
|
96 | 96 | const int TRANSITIONAL_STATE = 1; |
|
97 | 97 | const int SUCCEEDED_STATE = 2; |
|
98 | 98 | const int REJECTED_STATE = 3; |
|
99 | 99 | const int CANCELLED_STATE = 4; |
|
100 | 100 | |
|
101 | 101 | int m_childrenCount; |
|
102 | 102 | int m_state; |
|
103 | 103 | T m_result; |
|
104 | 104 | Exception m_error; |
|
105 | 105 | |
|
106 | 106 | readonly MTQueue<HandlerDescriptor> m_handlers = new MTQueue<HandlerDescriptor>(); |
|
107 | 107 | |
|
108 | 108 | public Promise() { |
|
109 | 109 | } |
|
110 | 110 | |
|
111 | 111 | public Promise(IPromise parent) { |
|
112 | 112 | if (parent != null) |
|
113 | 113 | AddHandler( |
|
114 | 114 | null, |
|
115 | 115 | null, |
|
116 | 116 | () => { |
|
117 | 117 | if (parent.IsExclusive) |
|
118 | 118 | parent.Cancel(); |
|
119 | 119 | }, |
|
120 | null | |
|
120 | null, | |
|
121 | false | |
|
121 | 122 | ); |
|
122 | 123 | } |
|
123 | 124 | |
|
124 | 125 | bool BeginTransit() { |
|
125 | 126 | return UNRESOLVED_SATE == Interlocked.CompareExchange(ref m_state, TRANSITIONAL_STATE, UNRESOLVED_SATE); |
|
126 | 127 | } |
|
127 | 128 | |
|
128 | 129 | void CompleteTransit(int state) { |
|
129 | 130 | if (TRANSITIONAL_STATE != Interlocked.CompareExchange(ref m_state, state, TRANSITIONAL_STATE)) |
|
130 | 131 | throw new InvalidOperationException("Can't complete transition when the object isn't in the transitional state"); |
|
131 | 132 | } |
|
132 | 133 | |
|
133 | 134 | void WaitTransition() { |
|
134 | 135 | while (m_state == TRANSITIONAL_STATE) { |
|
135 | 136 | Thread.MemoryBarrier(); |
|
136 | 137 | } |
|
137 | 138 | } |
|
138 | 139 | |
|
139 | 140 | public bool IsResolved { |
|
140 | 141 | get { |
|
141 | 142 | Thread.MemoryBarrier(); |
|
142 | 143 | return m_state > 1; |
|
143 | 144 | } |
|
144 | 145 | } |
|
145 | 146 | |
|
146 | 147 | public bool IsCancelled { |
|
147 | 148 | get { |
|
148 | 149 | Thread.MemoryBarrier(); |
|
149 | 150 | return m_state == CANCELLED_STATE; |
|
150 | 151 | } |
|
151 | 152 | } |
|
152 | 153 | |
|
153 | 154 | public Type PromiseType { |
|
154 | 155 | get { return typeof(T); } |
|
155 | 156 | } |
|
156 | 157 | |
|
157 | 158 | /// <summary> |
|
158 | 159 | /// ΠΡΠΏΠΎΠ»Π½ΡΠ΅Ρ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΡΠΎΠΎΠ±ΡΠ°Ρ ΠΎΠ± ΡΡΠΏΠ΅ΡΠ½ΠΎΠΌ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ. |
|
159 | 160 | /// </summary> |
|
160 | 161 | /// <param name="result">Π Π΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ.</param> |
|
161 | 162 | /// <exception cref="InvalidOperationException">ΠΠ°Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΡΠΆΠ΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ</exception> |
|
162 | 163 | public void Resolve(T result) { |
|
163 | 164 | if (BeginTransit()) { |
|
164 | 165 | m_result = result; |
|
165 | 166 | CompleteTransit(SUCCEEDED_STATE); |
|
166 | 167 | OnStateChanged(); |
|
167 | 168 | } else { |
|
168 | 169 | WaitTransition(); |
|
169 | 170 | if (m_state != CANCELLED_STATE) |
|
170 | 171 | throw new InvalidOperationException("The promise is already resolved"); |
|
171 | 172 | } |
|
172 | 173 | } |
|
173 | 174 | |
|
174 | 175 | /// <summary> |
|
175 | 176 | /// ΠΡΠΏΠΎΠ»Π½ΡΠ΅Ρ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΡΠΎΠΎΠ±ΡΠ°Ρ ΠΎΠ± ΡΡΠΏΠ΅ΡΠ½ΠΎΠΌ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ. Π Π΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠΌ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ Π±ΡΠ΄Π΅Ρ ΠΏΡΡΡΠΎΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΡ. |
|
176 | 177 | /// </summary> |
|
177 | 178 | /// <remarks> |
|
178 | 179 | /// ΠΠ°Π½Π½ΡΠΉ Π²Π°ΡΠΈΠ°Π½Ρ ΡΠ΄ΠΎΠ±Π΅Π½ Π² ΡΠ»ΡΡΠ°ΡΡ , ΠΊΠΎΠ³Π΄Π° ΠΈΠ½ΡΠ΅ΡΠ΅ΡΠ΅Π½ ΡΠ°ΠΊΡ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ, Π½Π΅ΠΆΠ΅Π»ΠΈ ΠΏΠΎΠ»ΡΡΠ΅Π½Π½ΠΎΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅. |
|
179 | 180 | /// </remarks> |
|
180 | 181 | public void Resolve() { |
|
181 | 182 | Resolve(default(T)); |
|
182 | 183 | } |
|
183 | 184 | |
|
184 | 185 | /// <summary> |
|
185 | 186 | /// ΠΡΠΏΠΎΠ»Π½ΡΠ΅Ρ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΡΠΎΠΎΠ±ΡΠ°Ρ ΠΎΠ± ΠΎΡΠΈΠ±ΠΊΠ΅ |
|
186 | 187 | /// </summary> |
|
187 | 188 | /// <remarks> |
|
188 | 189 | /// ΠΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π΄ΠΎΠ»ΠΆΠ½ΠΎ ΡΠ°Π±ΠΎΡΠ°ΡΡ Π² ΠΌΠ½ΠΎΠ³ΠΎΠΏΡΠΎΡΠ½ΠΎΠΉ ΡΡΠ΅Π΄Π΅, ΠΏΡΠΈ Π΅Π³ΠΎ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΡΡΠ°Π·Ρ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΏΠΎΡΠΎΠΊΠΎΠ² |
|
189 | 190 | /// ΠΌΠΎΠ³Ρ Π²Π΅ΡΠ½ΡΡΡ ΠΎΡΠΈΠ±ΠΊΡ, ΠΏΡΠΈ ΡΡΠΎΠΌ ΡΠΎΠ»ΡΠΊΠΎ ΠΏΠ΅ΡΠ²Π°Ρ Π±ΡΠ΄Π΅Ρ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½Π° Π² ΠΊΠ°ΡΠ΅ΡΡΠ²Π΅ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ°, ΠΎΡΡΠ°Π»ΡΠ½ΡΠ΅ |
|
190 | 191 | /// Π±ΡΠ΄ΡΡ ΠΏΡΠΎΠΈΠ³Π½ΠΎΡΠΈΡΠΎΠ²Π°Π½Ρ. |
|
191 | 192 | /// </remarks> |
|
192 | 193 | /// <param name="error">ΠΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π²ΠΎΠ·Π½ΠΈΠΊΡΠ΅Π΅ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ</param> |
|
193 | 194 | /// <exception cref="InvalidOperationException">ΠΠ°Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΡΠΆΠ΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ</exception> |
|
194 | 195 | public void Reject(Exception error) { |
|
195 | 196 | if (BeginTransit()) { |
|
196 | 197 | m_error = error is TransientPromiseException ? error.InnerException : error; |
|
197 | 198 | CompleteTransit(REJECTED_STATE); |
|
198 | 199 | OnStateChanged(); |
|
199 | 200 | } else { |
|
200 | 201 | WaitTransition(); |
|
201 | 202 | if (m_state == SUCCEEDED_STATE) |
|
202 | 203 | throw new InvalidOperationException("The promise is already resolved"); |
|
203 | 204 | } |
|
204 | 205 | } |
|
205 | 206 | |
|
206 | 207 | /// <summary> |
|
207 | 208 | /// ΠΡΠΌΠ΅Π½ΡΠ΅Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, Π΅ΡΠ»ΠΈ ΡΡΠΎ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ. |
|
208 | 209 | /// </summary> |
|
209 | 210 | /// <remarks>ΠΠ»Ρ ΠΎΠΏΡΠ΅Π΄Π΅Π»Π΅Π½ΠΈΡ Π±ΡΠ»Π° Π»ΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ ΠΎΡΠΌΠ΅Π½Π΅Π½Π° ΡΠ»Π΅Π΄ΡΠ΅Ρ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ ΡΠ²ΠΎΠΉΡΡΠ²ΠΎ <see cref="IsCancelled"/>.</remarks> |
|
210 | 211 | public void Cancel() { |
|
211 | 212 | if (BeginTransit()) { |
|
212 | 213 | CompleteTransit(CANCELLED_STATE); |
|
213 | 214 | OnStateChanged(); |
|
214 | 215 | } |
|
215 | 216 | } |
|
216 | 217 | |
|
217 | 218 | public IPromise<T> Then(Action<T> success, Func<Exception,T> error, Action cancel) { |
|
218 | 219 | if (success == null && error == null && cancel == null) |
|
219 | 220 | return this; |
|
220 | 221 | |
|
221 | 222 | var medium = new Promise<T>(this); |
|
222 | 223 | |
|
223 | AddHandler(success, error, cancel, medium); | |
|
224 | AddHandler(success, error, cancel, medium, true); | |
|
224 | 225 | |
|
225 | 226 | return medium; |
|
226 | 227 | } |
|
227 | 228 | |
|
228 | 229 | /// <summary> |
|
229 | 230 | /// Adds new handlers to this promise. |
|
230 | 231 | /// </summary> |
|
231 | 232 | /// <param name="success">The handler of the successfully completed operation. |
|
232 | 233 | /// This handler will recieve an operation result as a parameter.</param> |
|
233 | 234 | /// <param name="error">Handles an exception that may occur during the operation and returns the value which will be used as the result of the operation.</param> |
|
234 | 235 | /// <returns>The new promise chained to this one.</returns> |
|
235 | 236 | public IPromise<T> Then(Action<T> success, Func<Exception,T> error) { |
|
236 | 237 | if (success == null && error == null) |
|
237 | 238 | return this; |
|
238 | 239 | |
|
239 | 240 | var medium = new Promise<T>(this); |
|
240 | 241 | |
|
241 | AddHandler(success, error, null, medium); | |
|
242 | AddHandler(success, error, null, medium, true); | |
|
242 | 243 | |
|
243 | 244 | return medium; |
|
244 | 245 | } |
|
245 | 246 | |
|
246 | 247 | |
|
247 | 248 | |
|
248 | 249 | |
|
249 | 250 | public IPromise<T> Then(Action<T> success) { |
|
250 | 251 | if (success == null) |
|
251 | 252 | return this; |
|
252 | 253 | |
|
253 | 254 | var medium = new Promise<T>(this); |
|
254 | 255 | |
|
255 | AddHandler(success, null, null, medium); | |
|
256 | AddHandler(success, null, null, medium, true); | |
|
256 | 257 | |
|
257 | 258 | return medium; |
|
258 | 259 | } |
|
259 | 260 | |
|
260 | 261 | /// <summary> |
|
261 | 262 | /// ΠΠΎΡΠ»Π΅Π΄Π½ΠΈΠΉ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ Π² ΡΠ΅ΠΏΠΎΡΠΊΠΈ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ. |
|
262 | 263 | /// </summary> |
|
263 | 264 | /// <param name="success"></param> |
|
264 | 265 | /// <param name="error"></param> |
|
265 | 266 | /// <param name="cancel"></param> |
|
266 | 267 | /// <remarks> |
|
267 | 268 | /// <para> |
|
268 | 269 | /// ΠΠ°Π½Π½ΡΠΉ ΠΌΠ΅ΡΠΎΠ΄ Π½Π΅ ΡΠΎΠ·Π΄Π°Π΅Ρ ΡΠ²ΡΠ·Π°Π½Π½ΠΎΠ³ΠΎ Ρ ΡΠ΅ΠΊΡΡΠΈΠΌ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΈ ΠΏΡΠ΅Π΄Π½Π°Π·Π½Π°ΡΠ΅Π½ Π΄Π»Ρ ΠΎΠΊΠΎΠ½ΡΠ°Π½ΠΈΡ |
|
269 | 270 | /// ΡΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΠΎΠΉ ΡΠ΅ΠΏΠΎΡΠΊΠΈ. |
|
270 | 271 | /// </para> |
|
271 | 272 | /// <para> |
|
272 | 273 | /// ΠΡΠ»ΠΈ Π΄Π°Π½Π½ΡΠΉ ΠΌΠ΅ΡΠΎΠ΄ Π²ΡΠ·Π²Π°ΡΡ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΡΠ°Π·, Π»ΠΈΠ±ΠΎ Π΄ΠΎΠ±Π°Π²ΠΈΡΡ Π΄ΡΡΠ³ΠΈΠ΅ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ, ΡΠΎ ΡΠ΅ΠΏΠΎΡΠΊΠ° |
|
273 | 274 | /// Π½Π΅ Π±ΡΠ΄Π΅Ρ ΠΎΠ΄ΠΈΠ½ΠΎΡΠ½ΠΎΠΉ <see cref="IsExclusive"/> ΠΈ, ΠΊΠ°ΠΊ ΡΠ»Π΅Π΄ΡΡΠ²ΠΈΠ΅, Π±ΡΠ΄Π΅Ρ Π½Π΅Π²ΠΎΠ·ΠΌΠΎΠΆΠ½Π° ΠΎΡΠΌΠ΅Π½Π° |
|
274 | 275 | /// Π²ΡΠ΅ΠΉ ΡΠ΅ΠΏΠΈ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ ΡΠ½ΠΈΠ·Ρ (Ρ ΡΠ°ΠΌΠΎΠ³ΠΎ ΠΏΠΎΡΠ»Π΅Π΄Π½Π΅Π³ΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ). |
|
275 | 276 | /// </para> |
|
276 | 277 | /// </remarks> |
|
277 |
public void |
|
|
278 | public void On(Action<T> success, Action<Exception> error, Action cancel) { | |
|
278 | 279 | if (success == null && error == null && cancel == null) |
|
279 | 280 | return; |
|
280 | 281 | |
|
281 | 282 | Func<Exception,T> errorHandler = null; |
|
282 | 283 | if (error != null) |
|
283 | 284 | errorHandler = err => { |
|
284 | 285 | error(err); |
|
285 | 286 | return default(T); |
|
286 | 287 | }; |
|
287 | AddHandler(success, errorHandler, cancel, null); | |
|
288 | AddHandler(success, errorHandler, cancel, null, false); | |
|
289 | } | |
|
290 | ||
|
291 | public void On(Action<T> success, Action<Exception> error) { | |
|
292 | On(success, error, null); | |
|
293 | } | |
|
294 | ||
|
295 | public void On(Action<T> success) { | |
|
296 | On(success, null, null); | |
|
288 | 297 | } |
|
289 | 298 | |
|
290 | public void Last(Action<T> success, Action<Exception> error) { | |
|
291 | Last(success, error, null); | |
|
292 | } | |
|
299 | public void On(Action handler, PromiseEventType events) { | |
|
300 | Safe.ArgumentNotNull(handler, "handler"); | |
|
293 | 301 | |
|
294 | public void Last(Action<T> success) { | |
|
295 | Last(success, null, null); | |
|
302 | Action<T> success = events.HasFlag(PromiseEventType.Success) ? new Action<T>(x => handler()) : null; | |
|
303 | Func<Exception,T> error = events.HasFlag(PromiseEventType.Error) ? new Func<Exception,T>(e => { | |
|
304 | handler(); | |
|
305 | return default(T); | |
|
306 | }) : null; | |
|
307 | Action cancel = events.HasFlag(PromiseEventType.Cancelled) ? handler : null; | |
|
308 | ||
|
309 | AddHandler(success, error, cancel, null, false); | |
|
296 | 310 | } |
|
297 | 311 | |
|
298 | 312 | public IPromise Error(Action<Exception> error) { |
|
299 | 313 | if (error == null) |
|
300 | 314 | return this; |
|
301 | 315 | |
|
302 | 316 | var medium = new Promise<T>(this); |
|
303 | 317 | |
|
304 | 318 | AddHandler( |
|
305 | 319 | null, |
|
306 | 320 | e => { |
|
307 | 321 | error(e); |
|
308 | 322 | return default(T); |
|
309 | 323 | }, |
|
310 | 324 | null, |
|
311 | medium | |
|
325 | medium, | |
|
326 | true | |
|
312 | 327 | ); |
|
313 | 328 | |
|
314 | 329 | return medium; |
|
315 | 330 | } |
|
316 | 331 | |
|
317 | 332 | /// <summary> |
|
318 | 333 | /// Handles error and allows to keep the promise. |
|
319 | 334 | /// </summary> |
|
320 | 335 | /// <remarks> |
|
321 | 336 | /// If the specified handler throws an exception, this exception will be used to reject the promise. |
|
322 | 337 | /// </remarks> |
|
323 | 338 | /// <param name="handler">The error handler which returns the result of the promise.</param> |
|
324 | 339 | /// <returns>New promise.</returns> |
|
325 | 340 | public IPromise<T> Error(Func<Exception,T> handler) { |
|
326 | 341 | if (handler == null) |
|
327 | 342 | return this; |
|
328 | 343 | |
|
329 | 344 | var medium = new Promise<T>(this); |
|
330 | 345 | |
|
331 | AddHandler(null, handler, null, medium); | |
|
346 | AddHandler(null, handler, null, medium, true); | |
|
332 | 347 | |
|
333 | 348 | return medium; |
|
334 | 349 | } |
|
335 | 350 | |
|
336 | 351 | /// <summary> |
|
337 | 352 | /// ΠΠΎΠ·Π²ΠΎΠ»ΡΠ΅Ρ ΠΏΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²Π°ΡΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΠΏΠΎΠ»Π΅Π½ΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΠΊ Π½ΠΎΠ²ΠΎΠΌΡ ΡΠΈΠΏΡ. |
|
338 | 353 | /// </summary> |
|
339 | 354 | /// <typeparam name="TNew">ΠΠΎΠ²ΡΠΉ ΡΠΈΠΏ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ°.</typeparam> |
|
340 | 355 | /// <param name="mapper">ΠΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²Π°Π½ΠΈΠ΅ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ° ΠΊ Π½ΠΎΠ²ΠΎΠΌΡ ΡΠΈΠΏΡ.</param> |
|
341 | 356 | /// <param name="error">ΠΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΎΡΠΈΠ±ΠΊΠΈ. ΠΠ°Π½Π½ΡΠΉ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΏΠΎΠ»ΡΡΠΈΡ |
|
342 | 357 | /// ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π²ΠΎΠ·Π½ΠΈΠΊΡΠ΅Π΅ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</param> |
|
343 | 358 | /// <returns>ΠΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΠΊΠΎΡΠΎΡΠΎΠ΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΈΡΡ ΠΎΠ΄Π½ΠΎΠ³ΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ.</returns> |
|
344 | 359 | /// <param name = "cancel"></param> |
|
345 | 360 | public IPromise<TNew> Then<TNew>(Func<T, TNew> mapper, Func<Exception,TNew> error, Action cancel) { |
|
346 | 361 | Safe.ArgumentNotNull(mapper, "mapper"); |
|
347 | 362 | |
|
348 | 363 | // ΡΠΎΠ·Π΄Π°Π΅ΠΌ ΠΏΡΠΈΡΠ΅ΠΏΠ»Π΅Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ |
|
349 | 364 | var medium = new Promise<TNew>(this); |
|
350 | 365 | |
|
351 | 366 | Action<T> resultHandler = result => medium.Resolve(mapper(result)); |
|
352 | 367 | Func<Exception,T> errorHandler; |
|
353 | 368 | if (error != null) |
|
354 | 369 | errorHandler = e => { |
|
355 | 370 | try { |
|
356 | 371 | medium.Resolve(error(e)); |
|
357 | 372 | } catch (Exception e2) { |
|
358 | 373 | // Π² ΡΠ»ΡΡΠ°Π΅ ΠΎΡΠΈΠ±ΠΊΠΈ Π½ΡΠΆΠ½ΠΎ ΠΏΠ΅ΡΠ΅Π΄Π°ΡΡ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π΄Π°Π»ΡΡΠ΅ ΠΏΠΎ ΡΠ΅ΠΏΠΎΡΠΊΠ΅ |
|
359 | 374 | medium.Reject(e2); |
|
360 | 375 | } |
|
361 | 376 | return default(T); |
|
362 | 377 | }; |
|
363 | 378 | else |
|
364 | 379 | errorHandler = e => { |
|
365 | 380 | medium.Reject(e); |
|
366 | 381 | return default(T); |
|
367 | 382 | }; |
|
368 | 383 | |
|
369 | 384 | Action cancelHandler; |
|
370 | 385 | if (cancel != null) |
|
371 | 386 | cancelHandler = () => { |
|
372 | 387 | cancel(); |
|
373 | 388 | medium.Cancel(); |
|
374 | 389 | }; |
|
375 | 390 | else |
|
376 | 391 | cancelHandler = medium.Cancel; |
|
377 | 392 | |
|
378 | 393 | |
|
379 | 394 | AddHandler( |
|
380 | 395 | resultHandler, |
|
381 | 396 | errorHandler, |
|
382 | 397 | cancelHandler, |
|
383 | null | |
|
398 | null, | |
|
399 | true | |
|
384 | 400 | ); |
|
385 | 401 | |
|
386 | 402 | return medium; |
|
387 | 403 | } |
|
388 | 404 | |
|
389 | 405 | public IPromise<TNew> Then<TNew>(Func<T, TNew> mapper, Func<Exception,TNew> error) { |
|
390 | 406 | return Then(mapper, error, null); |
|
391 | 407 | } |
|
392 | 408 | |
|
393 | 409 | public IPromise<TNew> Then<TNew>(Func<T, TNew> mapper) { |
|
394 | 410 | return Then(mapper, null, null); |
|
395 | 411 | } |
|
396 | 412 | |
|
397 | 413 | /// <summary> |
|
398 | 414 | /// Π‘ΡΠ΅ΠΏΠ»ΡΠ΅Ρ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ Π°ΡΠ½Ρ ΡΠΎΠ½Π½ΡΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ. Π£ΠΊΠ°Π·Π°Π½Π½Π°Ρ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ Π±ΡΠ΄Π΅Ρ Π²ΡΠ·Π²Π°Π½Π° ΠΏΠΎΡΠ»Π΅ |
|
399 | 415 | /// Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΡΠ΅ΠΊΡΡΠ΅ΠΉ, Π° ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ ΡΠ΅ΠΊΡΡΠ΅ΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ Π΄Π»Ρ ΠΈΠ½ΠΈΡΠΈΠ°Π»ΠΈΠ·Π°ΡΠΈΠΈ |
|
400 | 416 | /// Π½ΠΎΠ²ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ. |
|
401 | 417 | /// </summary> |
|
402 | 418 | /// <typeparam name="TNew">Π’ΠΈΠΏ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ° ΡΠΊΠ°Π·Π°Π½Π½ΠΎΠΉ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</typeparam> |
|
403 | 419 | /// <param name="chained">ΠΡΠΈΠ½Ρ ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, ΠΊΠΎΡΠΎΡΠ°Ρ Π΄ΠΎΠ»ΠΆΠ½Π° Π±ΡΠ΄Π΅Ρ Π½Π°ΡΠ°ΡΡΡΡ ΠΏΠΎΡΠ»Π΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΡΠ΅ΠΊΡΡΠ΅ΠΉ.</param> |
|
404 | 420 | /// <param name="error">ΠΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΎΡΠΈΠ±ΠΊΠΈ. ΠΠ°Π½Π½ΡΠΉ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΏΠΎΠ»ΡΡΠΈΡ |
|
405 | 421 | /// ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π²ΠΎΠ·Π½ΠΈΠΊΡΠ΅Π΅ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΡΠ΅ΠΊΡΠ΅ΡΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</param> |
|
406 | 422 | /// <returns>ΠΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΠΊΠΎΡΠΎΡΠΎΠ΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ ΠΏΠΎ ΠΎΠΊΠΎΠ½ΡΠ°Π½ΠΈΡ ΡΠΊΠ°Π·Π°Π½Π½ΠΎΠΉ Π°ΡΠ½Ρ ΡΠΎΠ½Π½ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</returns> |
|
407 | 423 | /// <param name = "cancel"></param> |
|
408 | 424 | public IPromise<TNew> Chain<TNew>(Func<T, IPromise<TNew>> chained, Func<Exception,IPromise<TNew>> error, Action cancel) { |
|
409 | 425 | |
|
410 | 426 | Safe.ArgumentNotNull(chained, "chained"); |
|
411 | 427 | |
|
412 | 428 | // ΠΏΡΠΎΠ±Π»Π΅ΠΌΠ° Π² ΡΠΎΠΌ, ΡΡΠΎ Π½Π° ΠΌΠΎΠΌΠ΅Π½Ρ ΡΠ²ΡΠ·ΡΠ²Π°Π½ΠΈΡ Π΅ΡΠ΅ Π½Π΅ Π½Π°ΡΠ°ΡΠ° Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, ΠΏΠΎΡΡΠΎΠΌΡ Π½ΡΠΆΠ½ΠΎ |
|
413 | 429 | // ΡΠΎΠ·Π΄Π°ΡΡ ΠΏΠΎΡΡΠ΅Π΄Π½ΠΈΠΊΠ°, ΠΊ ΠΊΠΎΡΠΎΡΠΎΠΌΡ Π±ΡΠ΄ΡΡ ΠΏΠΎΠ΄Π²ΡΠ·ΡΠ²Π°ΡΡΡΡ ΡΠ»Π΅Π΄ΡΡΡΠΈΠ΅ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ. |
|
414 | 430 | // ΠΊΠΎΠ³Π΄Π° Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½Π° ΡΠ΅Π°Π»ΡΠ½Π°Ρ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, ΠΎΠ½Π° ΠΎΠ±ΡΠ°ΡΠΈΡΡΡΡ ΠΊ ΠΏΠΎΡΡΠ΅Π΄Π½ΠΈΠΊΡ, ΡΡΠΎΠ±Ρ |
|
415 | 431 | // ΠΏΠ΅ΡΠ΅Π΄Π°ΡΡ ΡΠ΅ΡΠ΅Π· Π½Π΅Π³ΠΎ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ ΡΠ°Π±ΠΎΡΡ. |
|
416 | 432 | var medium = new Promise<TNew>(this); |
|
417 | 433 | |
|
418 | 434 | Action<T> resultHandler = delegate(T result) { |
|
419 | 435 | if (medium.IsCancelled) |
|
420 | 436 | return; |
|
421 | 437 | |
|
422 | 438 | var promise = chained(result); |
|
423 | 439 | |
|
424 |
promise. |
|
|
440 | promise.On( | |
|
425 | 441 | medium.Resolve, |
|
426 | 442 | medium.Reject, |
|
427 | 443 | () => medium.Reject(new OperationCanceledException()) // Π²Π½Π΅ΡΠ½ΡΡ ΠΎΡΠΌΠ΅Π½Π° ΡΠ²ΡΠ·Π°Π½Π½ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΡΠ°ΡΡΠΌΠ°ΡΡΠΈΠ²Π°Π΅ΡΡΡ ΠΊΠ°ΠΊ ΠΎΡΠΈΠ±ΠΊΠ° |
|
428 | 444 | ); |
|
429 | 445 | |
|
430 | 446 | // notify chained operation that it's not needed anymore |
|
431 | 447 | // ΠΏΠΎΡΡΠ΄ΠΎΠΊ Π²ΡΠ·ΠΎΠ²Π° Then, Cancelled Π²Π°ΠΆΠ΅Π½, ΠΏΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΠΎΡ ΡΡΠΎΠ³ΠΎ |
|
432 | 448 | // Π·Π°Π²ΠΈΡΠΈΡ IsExclusive |
|
433 |
medium. |
|
|
449 | medium.On( | |
|
434 | 450 | null, |
|
435 | 451 | null, |
|
436 | 452 | () => { |
|
437 | 453 | if (promise.IsExclusive) |
|
438 | 454 | promise.Cancel(); |
|
439 | 455 | } |
|
440 | 456 | ); |
|
441 | 457 | }; |
|
442 | 458 | |
|
443 | 459 | Func<Exception,T> errorHandler; |
|
444 | 460 | |
|
445 | 461 | if (error != null) |
|
446 | 462 | errorHandler = delegate(Exception e) { |
|
447 | 463 | try { |
|
448 | 464 | var promise = error(e); |
|
449 | 465 | |
|
450 |
promise. |
|
|
466 | promise.On( | |
|
451 | 467 | medium.Resolve, |
|
452 | 468 | medium.Reject, |
|
453 | 469 | () => medium.Reject(new OperationCanceledException()) // Π²Π½Π΅ΡΠ½ΡΡ ΠΎΡΠΌΠ΅Π½Π° ΡΠ²ΡΠ·Π°Π½Π½ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΡΠ°ΡΡΠΌΠ°ΡΡΠΈΠ²Π°Π΅ΡΡΡ ΠΊΠ°ΠΊ ΠΎΡΠΈΠ±ΠΊΠ° |
|
454 | 470 | ); |
|
455 | 471 | |
|
456 | 472 | // notify chained operation that it's not needed anymore |
|
457 | 473 | // ΠΏΠΎΡΡΠ΄ΠΎΠΊ Π²ΡΠ·ΠΎΠ²Π° Then, Cancelled Π²Π°ΠΆΠ΅Π½, ΠΏΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΠΎΡ ΡΡΠΎΠ³ΠΎ |
|
458 | 474 | // Π·Π°Π²ΠΈΡΠΈΡ IsExclusive |
|
459 | 475 | medium.Cancelled(() => { |
|
460 | 476 | if (promise.IsExclusive) |
|
461 | 477 | promise.Cancel(); |
|
462 | 478 | }); |
|
463 | 479 | } catch (Exception e2) { |
|
464 | 480 | medium.Reject(e2); |
|
465 | 481 | } |
|
466 | 482 | return default(T); |
|
467 | 483 | }; |
|
468 | 484 | else |
|
469 | 485 | errorHandler = err => { |
|
470 | 486 | medium.Reject(err); |
|
471 | 487 | return default(T); |
|
472 | 488 | }; |
|
473 | 489 | |
|
474 | 490 | |
|
475 | 491 | Action cancelHandler; |
|
476 | 492 | if (cancel != null) |
|
477 | 493 | cancelHandler = () => { |
|
478 | 494 | if (cancel != null) |
|
479 | 495 | cancel(); |
|
480 | 496 | medium.Cancel(); |
|
481 | 497 | }; |
|
482 | 498 | else |
|
483 | 499 | cancelHandler = medium.Cancel; |
|
484 | 500 | |
|
485 | 501 | AddHandler( |
|
486 | 502 | resultHandler, |
|
487 | 503 | errorHandler, |
|
488 | 504 | cancelHandler, |
|
489 | null | |
|
505 | null, | |
|
506 | true | |
|
490 | 507 | ); |
|
491 | 508 | |
|
492 | 509 | return medium; |
|
493 | 510 | } |
|
494 | 511 | |
|
495 | 512 | public IPromise<TNew> Chain<TNew>(Func<T, IPromise<TNew>> chained, Func<Exception,IPromise<TNew>> error) { |
|
496 | 513 | return Chain(chained, error, null); |
|
497 | 514 | } |
|
498 | 515 | |
|
499 | 516 | public IPromise<TNew> Chain<TNew>(Func<T, IPromise<TNew>> chained) { |
|
500 | 517 | return Chain(chained, null, null); |
|
501 | 518 | } |
|
502 | 519 | |
|
503 | 520 | public IPromise<T> Cancelled(Action handler) { |
|
504 | 521 | var medium = new Promise<T>(this); |
|
505 | AddHandler(null, null, handler, medium); | |
|
522 | AddHandler(null, null, handler, medium, false); | |
|
506 | 523 | return medium; |
|
507 | 524 | } |
|
508 | 525 | |
|
509 | 526 | /// <summary> |
|
510 | 527 | /// Adds the specified handler for all cases (success, error, cancel) |
|
511 | 528 | /// </summary> |
|
512 | 529 | /// <param name="handler">The handler that will be called anyway</param> |
|
513 | 530 | /// <returns>self</returns> |
|
514 | 531 | public IPromise<T> Anyway(Action handler) { |
|
515 | 532 | Safe.ArgumentNotNull(handler, "handler"); |
|
516 | 533 | |
|
534 | var medium = new Promise<T>(this); | |
|
535 | ||
|
517 | 536 | AddHandler( |
|
518 | 537 | x => handler(), |
|
519 | 538 | e => { |
|
520 | 539 | handler(); |
|
521 | 540 | throw new TransientPromiseException(e); |
|
522 | 541 | }, |
|
523 | 542 | handler, |
|
524 |
|
|
|
543 | medium, | |
|
544 | true | |
|
525 | 545 | ); |
|
526 | return this; | |
|
546 | ||
|
547 | return medium; | |
|
527 | 548 | } |
|
528 | 549 | |
|
529 | 550 | /// <summary> |
|
530 | 551 | /// ΠΡΠ΅ΠΎΠ±ΡΠ°Π·ΡΠ΅Ρ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΊ Π½ΡΠΆΠ½ΠΎΠΌΡ ΡΠΈΠΏΡ |
|
531 | 552 | /// </summary> |
|
532 | 553 | /// <typeparam name="T2"></typeparam> |
|
533 | 554 | /// <returns></returns> |
|
534 | 555 | public IPromise<T2> Cast<T2>() { |
|
535 | 556 | return Then(x => (T2)(object)x, null); |
|
536 | 557 | } |
|
537 | 558 | |
|
538 | 559 | /// <summary> |
|
539 | 560 | /// ΠΠΎΠΆΠΈΠ΄Π°Π΅ΡΡΡ ΠΎΡΠ»ΠΎΠΆΠ΅Π½Π½ΠΎΠ³ΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΈ Π² ΡΠ»ΡΡΠ°Π΅ ΡΡΠΏΠ΅Ρ Π°, Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅Ρ |
|
540 | 561 | /// Π΅Π³ΠΎ, ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ, Π² ΠΏΡΠΎΡΠΈΠ²Π½ΠΎΠΌ ΡΠ»ΡΡΠ°Π΅ Π±ΡΠΎΡΠ°Π΅Ρ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅. |
|
541 | 562 | /// </summary> |
|
542 | 563 | /// <remarks> |
|
543 | 564 | /// <para> |
|
544 | 565 | /// ΠΡΠ»ΠΈ ΠΎΠΆΠΈΠ΄Π°Π½ΠΈΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ Π±ΡΠ»ΠΎ ΠΏΡΠ΅ΡΠ²Π°Π½ΠΎ ΠΏΠΎ ΡΠ°ΠΉΠΌΠ°ΡΡΡ, ΡΡΠΎ Π½Π΅ Π·Π½Π°ΡΠΈΡ, |
|
545 | 566 | /// ΡΡΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π±ΡΠ»ΠΎ ΠΎΡΠΌΠ΅Π½Π΅Π½ΠΎ ΠΈΠ»ΠΈ ΡΡΠΎ-ΡΠΎ Π² ΡΡΠΎΠΌ ΡΠΎΠ΄Π΅, ΡΡΠΎ ΡΠΎΠ»ΡΠΊΠΎ |
|
546 | 567 | /// ΠΎΠ·Π½Π°ΡΠ°Π΅Ρ, ΡΡΠΎ ΠΌΡ Π΅Π³ΠΎ Π½Π΅ Π΄ΠΎΠΆΠ΄Π°Π»ΠΈΡΡ, ΠΎΠ΄Π½Π°ΠΊΠΎ Π²ΡΠ΅ Π·Π°ΡΠ΅Π³ΠΈΡΡΡΠΈΡΠΎΠ²Π°Π½Π½ΡΠ΅ |
|
547 | 568 | /// ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ, ΠΊΠ°ΠΊ Π±ΡΠ»ΠΈ ΡΠ°ΠΊ ΠΎΡΡΠ°Π»ΠΈΡΡ ΠΈ ΠΎΠ½ΠΈ Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ, ΠΊΠΎΠ³Π΄Π° |
|
548 | 569 | /// ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ. |
|
549 | 570 | /// </para> |
|
550 | 571 | /// <para> |
|
551 | 572 | /// Π’Π°ΠΊΠΎΠ΅ ΠΏΠΎΠ²Π΅Π΄Π΅Π½ΠΈΠ΅ Π²ΠΏΠΎΠ»Π½Π΅ ΠΎΠΏΡΠ°Π²Π΄Π°Π½ΠΎ ΠΏΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΡΠ°ΠΉΠΌΠ°ΡΡ ΠΌΠΎΠΆΠ΅Ρ ΠΈΡΡΠ΅ΡΡ |
|
552 | 573 | /// Π² ΡΠΎΡ ΠΌΠΎΠΌΠ΅Π½Ρ, ΠΊΠΎΠ³Π΄Π° Π½Π°ΡΠ°Π»Π°ΡΡ ΠΎΠ±ΡΠ°Π±ΠΎΡΠΊΠ° ΡΠ΅ΠΏΠΎΡΠΊΠΈ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΎΠ², ΠΈ |
|
553 | 574 | /// ΠΊ ΡΠΎΠΌΡ ΠΆΠ΅ ΡΠ΅ΠΊΡΡΠ΅Π΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ ΡΡΠΎΡΡΡ Π² ΡΠ΅ΠΏΠΎΡΠΊΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ ΠΈ Π΅Π³ΠΎ |
|
554 | 575 | /// ΠΎΡΠΊΠ»ΠΎΠ½Π΅Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ ΠΏΡΠΈΠ²Π΅ΡΡΠΈ ΠΊ Π½Π΅ΠΏΡΠΎΠ³Π½ΠΎΠ·ΠΈΡΡΠ΅ΠΌΠΎΠΌΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ. |
|
555 | 576 | /// </para> |
|
556 | 577 | /// </remarks> |
|
557 | 578 | /// <param name="timeout">ΠΡΠ΅ΠΌΡ ΠΎΠΆΠΈΠ΄Π°Π½ΠΈΡ</param> |
|
558 | 579 | /// <returns>Π Π΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ</returns> |
|
559 | 580 | public T Join(int timeout) { |
|
560 | 581 | var evt = new ManualResetEvent(false); |
|
561 | 582 | Anyway(() => evt.Set()); |
|
562 | 583 | |
|
563 | 584 | if (!evt.WaitOne(timeout, true)) |
|
564 | 585 | throw new TimeoutException(); |
|
565 | 586 | |
|
566 | 587 | switch (m_state) { |
|
567 | 588 | case SUCCEEDED_STATE: |
|
568 | 589 | return m_result; |
|
569 | 590 | case CANCELLED_STATE: |
|
570 | 591 | throw new OperationCanceledException(); |
|
571 | 592 | case REJECTED_STATE: |
|
572 | 593 | throw new TargetInvocationException(m_error); |
|
573 | 594 | default: |
|
574 | 595 | throw new ApplicationException(String.Format("Invalid promise state {0}", m_state)); |
|
575 | 596 | } |
|
576 | 597 | } |
|
577 | 598 | |
|
578 | 599 | public T Join() { |
|
579 | 600 | return Join(Timeout.Infinite); |
|
580 | 601 | } |
|
581 | 602 | |
|
582 | void AddHandler(Action<T> success, Func<Exception,T> error, Action cancel, Promise<T> medium) { | |
|
583 | if (success != null || error != null) | |
|
603 | void AddHandler(Action<T> success, Func<Exception,T> error, Action cancel, Promise<T> medium, bool inc) { | |
|
604 | if (inc) | |
|
584 | 605 | Interlocked.Increment(ref m_childrenCount); |
|
585 | 606 | |
|
586 | 607 | var handler = new HandlerDescriptor { |
|
587 | 608 | resultHandler = success, |
|
588 | 609 | errorHandler = error, |
|
589 | 610 | cancellHandler = cancel, |
|
590 | 611 | medium = medium |
|
591 | 612 | }; |
|
592 | 613 | |
|
593 | 614 | bool queued; |
|
594 | 615 | |
|
595 | 616 | if (!IsResolved) { |
|
596 | 617 | m_handlers.Enqueue(handler); |
|
597 | 618 | queued = true; |
|
598 | 619 | } else { |
|
599 | 620 | // the promise is in resolved state, just invoke the handled with minimum overhead |
|
600 | 621 | queued = false; |
|
601 | 622 | InvokeHandler(handler); |
|
602 | 623 | } |
|
603 | 624 | |
|
604 | 625 | if (queued && IsResolved && m_handlers.TryDequeue(out handler)) |
|
605 | 626 | // if the promise have been resolved while we was adding handler to the queue |
|
606 | 627 | // we can't guarantee that someone is still processing it |
|
607 | 628 | // therefore we will fetch a handler from the queue and execute it |
|
608 | 629 | // note that fetched handler may be not the one that we have added |
|
609 | 630 | // even we can fetch no handlers at all :) |
|
610 | 631 | InvokeHandler(handler); |
|
611 | 632 | } |
|
612 | 633 | |
|
613 | 634 | protected virtual void InvokeHandler(HandlerDescriptor handler) { |
|
614 | 635 | switch (m_state) { |
|
615 | 636 | case SUCCEEDED_STATE: |
|
616 | 637 | handler.Resolve(m_result); |
|
617 | 638 | break; |
|
618 | 639 | case REJECTED_STATE: |
|
619 | 640 | handler.Reject(m_error); |
|
620 | 641 | break; |
|
621 | 642 | case CANCELLED_STATE: |
|
622 | 643 | handler.Cancel(); |
|
623 | 644 | break; |
|
624 | 645 | default: |
|
625 | 646 | // do nothing |
|
626 | 647 | return; |
|
627 | 648 | } |
|
628 | 649 | } |
|
629 | 650 | |
|
630 | 651 | void OnStateChanged() { |
|
631 | 652 | HandlerDescriptor handler; |
|
632 | 653 | while (m_handlers.TryDequeue(out handler)) |
|
633 | 654 | InvokeHandler(handler); |
|
634 | 655 | } |
|
635 | 656 | |
|
636 | 657 | public bool IsExclusive { |
|
637 | 658 | get { |
|
638 | 659 | return m_childrenCount <= 1; |
|
639 | 660 | } |
|
640 | 661 | } |
|
641 | 662 | |
|
642 | 663 | /// <summary> |
|
643 | 664 | /// ΠΠ±ΡΠ΅Π΄ΠΈΠ½ΡΠ΅Ρ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ Π² ΠΎΠ΄Π½ΠΎ, ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠΌ ΠΊΠΎΡΠΎΡΠΎΠ³ΠΎ ΡΠ²Π»ΡΠ΅ΡΡΡ ΠΌΠ°ΡΡΠΈΠ² ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ² Π΄ΡΡΠ³ΠΈΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ. |
|
644 | 665 | /// ΠΡΠ»ΠΈ Ρ ΠΎΡΡΠ±Ρ ΠΎΠ΄Π½ΠΎ ΠΈΠ· ΠΏΠ΅ΡΠ΅Π΄Π°Π½Π½ΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ Π½Π΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ, ΡΠΎ Π½ΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ΅Π½ΠΈΠ΅ ΡΠΎΠΆΠ΅ Π½Π΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ. |
|
645 | 666 | /// ΠΡΠΈ ΠΎΡΠΌΠ΅Π½Π΅ Π½ΠΎΠ²ΠΎΠ³ΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ, ΠΏΠ΅ΡΠ΅Π΄Π°Π½Π½ΡΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΡΠ°ΠΊΠΆΠ΅ Π±ΡΠ΄ΡΡ ΠΎΡΠΌΠ΅Π½Π΅Π½Ρ, Π΅ΡΠ»ΠΈ Π½ΠΈΠΊΡΠΎ Π±ΠΎΠ»ΡΡΠ΅ Π½Π° Π½ΠΈΡ Π½Π΅ ΠΏΠΎΠ΄ΠΏΠΈΡΠ°Π½. |
|
646 | 667 | /// </summary> |
|
647 | 668 | /// <param name="promises">Π‘ΠΏΠΈΡΠΎΠΊ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ. ΠΡΠ»ΠΈ ΡΠΏΠΈΡΠΎΠΊ ΠΏΡΡΡΠΎΠΉ, ΡΠΎ ΡΠ΅Π·ΡΠ»ΡΡΠΈΡΡΡΡΠ΅Π΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅ΡΡΡ ΡΠΆΠ΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½Π½ΡΠΌ.</param> |
|
648 | 669 | /// <returns>ΠΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΠΎΠ±ΡΠ΅Π΄ΠΈΠ½ΡΡΡΠ΅Π΅ Π² ΡΠ΅Π±Π΅ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ ΠΏΠ΅ΡΠ΅Π΄Π°Π½Π½ΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ.</returns> |
|
649 | 670 | /// <exception cref="ArgumentNullException"><paramref name="promises"/> Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ null</exception> |
|
650 | 671 | public static IPromise<T[]> CreateComposite(IList<IPromise<T>> promises) { |
|
651 | 672 | if (promises == null) |
|
652 | 673 | throw new ArgumentNullException(); |
|
653 | 674 | |
|
654 | 675 | // ΡΠΎΠ·Π΄Π°Π΅ΠΌ Π°ΠΊΠΊΡΠΌΡΠ»ΡΡΠΎΡ Π΄Π»Ρ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ² ΠΈ ΡΠ΅Π·ΡΠ»ΡΡΠΈΡΡΡΡΠ΅Π΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ |
|
655 | 676 | var result = new T[promises.Count]; |
|
656 | 677 | var promise = new Promise<T[]>(); |
|
657 | 678 | |
|
658 | 679 | // special case |
|
659 | 680 | if (promises.Count == 0) { |
|
660 | 681 | promise.Resolve(result); |
|
661 | 682 | return promise; |
|
662 | 683 | } |
|
663 | 684 | |
|
664 | 685 | int pending = promises.Count; |
|
665 | 686 | |
|
666 | 687 | for (int i = 0; i < promises.Count; i++) { |
|
667 | 688 | var dest = i; |
|
668 | 689 | |
|
669 | 690 | if (promises[i] != null) { |
|
670 | 691 | promises[i].Then( |
|
671 | 692 | x => { |
|
672 | 693 | result[dest] = x; |
|
673 | 694 | if (Interlocked.Decrement(ref pending) == 0) |
|
674 | 695 | promise.Resolve(result); |
|
675 | 696 | }, |
|
676 | 697 | e => { |
|
677 | 698 | promise.Reject(e); |
|
678 | 699 | return default(T); |
|
679 | 700 | } |
|
680 | 701 | ); |
|
681 | 702 | } else { |
|
682 | 703 | if (Interlocked.Decrement(ref pending) == 0) |
|
683 | 704 | promise.Resolve(result); |
|
684 | 705 | } |
|
685 | 706 | } |
|
686 | 707 | |
|
687 | 708 | promise.Cancelled( |
|
688 | 709 | () => { |
|
689 | 710 | foreach (var d in promises) |
|
690 | 711 | if (d != null && d.IsExclusive) |
|
691 | 712 | d.Cancel(); |
|
692 | 713 | } |
|
693 | 714 | ); |
|
694 | 715 | |
|
695 | 716 | return promise; |
|
696 | 717 | } |
|
697 | 718 | |
|
698 | 719 | /// <summary> |
|
699 | 720 | /// ΠΠ±ΡΠ΅Π΄ΠΈΠ½ΡΠ΅Ρ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ Π² ΠΎΠ΄Π½ΠΎ. Π Π΅Π·ΡΠ»ΡΡΠΈΡΡΡΡΠ΅Π΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ ΠΏΡΠΈ |
|
700 | 721 | /// Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ Π²ΡΠ΅Ρ ΡΠΊΠ°Π·Π°Π½Π½ΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ. ΠΡΠΈ ΡΡΠΎΠΌ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅ΠΌΡΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΡ ΠΏΠ΅ΡΠ²ΠΈΡΠ½ΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ |
|
701 | 722 | /// ΠΈΠ³Π½ΠΎΡΠΈΡΡΡΡΡΡ. |
|
702 | 723 | /// </summary> |
|
703 | 724 | /// <param name="promises">ΠΠΎΠ»Π»Π΅ΠΊΡΠΈΡ ΠΏΠ΅ΡΠ²ΠΈΡΠ½ΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ, ΠΊΠΎΡΠΎΡΡΠ΅ Π±ΡΠ΄ΡΡ ΠΎΠ±ΡΠ΅Π΄Π΅Π½Π΅Π½Ρ Π² ΠΎΠ΄Π½ΠΎ.</param> |
|
704 | 725 | /// <returns>ΠΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΠΎΠ±ΡΠ΅Π΄ΠΈΠ½ΡΡΡΠ΅Π΅ Π² ΡΠ΅Π±Π΅ ΠΏΠ΅ΡΠ΅Π΄Π°Π½Π½ΡΠ΅.</returns> |
|
705 | 726 | /// <remarks> |
|
706 | 727 | /// ΠΡΠ»ΠΈ Π² ΠΊΠΎΠ»Π»Π΅ΠΊΡΠΈΠΈ Π²ΡΡΡΠ΅ΡΠ°ΡΡΡΡ <c>null</c>, ΡΠΎ ΠΎΠ½ΠΈ Π²ΠΎΡΠΏΡΠΈΠ½ΠΈΠΌΠ°ΡΡΡΡ ΠΊΠ°ΠΊ Π²ΡΠΏΠΎΠ»Π½Π΅Π½Π½ΡΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ. |
|
707 | 728 | /// </remarks> |
|
708 | 729 | public static IPromise CreateComposite(ICollection<IPromise> promises) { |
|
709 | 730 | if (promises == null) |
|
710 | 731 | throw new ArgumentNullException(); |
|
711 | 732 | if (promises.Count == 0) |
|
712 | 733 | return Promise<object>.ResultToPromise(null); |
|
713 | 734 | |
|
714 | 735 | int countdown = promises.Count; |
|
715 | 736 | |
|
716 | 737 | var result = new Promise<object>(); |
|
717 | 738 | |
|
718 | 739 | foreach (var d in promises) { |
|
719 | 740 | if (d == null) { |
|
720 | 741 | if (Interlocked.Decrement(ref countdown) == 0) |
|
721 | 742 | result.Resolve(null); |
|
722 | 743 | } else { |
|
723 | 744 | d.Then(() => { |
|
724 | 745 | if (Interlocked.Decrement(ref countdown) == 0) |
|
725 | 746 | result.Resolve(null); |
|
726 | 747 | }); |
|
727 | 748 | } |
|
728 | 749 | } |
|
729 | 750 | |
|
730 | 751 | result.Cancelled(() => { |
|
731 | 752 | foreach (var d in promises) |
|
732 | 753 | if (d != null && d.IsExclusive) |
|
733 | 754 | d.Cancel(); |
|
734 | 755 | }); |
|
735 | 756 | |
|
736 | 757 | return result; |
|
737 | 758 | } |
|
738 | 759 | |
|
739 | 760 | public static Promise<T> ResultToPromise(T result) { |
|
740 | 761 | var p = new Promise<T>(); |
|
741 | 762 | p.Resolve(result); |
|
742 | 763 | return p; |
|
743 | 764 | } |
|
744 | 765 | |
|
745 | 766 | public static Promise<T> ExceptionToPromise(Exception error) { |
|
746 | 767 | if (error == null) |
|
747 | 768 | throw new ArgumentNullException(); |
|
748 | 769 | |
|
749 | 770 | var p = new Promise<T>(); |
|
750 | 771 | p.Reject(error); |
|
751 | 772 | return p; |
|
752 | 773 | } |
|
753 | 774 | |
|
754 | 775 | #region IPromiseBase explicit implementation |
|
755 | 776 | |
|
756 | 777 | IPromise IPromise.Then(Action success, Action<Exception> error, Action cancel) { |
|
757 | 778 | return Then( |
|
758 | 779 | success != null ? new Action<T>(x => success()) : null, |
|
759 | 780 | error != null ? new Func<Exception,T>(e => { |
|
760 | 781 | error(e); |
|
761 | 782 | return default(T); |
|
762 | 783 | }) : null, |
|
763 | 784 | cancel |
|
764 | 785 | ); |
|
765 | 786 | } |
|
766 | 787 | |
|
767 | 788 | IPromise IPromise.Then(Action success, Action<Exception> error) { |
|
768 | 789 | return Then( |
|
769 | 790 | success != null ? new Action<T>(x => success()) : null, |
|
770 | 791 | error != null ? new Func<Exception,T>(e => { |
|
771 | 792 | error(e); |
|
772 | 793 | return default(T); |
|
773 | 794 | }) : null |
|
774 | 795 | ); |
|
775 | 796 | } |
|
776 | 797 | |
|
777 | 798 | IPromise IPromise.Then(Action success) { |
|
778 | 799 | Safe.ArgumentNotNull(success, "success"); |
|
779 | 800 | return Then(x => success()); |
|
780 | 801 | } |
|
781 | 802 | |
|
782 | 803 | IPromise IPromise.Chain(Func<IPromise> chained, Func<Exception,IPromise> error, Action cancel) { |
|
783 | 804 | return ChainNoResult(chained, error, cancel); |
|
784 | 805 | } |
|
785 | 806 | |
|
786 | 807 | IPromise ChainNoResult(Func<IPromise> chained, Func<Exception,IPromise> error, Action cancel) { |
|
787 | 808 |
|
|
788 | 809 | |
|
789 | 810 | var medium = new Promise<object>(this); |
|
790 | 811 | |
|
791 | 812 | Action<T> resultHandler = delegate { |
|
792 | 813 |
|
|
793 | 814 |
|
|
794 | 815 | |
|
795 | 816 |
|
|
796 | 817 | |
|
797 |
|
|
|
818 | promise.On( | |
|
798 | 819 |
|
|
799 | 820 |
|
|
800 | 821 |
|
|
801 | 822 |
|
|
802 | 823 | |
|
803 | 824 |
|
|
804 | 825 |
|
|
805 | 826 |
|
|
806 | 827 |
|
|
807 | 828 |
|
|
808 | 829 |
|
|
809 | 830 |
|
|
810 | 831 |
|
|
811 | 832 | |
|
812 | 833 | Func<Exception,T> errorHandler; |
|
813 | 834 | |
|
814 | 835 |
|
|
815 | 836 |
|
|
816 | 837 | try { |
|
817 | 838 | var promise = error(e); |
|
818 | 839 | |
|
819 |
promise. |
|
|
840 | promise.On( | |
|
820 | 841 | medium.Resolve, |
|
821 | 842 | medium.Reject, |
|
822 | 843 | () => medium.Reject(new OperationCanceledException()) // Π²Π½Π΅ΡΠ½ΡΡ ΠΎΡΠΌΠ΅Π½Π° ΡΠ²ΡΠ·Π°Π½Π½ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΡΠ°ΡΡΠΌΠ°ΡΡΠΈΠ²Π°Π΅ΡΡΡ ΠΊΠ°ΠΊ ΠΎΡΠΈΠ±ΠΊΠ° |
|
823 | 844 | ); |
|
824 | 845 | |
|
825 | 846 | // notify chained operation that it's not needed anymore |
|
826 | 847 | // ΠΏΠΎΡΡΠ΄ΠΎΠΊ Π²ΡΠ·ΠΎΠ²Π° Then, Cancelled Π²Π°ΠΆΠ΅Π½, ΠΏΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΠΎΡ ΡΡΠΎΠ³ΠΎ |
|
827 | 848 | // Π·Π°Π²ΠΈΡΠΈΡ IsExclusive |
|
828 | 849 | medium.Cancelled(() => { |
|
829 | 850 | if (promise.IsExclusive) |
|
830 | 851 | promise.Cancel(); |
|
831 | 852 | }); |
|
832 | 853 | } catch (Exception e2) { |
|
833 | 854 | medium.Reject(e2); |
|
834 | 855 | } |
|
835 | 856 | return default(T); |
|
836 | 857 | }; |
|
837 | 858 |
|
|
838 | 859 |
|
|
839 | 860 | medium.Reject(err); |
|
840 | 861 | return default(T); |
|
841 | 862 | }; |
|
842 | 863 | |
|
843 | 864 | |
|
844 | 865 |
|
|
845 | 866 |
|
|
846 | 867 |
|
|
847 | 868 | if (cancel != null) |
|
848 | 869 | cancel(); |
|
849 | 870 | medium.Cancel(); |
|
850 | 871 | }; |
|
851 | 872 |
|
|
852 | 873 |
|
|
853 | 874 | |
|
854 | 875 |
|
|
855 | 876 |
|
|
856 | 877 |
|
|
857 | 878 |
|
|
858 |
|
|
|
879 | null, | |
|
880 | true | |
|
859 | 881 |
|
|
860 | 882 | |
|
861 | 883 |
|
|
862 | 884 | } |
|
885 | ||
|
863 | 886 | IPromise IPromise.Chain(Func<IPromise> chained, Func<Exception,IPromise> error) { |
|
864 | 887 | return ChainNoResult(chained, error, null); |
|
865 | 888 | } |
|
889 | ||
|
866 | 890 | IPromise IPromise.Chain(Func<IPromise> chained) { |
|
867 | 891 | return ChainNoResult(chained, null, null); |
|
868 | 892 |
} |
|
869 | 893 | |
|
870 | 894 | |
|
871 |
void IPromise. |
|
|
872 |
|
|
|
895 | void IPromise.On(Action success, Action<Exception> error, Action cancel) { | |
|
896 | On(x => success(), error, cancel); | |
|
873 | 897 | } |
|
874 | 898 | |
|
875 |
void IPromise. |
|
|
876 |
|
|
|
899 | void IPromise.On(Action success, Action<Exception> error) { | |
|
900 | On(x => success(), error, null); | |
|
877 | 901 | } |
|
878 | 902 | |
|
879 |
void IPromise. |
|
|
880 |
|
|
|
903 | void IPromise.On(Action success) { | |
|
904 | On(x => success(), null, null); | |
|
881 | 905 | } |
|
882 | 906 | |
|
883 | 907 | IPromise IPromise.Error(Action<Exception> error) { |
|
884 | 908 | return Error(error); |
|
885 | 909 | } |
|
886 | 910 | |
|
887 | 911 | IPromise IPromise.Anyway(Action handler) { |
|
888 | 912 | return Anyway(handler); |
|
889 | 913 | } |
|
890 | 914 | |
|
891 | 915 | IPromise IPromise.Cancelled(Action handler) { |
|
892 | 916 | return Cancelled(handler); |
|
893 | 917 | } |
|
894 | 918 | |
|
895 | 919 | void IPromise.Join() { |
|
896 | 920 | Join(); |
|
897 | 921 | } |
|
898 | 922 | |
|
899 | 923 | void IPromise.Join(int timeout) { |
|
900 | 924 | Join(timeout); |
|
901 | 925 | } |
|
902 | 926 | |
|
903 | 927 | #endregion |
|
904 | 928 | |
|
905 | 929 | |
|
906 | 930 | |
|
907 | 931 | } |
|
908 | 932 | } |
@@ -1,83 +1,83 | |||
|
1 | 1 | using System.Threading; |
|
2 | 2 | using System; |
|
3 | 3 | #if NET_4_5 |
|
4 | 4 | using System.Threading.Tasks; |
|
5 | 5 | #endif |
|
6 | 6 | |
|
7 | 7 | namespace Implab { |
|
8 | 8 | public static class PromiseExtensions { |
|
9 | 9 | public static IPromise<T> DispatchToCurrentContext<T>(this IPromise<T> that) { |
|
10 | 10 | Safe.ArgumentNotNull(that, "that"); |
|
11 | 11 | var context = SynchronizationContext.Current; |
|
12 | 12 | if (context == null) |
|
13 | 13 | return that; |
|
14 | 14 | |
|
15 | 15 | var p = new SyncContextPromise<T>(context, that); |
|
16 | 16 | |
|
17 |
that. |
|
|
17 | that.On( | |
|
18 | 18 | p.Resolve, |
|
19 | 19 | p.Reject, |
|
20 | 20 | p.Cancel |
|
21 | 21 | ); |
|
22 | 22 | return p; |
|
23 | 23 | } |
|
24 | 24 | |
|
25 | 25 | public static IPromise<T> DispatchToContext<T>(this IPromise<T> that, SynchronizationContext context) { |
|
26 | 26 | Safe.ArgumentNotNull(that, "that"); |
|
27 | 27 | Safe.ArgumentNotNull(context, "context"); |
|
28 | 28 | |
|
29 | 29 | var p = new SyncContextPromise<T>(context, that); |
|
30 | 30 | |
|
31 |
that. |
|
|
31 | that.On( | |
|
32 | 32 | p.Resolve, |
|
33 | 33 | p.Reject, |
|
34 | 34 | p.Cancel |
|
35 | 35 | ); |
|
36 | 36 | return p; |
|
37 | 37 | } |
|
38 | 38 | |
|
39 | 39 | /// <summary> |
|
40 | 40 | /// Ensures the dispatched. |
|
41 | 41 | /// </summary> |
|
42 | 42 | /// <returns>The dispatched.</returns> |
|
43 | 43 | /// <param name="that">That.</param> |
|
44 | 44 | /// <param name="head">Head.</param> |
|
45 | 45 | /// <param name="cleanup">Cleanup.</param> |
|
46 | 46 | /// <typeparam name="TPromise">The 1st type parameter.</typeparam> |
|
47 | 47 | /// <typeparam name="T">The 2nd type parameter.</typeparam> |
|
48 | 48 | public static TPromise EnsureDispatched<TPromise,T>(this TPromise that, IPromise<T> head, Action<T> cleanup) where TPromise : IPromise{ |
|
49 | 49 | Safe.ArgumentNotNull(that, "that"); |
|
50 | 50 | Safe.ArgumentNotNull(head, "head"); |
|
51 | 51 | |
|
52 |
that. |
|
|
52 | that.On(null,null,() => head.On(cleanup)); | |
|
53 | 53 | |
|
54 | 54 | return that; |
|
55 | 55 | } |
|
56 | 56 | |
|
57 | 57 | public static AsyncCallback AsyncCallback<T>(this Promise<T> that, Func<IAsyncResult,T> callback) { |
|
58 | 58 | Safe.ArgumentNotNull(that, "that"); |
|
59 | 59 | Safe.ArgumentNotNull(callback, "callback"); |
|
60 | 60 | return ar => { |
|
61 | 61 | try { |
|
62 | 62 | that.Resolve(callback(ar)); |
|
63 | 63 | } catch (Exception err) { |
|
64 | 64 | that.Reject(err); |
|
65 | 65 | } |
|
66 | 66 | }; |
|
67 | 67 | } |
|
68 | 68 | |
|
69 | 69 | #if NET_4_5 |
|
70 | 70 | |
|
71 | 71 | public static Task<T> GetTask<T>(this IPromise<T> that) { |
|
72 | 72 | Safe.ArgumentNotNull(that, "that"); |
|
73 | 73 | var tcs = new TaskCompletionSource<T>(); |
|
74 | 74 | |
|
75 |
that. |
|
|
75 | that.On(tcs.SetResult, tcs.SetException, tcs.SetCanceled); | |
|
76 | 76 | |
|
77 | 77 | return tcs.Task; |
|
78 | 78 | } |
|
79 | 79 | |
|
80 | 80 | #endif |
|
81 | 81 | } |
|
82 | 82 | } |
|
83 | 83 |
@@ -1,48 +1,48 | |||
|
1 | 1 | using System; |
|
2 | 2 | using Implab.Diagnostics; |
|
3 | 3 | using Implab.Parallels; |
|
4 | 4 | using Implab; |
|
5 | 5 | using System.Collections.Generic; |
|
6 | 6 | using System.Collections.Concurrent; |
|
7 | 7 | |
|
8 | 8 | namespace MonoPlay { |
|
9 | 9 | class MainClass { |
|
10 | 10 | public static void Main(string[] args) { |
|
11 | 11 | if (args == null) |
|
12 | 12 | throw new ArgumentNullException("args"); |
|
13 | 13 | |
|
14 | 14 | var q1 = new MTQueue<int>(); |
|
15 |
var q2 = new |
|
|
15 | var q2 = new Queue<int>(); | |
|
16 | 16 | |
|
17 | 17 | const int count = 10000000; |
|
18 | 18 | |
|
19 | 19 | var t1 = Environment.TickCount; |
|
20 | 20 | |
|
21 | 21 | for (var i = 0; i < count; i++) |
|
22 | 22 | q1.Enqueue(i); |
|
23 | 23 | |
|
24 | 24 | var t2 = Environment.TickCount; |
|
25 | 25 | Console.WriteLine("MTQueue: {0} ms", t2 - t1); |
|
26 | 26 | |
|
27 | 27 | t1 = Environment.TickCount; |
|
28 | 28 | |
|
29 | 29 | for (var i = 0; i < count; i++) |
|
30 | 30 | q2.Enqueue(i); |
|
31 | 31 | |
|
32 | 32 | t2 = Environment.TickCount; |
|
33 | 33 | Console.WriteLine("LinkedList: {0} ms", t2 - t1); |
|
34 | 34 | |
|
35 |
q2 = new |
|
|
35 | q2 = new Queue<int>(); | |
|
36 | 36 | |
|
37 | 37 | t1 = Environment.TickCount; |
|
38 | 38 | |
|
39 | 39 | for (var i = 0; i < count; i++) |
|
40 | 40 | lock (q2) |
|
41 | 41 | q2.Enqueue(i); |
|
42 | 42 | |
|
43 | 43 | t2 = Environment.TickCount; |
|
44 | 44 | Console.WriteLine("LinkedList+Lock: {0} ms", t2 - t1); |
|
45 | 45 | |
|
46 | 46 | } |
|
47 | 47 | } |
|
48 | 48 | } |
General Comments 0
You need to be logged in to leave comments.
Login now