|
@@
-1,646
+1,656
|
|
1
|
using System;
|
|
1
|
using System;
|
|
2
|
using System.Collections.Generic;
|
|
2
|
using System.Collections.Generic;
|
|
3
|
using System.Reflection;
|
|
3
|
using System.Reflection;
|
|
4
|
using System.Diagnostics;
|
|
4
|
using System.Diagnostics;
|
|
5
|
using System.Threading;
|
|
5
|
using System.Threading;
|
|
6
|
using Implab.Parallels;
|
|
6
|
using Implab.Parallels;
|
|
7
|
|
|
7
|
|
|
8
|
namespace Implab {
|
|
8
|
namespace Implab {
|
|
9
|
|
|
9
|
|
|
10
|
public delegate void ErrorHandler(Exception e);
|
|
10
|
public delegate void ErrorHandler(Exception e);
|
|
11
|
public delegate T ErrorHandler<out T>(Exception e);
|
|
11
|
public delegate T ErrorHandler<out T>(Exception e);
|
|
12
|
public delegate void ResultHandler<in T>(T result);
|
|
12
|
public delegate void ResultHandler<in T>(T result);
|
|
13
|
public delegate TNew ResultMapper<in TSrc, out TNew>(TSrc result);
|
|
13
|
public delegate TNew ResultMapper<in TSrc, out TNew>(TSrc result);
|
|
14
|
public delegate Promise<TNew> ChainedOperation<in TSrc, TNew>(TSrc result);
|
|
14
|
public delegate Promise<TNew> ChainedOperation<in TSrc, TNew>(TSrc result);
|
|
15
|
|
|
15
|
|
|
16
|
/// <summary>
|
|
16
|
/// <summary>
|
|
17
|
/// ΠΠ»Π°ΡΡ Π΄Π»Ρ Π°ΡΠΈΠ½Ρ
ΡΠΎΠ½Π½ΠΎΠ³ΠΎ ΠΏΠΎΠ»ΡΡΠ΅Π½ΠΈΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ². Π’Π°ΠΊ Π½Π°Π·ΡΠ²Π°Π΅ΠΌΠΎΠ΅ "ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅".
|
|
17
|
/// ΠΠ»Π°ΡΡ Π΄Π»Ρ Π°ΡΠΈΠ½Ρ
ΡΠΎΠ½Π½ΠΎΠ³ΠΎ ΠΏΠΎΠ»ΡΡΠ΅Π½ΠΈΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ². Π’Π°ΠΊ Π½Π°Π·ΡΠ²Π°Π΅ΠΌΠΎΠ΅ "ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅".
|
|
18
|
/// </summary>
|
|
18
|
/// </summary>
|
|
19
|
/// <typeparam name="T">Π’ΠΈΠΏ ΠΏΠΎΠ»ΡΡΠ°Π΅ΠΌΠΎΠ³ΠΎ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ°</typeparam>
|
|
19
|
/// <typeparam name="T">Π’ΠΈΠΏ ΠΏΠΎΠ»ΡΡΠ°Π΅ΠΌΠΎΠ³ΠΎ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ°</typeparam>
|
|
20
|
/// <remarks>
|
|
20
|
/// <remarks>
|
|
21
|
/// <para>Π‘Π΅ΡΠ²ΠΈΡ ΠΏΡΠΈ ΠΎΠ±ΡΠ°ΡΠ΅Π½ΠΈΠΈ ΠΊ Π΅Π³ΠΎ ΠΌΠ΅ΡΠΎΠ΄Ρ Π΄Π°Π΅Ρ ΠΎΠ±Π΅ΡΠ°ΠΈΠ½ΠΈΠ΅ ΠΎ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ,
|
|
21
|
/// <para>Π‘Π΅ΡΠ²ΠΈΡ ΠΏΡΠΈ ΠΎΠ±ΡΠ°ΡΠ΅Π½ΠΈΠΈ ΠΊ Π΅Π³ΠΎ ΠΌΠ΅ΡΠΎΠ΄Ρ Π΄Π°Π΅Ρ ΠΎΠ±Π΅ΡΠ°ΠΈΠ½ΠΈΠ΅ ΠΎ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ,
|
|
22
|
/// ΠΊΠ»ΠΈΠ΅Π½Ρ ΠΏΠΎΠ»ΡΡΠΈΠ² ΡΠ°ΠΊΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ ΡΡΡΠ°Π½ΠΎΠ²ΠΈΡΡ ΡΡΠ΄ ΠΎΠ±ΡΠ°ΡΠ½ΡΡ
Π²ΡΠ·ΠΎΠ²ΠΎ Π΄Π»Ρ ΠΏΠΎΠ»ΡΡΠ΅Π½ΠΈΡ
|
|
22
|
/// ΠΊΠ»ΠΈΠ΅Π½Ρ ΠΏΠΎΠ»ΡΡΠΈΠ² ΡΠ°ΠΊΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ ΡΡΡΠ°Π½ΠΎΠ²ΠΈΡΡ ΡΡΠ΄ ΠΎΠ±ΡΠ°ΡΠ½ΡΡ
Π²ΡΠ·ΠΎΠ²ΠΎ Π΄Π»Ρ ΠΏΠΎΠ»ΡΡΠ΅Π½ΠΈΡ
|
|
23
|
/// ΡΠΎΠ±ΡΡΠΈΠΉ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ, ΡΠΎΠ΅ΡΡΡ Π·Π°Π²Π΅ΡΡΠ΅Π½ΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΠΈ ΠΏΡΠ΅Π΄ΠΎΡΡΠ°Π²Π»Π΅Π½ΠΈΠΈ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ².</para>
|
|
23
|
/// ΡΠΎΠ±ΡΡΠΈΠΉ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ, ΡΠΎΠ΅ΡΡΡ Π·Π°Π²Π΅ΡΡΠ΅Π½ΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΠΈ ΠΏΡΠ΅Π΄ΠΎΡΡΠ°Π²Π»Π΅Π½ΠΈΠΈ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ².</para>
|
|
24
|
/// <para>
|
|
24
|
/// <para>
|
|
25
|
/// ΠΠ±Π΅ΡΠ΅Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ ΠΊΠ°ΠΊ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ, ΡΠ°ΠΊ ΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ Ρ ΠΎΡΠΈΠ±ΠΊΠΎΠΉ. ΠΠ»Ρ ΠΏΠΎΠ΄ΠΏΠΈΡΠΊΠΈ Π½Π°
|
|
25
|
/// ΠΠ±Π΅ΡΠ΅Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ ΠΊΠ°ΠΊ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ, ΡΠ°ΠΊ ΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ Ρ ΠΎΡΠΈΠ±ΠΊΠΎΠΉ. ΠΠ»Ρ ΠΏΠΎΠ΄ΠΏΠΈΡΠΊΠΈ Π½Π°
|
|
26
|
/// Π΄Π°Π½Π½ΡΠ΅ ΡΠΎΠ±ΡΡΠΈΡ ΠΊΠ»ΠΈΠ΅Π½Ρ Π΄ΠΎΠ»ΠΆΠ΅Π½ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ ΠΌΠ΅ΡΠΎΠ΄Ρ <c>Then</c>.
|
|
26
|
/// Π΄Π°Π½Π½ΡΠ΅ ΡΠΎΠ±ΡΡΠΈΡ ΠΊΠ»ΠΈΠ΅Π½Ρ Π΄ΠΎΠ»ΠΆΠ΅Π½ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ ΠΌΠ΅ΡΠΎΠ΄Ρ <c>Then</c>.
|
|
27
|
/// </para>
|
|
27
|
/// </para>
|
|
28
|
/// <para>
|
|
28
|
/// <para>
|
|
29
|
/// Π‘Π΅ΡΠ²ΠΈΡ, Π² ΡΠ²ΠΎΡ ΠΎΡΠ΅ΡΠ΅Π΄Ρ, ΠΏΠΎ ΠΎΠΊΠΎΠ½ΡΠ°Π½ΠΈΡ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ (Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ Ρ ΠΎΡΠΈΠ±ΠΊΠΎΠΉ),
|
|
29
|
/// Π‘Π΅ΡΠ²ΠΈΡ, Π² ΡΠ²ΠΎΡ ΠΎΡΠ΅ΡΠ΅Π΄Ρ, ΠΏΠΎ ΠΎΠΊΠΎΠ½ΡΠ°Π½ΠΈΡ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ (Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ Ρ ΠΎΡΠΈΠ±ΠΊΠΎΠΉ),
|
|
30
|
/// ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅Ρ ΠΌΠ΅ΡΠΎΠ΄Ρ <c>Resolve</c> Π»ΠΈΠ±ΠΎ <c>Reject</c> Π΄Π»Ρ ΠΎΠΏΠΎΠ²Π΅ΡΠ΅Π½ΠΈΡ ΠΊΠ»ΠΈΠ΅ΡΠ½Π° ΠΎ
|
|
30
|
/// ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅Ρ ΠΌΠ΅ΡΠΎΠ΄Ρ <c>Resolve</c> Π»ΠΈΠ±ΠΎ <c>Reject</c> Π΄Π»Ρ ΠΎΠΏΠΎΠ²Π΅ΡΠ΅Π½ΠΈΡ ΠΊΠ»ΠΈΠ΅ΡΠ½Π° ΠΎ
|
|
31
|
/// Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ.
|
|
31
|
/// Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ.
|
|
32
|
/// </para>
|
|
32
|
/// </para>
|
|
33
|
/// <para>
|
|
33
|
/// <para>
|
|
34
|
/// ΠΡΠ»ΠΈ ΡΠ΅ΡΠ²Π΅Ρ ΡΡΠΏΠ΅Π» Π²ΡΠΏΠΎΠ»Π½ΠΈΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π΅ΡΠ΅ Π΄ΠΎ ΡΠΎΠ³ΠΎ, ΠΊΠ°ΠΊ ΠΊΠ»ΠΈΠ΅Π½Ρ Π½Π° Π½Π΅Π³ΠΎ ΠΏΠΎΠ΄ΠΏΠΈΡΠ°Π»ΡΡ,
|
|
34
|
/// ΠΡΠ»ΠΈ ΡΠ΅ΡΠ²Π΅Ρ ΡΡΠΏΠ΅Π» Π²ΡΠΏΠΎΠ»Π½ΠΈΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π΅ΡΠ΅ Π΄ΠΎ ΡΠΎΠ³ΠΎ, ΠΊΠ°ΠΊ ΠΊΠ»ΠΈΠ΅Π½Ρ Π½Π° Π½Π΅Π³ΠΎ ΠΏΠΎΠ΄ΠΏΠΈΡΠ°Π»ΡΡ,
|
|
35
|
/// ΡΠΎ Π² ΠΌΠΎΠΌΠ΅Π½Ρ ΠΏΠΎΠ΄ΠΏΠΈΡΠΊΠΈ ΠΊΠ»ΠΈΠ΅Π½ΡΠ° Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ ΡΠΎΠΎΡΠ²Π΅ΡΡΠ²ΡΡΡΠΈΠ΅ ΡΠΎΠ±ΡΡΠΈΡ Π² ΡΠΈΠ½Ρ
ΡΠΎΠ½Π½ΠΎΠΌ
|
|
35
|
/// ΡΠΎ Π² ΠΌΠΎΠΌΠ΅Π½Ρ ΠΏΠΎΠ΄ΠΏΠΈΡΠΊΠΈ ΠΊΠ»ΠΈΠ΅Π½ΡΠ° Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ ΡΠΎΠΎΡΠ²Π΅ΡΡΠ²ΡΡΡΠΈΠ΅ ΡΠΎΠ±ΡΡΠΈΡ Π² ΡΠΈΠ½Ρ
ΡΠΎΠ½Π½ΠΎΠΌ
|
|
36
|
/// ΡΠ΅ΠΆΠΈΠΌΠ΅ ΠΈ ΠΊΠ»ΠΈΠ΅Π½Ρ Π±ΡΠ΄Π΅Ρ ΠΎΠΏΠΎΠ²Π΅ΡΠ΅Π½ Π² Π»ΡΠ±ΠΎΠΌ ΡΠ»ΡΡΠ°Π΅. ΠΠ½Π°ΡΠ΅, ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ Π΄ΠΎΠ±Π°Π²Π»ΡΡΡΡΡ Π²
|
|
36
|
/// ΡΠ΅ΠΆΠΈΠΌΠ΅ ΠΈ ΠΊΠ»ΠΈΠ΅Π½Ρ Π±ΡΠ΄Π΅Ρ ΠΎΠΏΠΎΠ²Π΅ΡΠ΅Π½ Π² Π»ΡΠ±ΠΎΠΌ ΡΠ»ΡΡΠ°Π΅. ΠΠ½Π°ΡΠ΅, ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ Π΄ΠΎΠ±Π°Π²Π»ΡΡΡΡΡ Π²
|
|
37
|
/// ΡΠΏΠΈΡΠΎΠΊ Π² ΠΏΠΎΡΡΠ΄ΠΊΠ΅ ΠΏΠΎΠ΄ΠΏΠΈΡΠ°Π½ΠΈΡ ΠΈ Π² ΡΡΠΎΠΌ ΠΆΠ΅ ΠΏΠΎΡΡΠ΄ΠΊΠ΅ ΠΎΠ½ΠΈ Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ
|
|
37
|
/// ΡΠΏΠΈΡΠΎΠΊ Π² ΠΏΠΎΡΡΠ΄ΠΊΠ΅ ΠΏΠΎΠ΄ΠΏΠΈΡΠ°Π½ΠΈΡ ΠΈ Π² ΡΡΠΎΠΌ ΠΆΠ΅ ΠΏΠΎΡΡΠ΄ΠΊΠ΅ ΠΎΠ½ΠΈ Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ
|
|
38
|
/// ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ.
|
|
38
|
/// ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ.
|
|
39
|
/// </para>
|
|
39
|
/// </para>
|
|
40
|
/// <para>
|
|
40
|
/// <para>
|
|
41
|
/// ΠΠ±ΡΠ°Π±Π°ΡΡΠ²Π°Ρ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΌΠΎΠΆΠ½ΠΎ ΠΏΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²ΡΠ²Π°ΡΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ Π»ΠΈΠ±ΠΎ ΠΈΠ½ΠΈΡΠΈΠΈΡΠΎΠ²Π°ΡΡ
|
|
41
|
/// ΠΠ±ΡΠ°Π±Π°ΡΡΠ²Π°Ρ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΌΠΎΠΆΠ½ΠΎ ΠΏΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²ΡΠ²Π°ΡΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ Π»ΠΈΠ±ΠΎ ΠΈΠ½ΠΈΡΠΈΠΈΡΠΎΠ²Π°ΡΡ
|
|
42
|
/// ΡΠ²ΡΠ·Π°Π½Π½ΡΠ΅ Π°ΡΠΈΠ½Ρ
ΡΠΎΠ½Π½ΡΠ΅ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ, ΠΊΠΎΡΠΎΡΡΠ΅ ΡΠ°ΠΊΠΆΠ΅ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°ΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ. ΠΠ»Ρ ΡΡΠΎΠ³ΠΎ ΡΠ»Π΅Π΄ΡΠ΅Ρ
|
|
42
|
/// ΡΠ²ΡΠ·Π°Π½Π½ΡΠ΅ Π°ΡΠΈΠ½Ρ
ΡΠΎΠ½Π½ΡΠ΅ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ, ΠΊΠΎΡΠΎΡΡΠ΅ ΡΠ°ΠΊΠΆΠ΅ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°ΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ. ΠΠ»Ρ ΡΡΠΎΠ³ΠΎ ΡΠ»Π΅Π΄ΡΠ΅Ρ
|
|
43
|
/// ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ ΡΠΎΠΎΡΠ²Π΅ΡΡΡΠ²ΡΡΡΡΡ ΡΠΎΡΠΌΡ ΠΌΠ΅ΡΠΎΠ΄Π΅ <c>Then</c>.
|
|
43
|
/// ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ ΡΠΎΠΎΡΠ²Π΅ΡΡΡΠ²ΡΡΡΡΡ ΡΠΎΡΠΌΡ ΠΌΠ΅ΡΠΎΠ΄Π΅ <c>Then</c>.
|
|
44
|
/// </para>
|
|
44
|
/// </para>
|
|
45
|
/// <para>
|
|
45
|
/// <para>
|
|
46
|
/// Π’Π°ΠΊΠΆΠ΅ Ρ
ΠΎΡΠΎΡΠΈΠΌ ΠΏΡΠ°Π²ΠΈΠ»ΠΎΠΌ ΡΠ²Π»ΡΠ΅ΡΡΡ ΡΠΎ, ΡΡΠΎ <c>Resolve</c> ΠΈ <c>Reject</c> Π΄ΠΎΠ»ΠΆΠ΅Π½ Π²ΡΠ·ΡΠ²Π°ΡΡ
|
|
46
|
/// Π’Π°ΠΊΠΆΠ΅ Ρ
ΠΎΡΠΎΡΠΈΠΌ ΠΏΡΠ°Π²ΠΈΠ»ΠΎΠΌ ΡΠ²Π»ΡΠ΅ΡΡΡ ΡΠΎ, ΡΡΠΎ <c>Resolve</c> ΠΈ <c>Reject</c> Π΄ΠΎΠ»ΠΆΠ΅Π½ Π²ΡΠ·ΡΠ²Π°ΡΡ
|
|
47
|
/// ΡΠΎΠ»ΡΠΊΠΎ ΠΈΠ½ΠΈΡΠΈΠ°ΡΠΎΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΈΠ½Π°ΡΠ΅ ΠΌΠΎΠ³ΡΡ Π²ΠΎΠ·Π½ΠΈΠΊΠ½ΡΡΡ ΠΏΡΠΎΡΠΈΠ²ΠΎΡΠ΅ΡΠΈΡ.
|
|
47
|
/// ΡΠΎΠ»ΡΠΊΠΎ ΠΈΠ½ΠΈΡΠΈΠ°ΡΠΎΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΈΠ½Π°ΡΠ΅ ΠΌΠΎΠ³ΡΡ Π²ΠΎΠ·Π½ΠΈΠΊΠ½ΡΡΡ ΠΏΡΠΎΡΠΈΠ²ΠΎΡΠ΅ΡΠΈΡ.
|
|
48
|
/// </para>
|
|
48
|
/// </para>
|
|
49
|
/// </remarks>
|
|
49
|
/// </remarks>
|
|
50
|
public class Promise<T> : IPromise<T> {
|
|
50
|
public class Promise<T> : IPromise<T> {
|
|
51
|
|
|
51
|
|
|
52
|
struct HandlerDescriptor {
|
|
52
|
struct HandlerDescriptor {
|
|
53
|
public ResultHandler<T> resultHandler;
|
|
53
|
public ResultHandler<T> resultHandler;
|
|
54
|
public ErrorHandler errorHandler;
|
|
54
|
public ErrorHandler errorHandler;
|
|
55
|
public Action cancellHandler;
|
|
55
|
public Action cancellHandler;
|
|
56
|
|
|
56
|
|
|
57
|
public void Resolve(T result) {
|
|
57
|
public void Resolve(T result) {
|
|
58
|
if (resultHandler != null)
|
|
58
|
if (resultHandler != null)
|
|
59
|
try {
|
|
59
|
try {
|
|
60
|
resultHandler(result);
|
|
60
|
resultHandler(result);
|
|
61
|
} catch (Exception e) {
|
|
61
|
} catch (Exception e) {
|
|
62
|
Reject(e);
|
|
62
|
Reject(e);
|
|
63
|
}
|
|
63
|
}
|
|
64
|
}
|
|
64
|
}
|
|
65
|
|
|
65
|
|
|
66
|
public void Reject(Exception err) {
|
|
66
|
public void Reject(Exception err) {
|
|
67
|
if (errorHandler != null)
|
|
67
|
if (errorHandler != null)
|
|
68
|
try {
|
|
68
|
try {
|
|
69
|
errorHandler(err);
|
|
69
|
errorHandler(err);
|
|
70
|
} catch {
|
|
70
|
} catch {
|
|
71
|
}
|
|
71
|
}
|
|
72
|
}
|
|
72
|
}
|
|
73
|
|
|
73
|
|
|
74
|
public void Cancel() {
|
|
74
|
public void Cancel() {
|
|
75
|
if (cancellHandler != null)
|
|
75
|
if (cancellHandler != null)
|
|
76
|
try {
|
|
76
|
try {
|
|
77
|
cancellHandler();
|
|
77
|
cancellHandler();
|
|
78
|
} catch {
|
|
78
|
} catch {
|
|
79
|
}
|
|
79
|
}
|
|
80
|
}
|
|
80
|
}
|
|
81
|
}
|
|
81
|
}
|
|
82
|
|
|
82
|
|
|
83
|
const int UnresolvedSate = 0;
|
|
83
|
const int UnresolvedSate = 0;
|
|
84
|
const int TransitionalState = 1;
|
|
84
|
const int TransitionalState = 1;
|
|
85
|
const int SucceededState = 2;
|
|
85
|
const int SucceededState = 2;
|
|
86
|
const int RejectedState = 3;
|
|
86
|
const int RejectedState = 3;
|
|
87
|
const int CancelledState = 4;
|
|
87
|
const int CancelledState = 4;
|
|
88
|
|
|
88
|
|
|
89
|
readonly IPromiseBase m_parent;
|
|
89
|
readonly IPromiseBase m_parent;
|
|
90
|
readonly bool m_cancellable;
|
|
90
|
readonly bool m_cancellable;
|
|
91
|
|
|
91
|
|
|
92
|
int m_childrenCount = 0;
|
|
92
|
int m_childrenCount = 0;
|
|
93
|
int m_state;
|
|
93
|
int m_state;
|
|
94
|
T m_result;
|
|
94
|
T m_result;
|
|
95
|
Exception m_error;
|
|
95
|
Exception m_error;
|
|
96
|
|
|
96
|
|
|
97
|
readonly MTQueue<HandlerDescriptor> m_handlers = new MTQueue<HandlerDescriptor>();
|
|
97
|
readonly MTQueue<HandlerDescriptor> m_handlers = new MTQueue<HandlerDescriptor>();
|
|
98
|
|
|
98
|
|
|
99
|
public Promise() {
|
|
99
|
public Promise() {
|
|
100
|
m_cancellable = true;
|
|
100
|
m_cancellable = true;
|
|
101
|
}
|
|
101
|
}
|
|
102
|
|
|
102
|
|
|
103
|
public Promise(IPromiseBase parent, bool cancellable) {
|
|
103
|
public Promise(IPromiseBase parent, bool cancellable) {
|
|
104
|
m_cancellable = cancellable;
|
|
104
|
m_cancellable = cancellable;
|
|
105
|
m_parent = parent;
|
|
105
|
m_parent = parent;
|
|
106
|
}
|
|
106
|
}
|
|
107
|
|
|
107
|
|
|
108
|
void InternalCancel() {
|
|
108
|
void InternalCancel() {
|
|
109
|
// don't try to cancel parent :)
|
|
109
|
// don't try to cancel parent :)
|
|
110
|
Cancel(false);
|
|
110
|
Cancel(false);
|
|
111
|
}
|
|
111
|
}
|
|
112
|
|
|
112
|
|
|
113
|
bool BeginTransit() {
|
|
113
|
bool BeginTransit() {
|
|
114
|
return UnresolvedSate == Interlocked.CompareExchange(ref m_state, TransitionalState, UnresolvedSate);
|
|
114
|
return UnresolvedSate == Interlocked.CompareExchange(ref m_state, TransitionalState, UnresolvedSate);
|
|
115
|
}
|
|
115
|
}
|
|
116
|
|
|
116
|
|
|
117
|
void CompleteTransit(int state) {
|
|
117
|
void CompleteTransit(int state) {
|
|
118
|
if (TransitionalState != Interlocked.CompareExchange(ref m_state, state, TransitionalState))
|
|
118
|
if (TransitionalState != Interlocked.CompareExchange(ref m_state, state, TransitionalState))
|
|
119
|
throw new InvalidOperationException("Can't complete transition when the object isn't in the transitional state");
|
|
119
|
throw new InvalidOperationException("Can't complete transition when the object isn't in the transitional state");
|
|
120
|
}
|
|
120
|
}
|
|
121
|
|
|
121
|
|
|
122
|
void WaitTransition() {
|
|
122
|
void WaitTransition() {
|
|
123
|
while (m_state == TransitionalState) {
|
|
123
|
while (m_state == TransitionalState) {
|
|
124
|
/* noop */
|
|
124
|
/* noop */
|
|
125
|
}
|
|
125
|
}
|
|
126
|
}
|
|
126
|
}
|
|
127
|
|
|
127
|
|
|
128
|
public bool IsResolved {
|
|
128
|
public bool IsResolved {
|
|
129
|
get {
|
|
129
|
get {
|
|
130
|
return m_state > 1;
|
|
130
|
return m_state > 1;
|
|
131
|
}
|
|
131
|
}
|
|
132
|
}
|
|
132
|
}
|
|
133
|
|
|
133
|
|
|
134
|
public bool IsCancelled {
|
|
134
|
public bool IsCancelled {
|
|
135
|
get {
|
|
135
|
get {
|
|
136
|
return m_state == CancelledState;
|
|
136
|
return m_state == CancelledState;
|
|
137
|
}
|
|
137
|
}
|
|
138
|
}
|
|
138
|
}
|
|
139
|
|
|
139
|
|
|
140
|
/// <summary>
|
|
140
|
/// <summary>
|
|
141
|
/// ΠΡΠΏΠΎΠ»Π½ΡΠ΅Ρ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΡΠΎΠΎΠ±ΡΠ°Ρ ΠΎΠ± ΡΡΠΏΠ΅ΡΠ½ΠΎΠΌ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ.
|
|
141
|
/// ΠΡΠΏΠΎΠ»Π½ΡΠ΅Ρ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΡΠΎΠΎΠ±ΡΠ°Ρ ΠΎΠ± ΡΡΠΏΠ΅ΡΠ½ΠΎΠΌ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ.
|
|
142
|
/// </summary>
|
|
142
|
/// </summary>
|
|
143
|
/// <param name="result">Π Π΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ.</param>
|
|
143
|
/// <param name="result">Π Π΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ.</param>
|
|
144
|
/// <exception cref="InvalidOperationException">ΠΠ°Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΡΠΆΠ΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ</exception>
|
|
144
|
/// <exception cref="InvalidOperationException">ΠΠ°Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΡΠΆΠ΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ</exception>
|
|
145
|
public void Resolve(T result) {
|
|
145
|
public void Resolve(T result) {
|
|
146
|
if (BeginTransit()) {
|
|
146
|
if (BeginTransit()) {
|
|
147
|
m_result = result;
|
|
147
|
m_result = result;
|
|
148
|
CompleteTransit(SucceededState);
|
|
148
|
CompleteTransit(SucceededState);
|
|
149
|
OnStateChanged();
|
|
149
|
OnStateChanged();
|
|
150
|
} else {
|
|
150
|
} else {
|
|
151
|
WaitTransition();
|
|
151
|
WaitTransition();
|
|
152
|
if (m_state != CancelledState)
|
|
152
|
if (m_state != CancelledState)
|
|
153
|
throw new InvalidOperationException("The promise is already resolved");
|
|
153
|
throw new InvalidOperationException("The promise is already resolved");
|
|
154
|
}
|
|
154
|
}
|
|
155
|
}
|
|
155
|
}
|
|
156
|
|
|
156
|
|
|
157
|
/// <summary>
|
|
157
|
/// <summary>
|
|
158
|
/// ΠΡΠΏΠΎΠ»Π½ΡΠ΅Ρ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΡΠΎΠΎΠ±ΡΠ°Ρ ΠΎΠ± ΠΎΡΠΈΠ±ΠΊΠ΅
|
|
158
|
/// ΠΡΠΏΠΎΠ»Π½ΡΠ΅Ρ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΡΠΎΠΎΠ±ΡΠ°Ρ ΠΎΠ± ΠΎΡΠΈΠ±ΠΊΠ΅
|
|
159
|
/// </summary>
|
|
159
|
/// </summary>
|
|
160
|
/// <remarks>
|
|
160
|
/// <remarks>
|
|
161
|
/// ΠΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π΄ΠΎΠ»ΠΆΠ½ΠΎ ΡΠ°Π±ΠΎΡΠ°ΡΡ Π² ΠΌΠ½ΠΎΠ³ΠΎΠΏΡΠΎΡΠ½ΠΎΠΉ ΡΡΠ΅Π΄Π΅, ΠΏΡΠΈ Π΅Π³ΠΎ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΡΡΠ°Π·Ρ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΏΠΎΡΠΎΠΊΠΎΠ²
|
|
161
|
/// ΠΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π΄ΠΎΠ»ΠΆΠ½ΠΎ ΡΠ°Π±ΠΎΡΠ°ΡΡ Π² ΠΌΠ½ΠΎΠ³ΠΎΠΏΡΠΎΡΠ½ΠΎΠΉ ΡΡΠ΅Π΄Π΅, ΠΏΡΠΈ Π΅Π³ΠΎ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΡΡΠ°Π·Ρ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΏΠΎΡΠΎΠΊΠΎΠ²
|
|
162
|
/// ΠΌΠΎΠ³Ρ Π²Π΅ΡΠ½ΡΡΡ ΠΎΡΠΈΠ±ΠΊΡ, ΠΏΡΠΈ ΡΡΠΎΠΌ ΡΠΎΠ»ΡΠΊΠΎ ΠΏΠ΅ΡΠ²Π°Ρ Π±ΡΠ΄Π΅Ρ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½Π° Π² ΠΊΠ°ΡΠ΅ΡΡΠ²Π΅ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ°, ΠΎΡΡΠ°Π»ΡΠ½ΡΠ΅
|
|
162
|
/// ΠΌΠΎΠ³Ρ Π²Π΅ΡΠ½ΡΡΡ ΠΎΡΠΈΠ±ΠΊΡ, ΠΏΡΠΈ ΡΡΠΎΠΌ ΡΠΎΠ»ΡΠΊΠΎ ΠΏΠ΅ΡΠ²Π°Ρ Π±ΡΠ΄Π΅Ρ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½Π° Π² ΠΊΠ°ΡΠ΅ΡΡΠ²Π΅ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ°, ΠΎΡΡΠ°Π»ΡΠ½ΡΠ΅
|
|
163
|
/// Π±ΡΠ΄ΡΡ ΠΏΡΠΎΠΈΠ³Π½ΠΎΡΠΈΡΠΎΠ²Π°Π½Ρ.
|
|
163
|
/// Π±ΡΠ΄ΡΡ ΠΏΡΠΎΠΈΠ³Π½ΠΎΡΠΈΡΠΎΠ²Π°Π½Ρ.
|
|
164
|
/// </remarks>
|
|
164
|
/// </remarks>
|
|
165
|
/// <param name="error">ΠΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π²ΠΎΠ·Π½ΠΈΠΊΡΠ΅Π΅ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ</param>
|
|
165
|
/// <param name="error">ΠΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π²ΠΎΠ·Π½ΠΈΠΊΡΠ΅Π΅ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ</param>
|
|
166
|
/// <exception cref="InvalidOperationException">ΠΠ°Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΡΠΆΠ΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ</exception>
|
|
166
|
/// <exception cref="InvalidOperationException">ΠΠ°Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΡΠΆΠ΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ</exception>
|
|
167
|
public void Reject(Exception error) {
|
|
167
|
public void Reject(Exception error) {
|
|
168
|
if (BeginTransit()) {
|
|
168
|
if (BeginTransit()) {
|
|
169
|
m_error = error;
|
|
169
|
m_error = error;
|
|
170
|
CompleteTransit(RejectedState);
|
|
170
|
CompleteTransit(RejectedState);
|
|
171
|
OnStateChanged();
|
|
171
|
OnStateChanged();
|
|
172
|
} else {
|
|
172
|
} else {
|
|
173
|
WaitTransition();
|
|
173
|
WaitTransition();
|
|
174
|
if (m_state == SucceededState)
|
|
174
|
if (m_state == SucceededState)
|
|
175
|
throw new InvalidOperationException("The promise is already resolved");
|
|
175
|
throw new InvalidOperationException("The promise is already resolved");
|
|
176
|
}
|
|
176
|
}
|
|
177
|
}
|
|
177
|
}
|
|
178
|
|
|
178
|
|
|
179
|
/// <summary>
|
|
179
|
/// <summary>
|
|
180
|
/// ΠΡΠΌΠ΅Π½ΡΠ΅Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, Π΅ΡΠ»ΠΈ ΡΡΠΎ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ.
|
|
180
|
/// ΠΡΠΌΠ΅Π½ΡΠ΅Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, Π΅ΡΠ»ΠΈ ΡΡΠΎ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ.
|
|
181
|
/// </summary>
|
|
181
|
/// </summary>
|
|
182
|
/// <returns><c>true</c> ΠΠΏΠ΅ΡΠ°ΡΠΈΡ Π±ΡΠ»Π° ΠΎΡΠΌΠ΅Π½Π΅Π½Π°, ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ Π½Π΅ Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ.<c>false</c> ΠΎΡΠΌΠ΅Π½Π° Π½Π΅ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½Π°, ΠΏΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΡΠΆΠ΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ ΠΈ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ ΠΎΡΡΠ°Π±ΠΎΡΠ°Π»ΠΈ.</returns>
|
|
182
|
/// <returns><c>true</c> ΠΠΏΠ΅ΡΠ°ΡΠΈΡ Π±ΡΠ»Π° ΠΎΡΠΌΠ΅Π½Π΅Π½Π°, ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ Π½Π΅ Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ.<c>false</c> ΠΎΡΠΌΠ΅Π½Π° Π½Π΅ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½Π°, ΠΏΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΡΠΆΠ΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ ΠΈ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ ΠΎΡΡΠ°Π±ΠΎΡΠ°Π»ΠΈ.</returns>
|
|
183
|
public bool Cancel() {
|
|
183
|
public bool Cancel() {
|
|
184
|
return Cancel(true);
|
|
184
|
return Cancel(true);
|
|
185
|
}
|
|
185
|
}
|
|
186
|
|
|
186
|
|
|
187
|
/// <summary>
|
|
187
|
/// <summary>
|
|
188
|
/// Adds new handlers to this promise.
|
|
188
|
/// Adds new handlers to this promise.
|
|
189
|
/// </summary>
|
|
189
|
/// </summary>
|
|
190
|
/// <param name="success">The handler of the successfully completed operation.
|
|
190
|
/// <param name="success">The handler of the successfully completed operation.
|
|
191
|
/// This handler will recieve an operation result as a parameter.</param>
|
|
191
|
/// This handler will recieve an operation result as a parameter.</param>
|
|
192
|
/// <param name="error">Handles an exception that may occur during the operation.</param>
|
|
192
|
/// <param name="error">Handles an exception that may occur during the operation.</param>
|
|
193
|
/// <returns>The new promise chained to this one.</returns>
|
|
193
|
/// <returns>The new promise chained to this one.</returns>
|
|
194
|
public Promise<T> Then(ResultHandler<T> success, ErrorHandler error) {
|
|
194
|
public IPromise<T> Then(ResultHandler<T> success, ErrorHandler error) {
|
|
195
|
if (success == null && error == null)
|
|
195
|
if (success == null && error == null)
|
|
196
|
return this;
|
|
196
|
return this;
|
|
197
|
|
|
197
|
|
|
198
|
var medium = new Promise<T>(this, true);
|
|
198
|
var medium = new Promise<T>(this, true);
|
|
199
|
|
|
199
|
|
|
200
|
ResultHandler<T> resultHandler;
|
|
200
|
ResultHandler<T> resultHandler;
|
|
201
|
if (success != null)
|
|
201
|
if (success != null)
|
|
202
|
resultHandler = x => {
|
|
202
|
resultHandler = x => {
|
|
203
|
success(x);
|
|
203
|
success(x);
|
|
204
|
medium.Resolve(x);
|
|
204
|
medium.Resolve(x);
|
|
205
|
};
|
|
205
|
};
|
|
206
|
else
|
|
206
|
else
|
|
207
|
resultHandler = medium.Resolve;
|
|
207
|
resultHandler = medium.Resolve;
|
|
208
|
|
|
208
|
|
|
209
|
ErrorHandler errorHandler;
|
|
209
|
ErrorHandler errorHandler;
|
|
210
|
if (error != null)
|
|
210
|
if (error != null)
|
|
211
|
errorHandler = x => {
|
|
211
|
errorHandler = x => {
|
|
212
|
// Π½Π΅ΡΠΌΠΎΡΡΡ Π½Π° ΡΠΎ, ΡΡΠΎ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΎΡΠΈΠ±ΠΊΠΈ Π²ΡΠ·ΡΠ²Π°Π΅ΡΡΡ Π±Π΅Π·ΠΎΠΏΠ°ΡΠ½ΠΎ,
|
|
212
|
// Π½Π΅ΡΠΌΠΎΡΡΡ Π½Π° ΡΠΎ, ΡΡΠΎ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΎΡΠΈΠ±ΠΊΠΈ Π²ΡΠ·ΡΠ²Π°Π΅ΡΡΡ Π±Π΅Π·ΠΎΠΏΠ°ΡΠ½ΠΎ,
|
|
213
|
// Ρ.Π΅. Π²ΠΎΠ·Π½ΠΈΠΊΡΠΈΠ΅ Π² Π½Π΅ΠΌ ΠΎΡΠΈΠ±ΠΊΠΈ Π±ΡΠ΄ΡΡ ΠΏΠΎΠ΄Π°Π²Π»Π΅Π½Ρ, Π½Π°ΠΌ Π½ΡΠΆΠ½ΠΎ
|
|
213
|
// Ρ.Π΅. Π²ΠΎΠ·Π½ΠΈΠΊΡΠΈΠ΅ Π² Π½Π΅ΠΌ ΠΎΡΠΈΠ±ΠΊΠΈ Π±ΡΠ΄ΡΡ ΠΏΠΎΠ΄Π°Π²Π»Π΅Π½Ρ, Π½Π°ΠΌ Π½ΡΠΆΠ½ΠΎ
|
|
214
|
// Π³Π°ΡΠ°Π½ΡΠΈΡΠΎΠ²Π°ΡΡ, ΡΡΠΎ ΠΎΡΠΈΠ±ΠΊΠ° Π±ΡΠ΄Π΅Ρ ΠΏΠ΅ΡΠ΅Π΄Π°Π½Π° Π΄Π°Π»ΡΡΠ΅ ΠΏΠΎ ΡΠ΅ΠΏΠΎΡΠΊΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ
|
|
214
|
// Π³Π°ΡΠ°Π½ΡΠΈΡΠΎΠ²Π°ΡΡ, ΡΡΠΎ ΠΎΡΠΈΠ±ΠΊΠ° Π±ΡΠ΄Π΅Ρ ΠΏΠ΅ΡΠ΅Π΄Π°Π½Π° Π΄Π°Π»ΡΡΠ΅ ΠΏΠΎ ΡΠ΅ΠΏΠΎΡΠΊΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ
|
|
215
|
try {
|
|
215
|
try {
|
|
216
|
error(x);
|
|
216
|
error(x);
|
|
217
|
} catch { }
|
|
217
|
} catch { }
|
|
218
|
medium.Reject(x);
|
|
218
|
medium.Reject(x);
|
|
219
|
};
|
|
219
|
};
|
|
220
|
else
|
|
220
|
else
|
|
221
|
errorHandler = medium.Reject;
|
|
221
|
errorHandler = medium.Reject;
|
|
222
|
|
|
222
|
|
|
223
|
AddHandler(resultHandler, errorHandler, medium.InternalCancel);
|
|
223
|
AddHandler(resultHandler, errorHandler, medium.InternalCancel);
|
|
224
|
|
|
224
|
|
|
225
|
return medium;
|
|
225
|
return medium;
|
|
226
|
}
|
|
226
|
}
|
|
227
|
|
|
227
|
|
|
|
|
|
228
|
public IPromiseBase Then(Action success,ErrorHandler error)
|
|
|
|
|
229
|
{
|
|
|
|
|
230
|
return Then(x => success(), error);
|
|
|
|
|
231
|
}
|
|
|
|
|
232
|
|
|
|
|
|
233
|
public IPromiseBase Then(Action success)
|
|
|
|
|
234
|
{
|
|
|
|
|
235
|
return Then(success);
|
|
|
|
|
236
|
}
|
|
|
|
|
237
|
|
|
228
|
/// <summary>
|
|
238
|
/// <summary>
|
|
229
|
/// Adds new handlers to this promise.
|
|
239
|
/// Adds new handlers to this promise.
|
|
230
|
/// </summary>
|
|
240
|
/// </summary>
|
|
231
|
/// <param name="success">The handler of the successfully completed operation.
|
|
241
|
/// <param name="success">The handler of the successfully completed operation.
|
|
232
|
/// This handler will recieve an operation result as a parameter.</param>
|
|
242
|
/// This handler will recieve an operation result as a parameter.</param>
|
|
233
|
/// <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>
|
|
243
|
/// <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
|
/// <returns>The new promise chained to this one.</returns>
|
|
244
|
/// <returns>The new promise chained to this one.</returns>
|
|
235
|
public Promise<T> Then(ResultHandler<T> success, ErrorHandler<T> error) {
|
|
245
|
public IPromise<T> Then(ResultHandler<T> success, ErrorHandler<T> error) {
|
|
236
|
if (success == null && error == null)
|
|
246
|
if (success == null && error == null)
|
|
237
|
return this;
|
|
247
|
return this;
|
|
238
|
|
|
248
|
|
|
239
|
var medium = new Promise<T>(this, true);
|
|
249
|
var medium = new Promise<T>(this, true);
|
|
240
|
|
|
250
|
|
|
241
|
ResultHandler<T> resultHandler;
|
|
251
|
ResultHandler<T> resultHandler;
|
|
242
|
ErrorHandler errorHandler;
|
|
252
|
ErrorHandler errorHandler;
|
|
243
|
|
|
253
|
|
|
244
|
if (success != null)
|
|
254
|
if (success != null)
|
|
245
|
resultHandler = x => {
|
|
255
|
resultHandler = x => {
|
|
246
|
success(x);
|
|
256
|
success(x);
|
|
247
|
medium.Resolve(x);
|
|
257
|
medium.Resolve(x);
|
|
248
|
};
|
|
258
|
};
|
|
249
|
else
|
|
259
|
else
|
|
250
|
resultHandler = medium.Resolve;
|
|
260
|
resultHandler = medium.Resolve;
|
|
251
|
|
|
261
|
|
|
252
|
if (error != null)
|
|
262
|
if (error != null)
|
|
253
|
errorHandler = x => {
|
|
263
|
errorHandler = x => {
|
|
254
|
try {
|
|
264
|
try {
|
|
255
|
medium.Resolve(error(x));
|
|
265
|
medium.Resolve(error(x));
|
|
256
|
} catch(Exception e) {
|
|
266
|
} catch(Exception e) {
|
|
257
|
medium.Reject(e);
|
|
267
|
medium.Reject(e);
|
|
258
|
}
|
|
268
|
}
|
|
259
|
};
|
|
269
|
};
|
|
260
|
else
|
|
270
|
else
|
|
261
|
errorHandler = medium.Reject;
|
|
271
|
errorHandler = medium.Reject;
|
|
262
|
|
|
272
|
|
|
263
|
AddHandler(resultHandler, errorHandler, medium.InternalCancel);
|
|
273
|
AddHandler(resultHandler, errorHandler, medium.InternalCancel);
|
|
264
|
|
|
274
|
|
|
265
|
return medium;
|
|
275
|
return medium;
|
|
266
|
}
|
|
276
|
}
|
|
267
|
|
|
277
|
|
|
268
|
|
|
278
|
|
|
269
|
public Promise<T> Then(ResultHandler<T> success) {
|
|
279
|
public IPromise<T> Then(ResultHandler<T> success) {
|
|
270
|
if (success == null)
|
|
280
|
if (success == null)
|
|
271
|
return this;
|
|
281
|
return this;
|
|
272
|
|
|
282
|
|
|
273
|
var medium = new Promise<T>(this, true);
|
|
283
|
var medium = new Promise<T>(this, true);
|
|
274
|
|
|
284
|
|
|
275
|
ResultHandler<T> resultHandler;
|
|
285
|
ResultHandler<T> resultHandler;
|
|
276
|
|
|
286
|
|
|
277
|
if (success != null)
|
|
287
|
if (success != null)
|
|
278
|
resultHandler = x => {
|
|
288
|
resultHandler = x => {
|
|
279
|
success(x);
|
|
289
|
success(x);
|
|
280
|
medium.Resolve(x);
|
|
290
|
medium.Resolve(x);
|
|
281
|
};
|
|
291
|
};
|
|
282
|
else
|
|
292
|
else
|
|
283
|
resultHandler = medium.Resolve;
|
|
293
|
resultHandler = medium.Resolve;
|
|
284
|
|
|
294
|
|
|
285
|
AddHandler(resultHandler, medium.Reject, medium.InternalCancel);
|
|
295
|
AddHandler(resultHandler, medium.Reject, medium.InternalCancel);
|
|
286
|
|
|
296
|
|
|
287
|
return medium;
|
|
297
|
return medium;
|
|
288
|
}
|
|
298
|
}
|
|
289
|
|
|
299
|
|
|
290
|
public Promise<T> Error(ErrorHandler error) {
|
|
300
|
public IPromise<T> Error(ErrorHandler error) {
|
|
291
|
return Then(null, error);
|
|
301
|
return Then((ResultHandler<T>)null, error);
|
|
292
|
}
|
|
302
|
}
|
|
293
|
|
|
303
|
|
|
294
|
/// <summary>
|
|
304
|
/// <summary>
|
|
295
|
/// Handles error and allows to keep the promise.
|
|
305
|
/// Handles error and allows to keep the promise.
|
|
296
|
/// </summary>
|
|
306
|
/// </summary>
|
|
297
|
/// <remarks>
|
|
307
|
/// <remarks>
|
|
298
|
/// If the specified handler throws an exception, this exception will be used to reject the promise.
|
|
308
|
/// If the specified handler throws an exception, this exception will be used to reject the promise.
|
|
299
|
/// </remarks>
|
|
309
|
/// </remarks>
|
|
300
|
/// <param name="handler">The error handler which returns the result of the promise.</param>
|
|
310
|
/// <param name="handler">The error handler which returns the result of the promise.</param>
|
|
301
|
/// <returns>New promise.</returns>
|
|
311
|
/// <returns>New promise.</returns>
|
|
302
|
public Promise<T> Error(ErrorHandler<T> handler) {
|
|
312
|
public IPromise<T> Error(ErrorHandler<T> handler) {
|
|
303
|
if (handler == null)
|
|
313
|
if (handler == null)
|
|
304
|
return this;
|
|
314
|
return this;
|
|
305
|
|
|
315
|
|
|
306
|
var medium = new Promise<T>(this, true);
|
|
316
|
var medium = new Promise<T>(this, true);
|
|
307
|
|
|
317
|
|
|
308
|
AddHandler(
|
|
318
|
AddHandler(
|
|
309
|
null,
|
|
319
|
null,
|
|
310
|
e => {
|
|
320
|
e => {
|
|
311
|
try {
|
|
321
|
try {
|
|
312
|
medium.Resolve(handler(e));
|
|
322
|
medium.Resolve(handler(e));
|
|
313
|
} catch (Exception e2) {
|
|
323
|
} catch (Exception e2) {
|
|
314
|
medium.Reject(e2);
|
|
324
|
medium.Reject(e2);
|
|
315
|
}
|
|
325
|
}
|
|
316
|
},
|
|
326
|
},
|
|
317
|
medium.InternalCancel
|
|
327
|
medium.InternalCancel
|
|
318
|
);
|
|
328
|
);
|
|
319
|
|
|
329
|
|
|
320
|
return medium;
|
|
330
|
return medium;
|
|
321
|
}
|
|
331
|
}
|
|
322
|
|
|
332
|
|
|
323
|
public Promise<T> Anyway(Action handler) {
|
|
333
|
public IPromise<T> Anyway(Action handler) {
|
|
324
|
if (handler == null)
|
|
334
|
if (handler == null)
|
|
325
|
return this;
|
|
335
|
return this;
|
|
326
|
|
|
336
|
|
|
327
|
var medium = new Promise<T>();
|
|
337
|
var medium = new Promise<T>();
|
|
328
|
|
|
338
|
|
|
329
|
AddHandler(
|
|
339
|
AddHandler(
|
|
330
|
x => {
|
|
340
|
x => {
|
|
331
|
// to avoid handler being called multiple times we handle exception by ourselfs
|
|
341
|
// to avoid handler being called multiple times we handle exception by ourselfs
|
|
332
|
try {
|
|
342
|
try {
|
|
333
|
handler();
|
|
343
|
handler();
|
|
334
|
medium.Resolve(x);
|
|
344
|
medium.Resolve(x);
|
|
335
|
} catch (Exception e) {
|
|
345
|
} catch (Exception e) {
|
|
336
|
medium.Reject(e);
|
|
346
|
medium.Reject(e);
|
|
337
|
}
|
|
347
|
}
|
|
338
|
},
|
|
348
|
},
|
|
339
|
|
|
349
|
|
|
340
|
e => {
|
|
350
|
e => {
|
|
341
|
try {
|
|
351
|
try {
|
|
342
|
handler();
|
|
352
|
handler();
|
|
343
|
} catch { }
|
|
353
|
} catch { }
|
|
344
|
medium.Reject(e);
|
|
354
|
medium.Reject(e);
|
|
345
|
},
|
|
355
|
},
|
|
346
|
|
|
356
|
|
|
347
|
medium.InternalCancel
|
|
357
|
medium.InternalCancel
|
|
348
|
);
|
|
358
|
);
|
|
349
|
|
|
359
|
|
|
350
|
return medium;
|
|
360
|
return medium;
|
|
351
|
}
|
|
361
|
}
|
|
352
|
|
|
362
|
|
|
353
|
/// <summary>
|
|
363
|
/// <summary>
|
|
354
|
/// ΠΠΎΠ·Π²ΠΎΠ»ΡΠ΅Ρ ΠΏΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²Π°ΡΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΠΏΠΎΠ»Π΅Π½ΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΠΊ Π½ΠΎΠ²ΠΎΠΌΡ ΡΠΈΠΏΡ.
|
|
364
|
/// ΠΠΎΠ·Π²ΠΎΠ»ΡΠ΅Ρ ΠΏΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²Π°ΡΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΠΏΠΎΠ»Π΅Π½ΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΠΊ Π½ΠΎΠ²ΠΎΠΌΡ ΡΠΈΠΏΡ.
|
|
355
|
/// </summary>
|
|
365
|
/// </summary>
|
|
356
|
/// <typeparam name="TNew">ΠΠΎΠ²ΡΠΉ ΡΠΈΠΏ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ°.</typeparam>
|
|
366
|
/// <typeparam name="TNew">ΠΠΎΠ²ΡΠΉ ΡΠΈΠΏ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ°.</typeparam>
|
|
357
|
/// <param name="mapper">ΠΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²Π°Π½ΠΈΠ΅ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ° ΠΊ Π½ΠΎΠ²ΠΎΠΌΡ ΡΠΈΠΏΡ.</param>
|
|
367
|
/// <param name="mapper">ΠΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²Π°Π½ΠΈΠ΅ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ° ΠΊ Π½ΠΎΠ²ΠΎΠΌΡ ΡΠΈΠΏΡ.</param>
|
|
358
|
/// <param name="error">ΠΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΎΡΠΈΠ±ΠΊΠΈ. ΠΠ°Π½Π½ΡΠΉ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΏΠΎΠ»ΡΡΠΈΡ
|
|
368
|
/// <param name="error">ΠΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΎΡΠΈΠ±ΠΊΠΈ. ΠΠ°Π½Π½ΡΠΉ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΏΠΎΠ»ΡΡΠΈΡ
|
|
359
|
/// ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π²ΠΎΠ·Π½ΠΈΠΊΡΠ΅Π΅ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</param>
|
|
369
|
/// ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π²ΠΎΠ·Π½ΠΈΠΊΡΠ΅Π΅ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</param>
|
|
360
|
/// <returns>ΠΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΠΊΠΎΡΠΎΡΠΎΠ΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΈΡΡ
ΠΎΠ΄Π½ΠΎΠ³ΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ.</returns>
|
|
370
|
/// <returns>ΠΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΠΊΠΎΡΠΎΡΠΎΠ΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΈΡΡ
ΠΎΠ΄Π½ΠΎΠ³ΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ.</returns>
|
|
361
|
public Promise<TNew> Map<TNew>(ResultMapper<T, TNew> mapper, ErrorHandler error) {
|
|
371
|
public IPromise<TNew> Map<TNew>(ResultMapper<T, TNew> mapper, ErrorHandler error) {
|
|
362
|
if (mapper == null)
|
|
372
|
if (mapper == null)
|
|
363
|
throw new ArgumentNullException("mapper");
|
|
373
|
throw new ArgumentNullException("mapper");
|
|
364
|
|
|
374
|
|
|
365
|
// ΡΠΎΠ·Π΄Π°Π΅ΠΌ ΠΏΡΠΈΡΠ΅ΠΏΠ»Π΅Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅
|
|
375
|
// ΡΠΎΠ·Π΄Π°Π΅ΠΌ ΠΏΡΠΈΡΠ΅ΠΏΠ»Π΅Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅
|
|
366
|
var chained = new Promise<TNew>();
|
|
376
|
var chained = new Promise<TNew>();
|
|
367
|
|
|
377
|
|
|
368
|
ResultHandler<T> resultHandler = result => chained.Resolve(mapper(result));
|
|
378
|
ResultHandler<T> resultHandler = result => chained.Resolve(mapper(result));
|
|
369
|
ErrorHandler errorHandler = delegate(Exception e) {
|
|
379
|
ErrorHandler errorHandler = delegate(Exception e) {
|
|
370
|
if (error != null)
|
|
380
|
if (error != null)
|
|
371
|
try {
|
|
381
|
try {
|
|
372
|
error(e);
|
|
382
|
error(e);
|
|
373
|
} catch { }
|
|
383
|
} catch { }
|
|
374
|
// Π² ΡΠ»ΡΡΠ°Π΅ ΠΎΡΠΈΠ±ΠΊΠΈ Π½ΡΠΆΠ½ΠΎ ΠΏΠ΅ΡΠ΅Π΄Π°ΡΡ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π΄Π°Π»ΡΡΠ΅ ΠΏΠΎ ΡΠ΅ΠΏΠΎΡΠΊΠ΅
|
|
384
|
// Π² ΡΠ»ΡΡΠ°Π΅ ΠΎΡΠΈΠ±ΠΊΠΈ Π½ΡΠΆΠ½ΠΎ ΠΏΠ΅ΡΠ΅Π΄Π°ΡΡ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π΄Π°Π»ΡΡΠ΅ ΠΏΠΎ ΡΠ΅ΠΏΠΎΡΠΊΠ΅
|
|
375
|
chained.Reject(e);
|
|
385
|
chained.Reject(e);
|
|
376
|
};
|
|
386
|
};
|
|
377
|
|
|
387
|
|
|
378
|
|
|
388
|
|
|
379
|
AddHandler(
|
|
389
|
AddHandler(
|
|
380
|
resultHandler,
|
|
390
|
resultHandler,
|
|
381
|
errorHandler,
|
|
391
|
errorHandler,
|
|
382
|
chained.InternalCancel
|
|
392
|
chained.InternalCancel
|
|
383
|
);
|
|
393
|
);
|
|
384
|
|
|
394
|
|
|
385
|
return chained;
|
|
395
|
return chained;
|
|
386
|
}
|
|
396
|
}
|
|
387
|
|
|
397
|
|
|
388
|
public Promise<TNew> Map<TNew>(ResultMapper<T, TNew> mapper) {
|
|
398
|
public IPromise<TNew> Map<TNew>(ResultMapper<T, TNew> mapper) {
|
|
389
|
return Map(mapper, null);
|
|
399
|
return Map(mapper, null);
|
|
390
|
}
|
|
400
|
}
|
|
391
|
|
|
401
|
|
|
392
|
/// <summary>
|
|
402
|
/// <summary>
|
|
393
|
/// Π‘ΡΠ΅ΠΏΠ»ΡΠ΅Ρ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ Π°ΡΠ½Ρ
ΡΠΎΠ½Π½ΡΡ
ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ. Π£ΠΊΠ°Π·Π°Π½Π½Π°Ρ Π°ΡΠΈΠ½Ρ
ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ Π±ΡΠ΄Π΅Ρ Π²ΡΠ·Π²Π°Π½Π° ΠΏΠΎΡΠ»Π΅
|
|
403
|
/// Π‘ΡΠ΅ΠΏΠ»ΡΠ΅Ρ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ Π°ΡΠ½Ρ
ΡΠΎΠ½Π½ΡΡ
ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ. Π£ΠΊΠ°Π·Π°Π½Π½Π°Ρ Π°ΡΠΈΠ½Ρ
ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ Π±ΡΠ΄Π΅Ρ Π²ΡΠ·Π²Π°Π½Π° ΠΏΠΎΡΠ»Π΅
|
|
394
|
/// Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΡΠ΅ΠΊΡΡΠ΅ΠΉ, Π° ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ ΡΠ΅ΠΊΡΡΠ΅ΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ Π΄Π»Ρ ΠΈΠ½ΠΈΡΠΈΠ°Π»ΠΈΠ·Π°ΡΠΈΠΈ
|
|
404
|
/// Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΡΠ΅ΠΊΡΡΠ΅ΠΉ, Π° ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ ΡΠ΅ΠΊΡΡΠ΅ΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ Π΄Π»Ρ ΠΈΠ½ΠΈΡΠΈΠ°Π»ΠΈΠ·Π°ΡΠΈΠΈ
|
|
395
|
/// Π½ΠΎΠ²ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.
|
|
405
|
/// Π½ΠΎΠ²ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.
|
|
396
|
/// </summary>
|
|
406
|
/// </summary>
|
|
397
|
/// <typeparam name="TNew">Π’ΠΈΠΏ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ° ΡΠΊΠ°Π·Π°Π½Π½ΠΎΠΉ Π°ΡΠΈΠ½Ρ
ΡΠΎΠ½Π½ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</typeparam>
|
|
407
|
/// <typeparam name="TNew">Π’ΠΈΠΏ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ° ΡΠΊΠ°Π·Π°Π½Π½ΠΎΠΉ Π°ΡΠΈΠ½Ρ
ΡΠΎΠ½Π½ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</typeparam>
|
|
398
|
/// <param name="chained">ΠΡΠΈΠ½Ρ
ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, ΠΊΠΎΡΠΎΡΠ°Ρ Π΄ΠΎΠ»ΠΆΠ½Π° Π±ΡΠ΄Π΅Ρ Π½Π°ΡΠ°ΡΡΡΡ ΠΏΠΎΡΠ»Π΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΡΠ΅ΠΊΡΡΠ΅ΠΉ.</param>
|
|
408
|
/// <param name="chained">ΠΡΠΈΠ½Ρ
ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, ΠΊΠΎΡΠΎΡΠ°Ρ Π΄ΠΎΠ»ΠΆΠ½Π° Π±ΡΠ΄Π΅Ρ Π½Π°ΡΠ°ΡΡΡΡ ΠΏΠΎΡΠ»Π΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΡΠ΅ΠΊΡΡΠ΅ΠΉ.</param>
|
|
399
|
/// <param name="error">ΠΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΎΡΠΈΠ±ΠΊΠΈ. ΠΠ°Π½Π½ΡΠΉ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΏΠΎΠ»ΡΡΠΈΡ
|
|
409
|
/// <param name="error">ΠΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΎΡΠΈΠ±ΠΊΠΈ. ΠΠ°Π½Π½ΡΠΉ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΏΠΎΠ»ΡΡΠΈΡ
|
|
400
|
/// ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π²ΠΎΠ·Π½ΠΈΠΊΡΠ΅Π΅ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΡΠ΅ΠΊΡΠ΅ΡΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</param>
|
|
410
|
/// ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π²ΠΎΠ·Π½ΠΈΠΊΡΠ΅Π΅ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΡΠ΅ΠΊΡΠ΅ΡΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</param>
|
|
401
|
/// <returns>ΠΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΠΊΠΎΡΠΎΡΠΎΠ΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ ΠΏΠΎ ΠΎΠΊΠΎΠ½ΡΠ°Π½ΠΈΡ ΡΠΊΠ°Π·Π°Π½Π½ΠΎΠΉ Π°ΡΠ½Ρ
ΡΠΎΠ½Π½ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</returns>
|
|
411
|
/// <returns>ΠΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΠΊΠΎΡΠΎΡΠΎΠ΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ ΠΏΠΎ ΠΎΠΊΠΎΠ½ΡΠ°Π½ΠΈΡ ΡΠΊΠ°Π·Π°Π½Π½ΠΎΠΉ Π°ΡΠ½Ρ
ΡΠΎΠ½Π½ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</returns>
|
|
402
|
public Promise<TNew> Chain<TNew>(ChainedOperation<T, TNew> chained, ErrorHandler error) {
|
|
412
|
public IPromise<TNew> Chain<TNew>(ChainedOperation<T, TNew> chained, ErrorHandler error) {
|
|
403
|
|
|
413
|
|
|
404
|
// ΠΏΡΠΎΠ±Π»Π΅ΠΌΠ° Π² ΡΠΎΠΌ, ΡΡΠΎ Π½Π° ΠΌΠΎΠΌΠ΅Π½Ρ ΡΠ²ΡΠ·ΡΠ²Π°Π½ΠΈΡ Π΅ΡΠ΅ Π½Π΅ Π½Π°ΡΠ°ΡΠ° Π°ΡΠΈΠ½Ρ
ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, ΠΏΠΎΡΡΠΎΠΌΡ Π½ΡΠΆΠ½ΠΎ
|
|
414
|
// ΠΏΡΠΎΠ±Π»Π΅ΠΌΠ° Π² ΡΠΎΠΌ, ΡΡΠΎ Π½Π° ΠΌΠΎΠΌΠ΅Π½Ρ ΡΠ²ΡΠ·ΡΠ²Π°Π½ΠΈΡ Π΅ΡΠ΅ Π½Π΅ Π½Π°ΡΠ°ΡΠ° Π°ΡΠΈΠ½Ρ
ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, ΠΏΠΎΡΡΠΎΠΌΡ Π½ΡΠΆΠ½ΠΎ
|
|
405
|
// ΡΠΎΠ·Π΄Π°ΡΡ ΠΏΠΎΡΡΠ΅Π΄Π½ΠΈΠΊΠ°, ΠΊ ΠΊΠΎΡΠΎΡΠΎΠΌΡ Π±ΡΠ΄ΡΡ ΠΏΠΎΠ΄Π²ΡΠ·ΡΠ²Π°ΡΡΡΡ ΡΠ»Π΅Π΄ΡΡΡΠΈΠ΅ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ.
|
|
415
|
// ΡΠΎΠ·Π΄Π°ΡΡ ΠΏΠΎΡΡΠ΅Π΄Π½ΠΈΠΊΠ°, ΠΊ ΠΊΠΎΡΠΎΡΠΎΠΌΡ Π±ΡΠ΄ΡΡ ΠΏΠΎΠ΄Π²ΡΠ·ΡΠ²Π°ΡΡΡΡ ΡΠ»Π΅Π΄ΡΡΡΠΈΠ΅ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ.
|
|
406
|
// ΠΊΠΎΠ³Π΄Π° Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½Π° ΡΠ΅Π°Π»ΡΠ½Π°Ρ Π°ΡΠΈΠ½Ρ
ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, ΠΎΠ½Π° ΠΎΠ±ΡΠ°ΡΠΈΡΡΡΡ ΠΊ ΠΏΠΎΡΡΠ΅Π΄Π½ΠΈΠΊΡ, ΡΡΠΎΠ±Ρ
|
|
416
|
// ΠΊΠΎΠ³Π΄Π° Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½Π° ΡΠ΅Π°Π»ΡΠ½Π°Ρ Π°ΡΠΈΠ½Ρ
ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, ΠΎΠ½Π° ΠΎΠ±ΡΠ°ΡΠΈΡΡΡΡ ΠΊ ΠΏΠΎΡΡΠ΅Π΄Π½ΠΈΠΊΡ, ΡΡΠΎΠ±Ρ
|
|
407
|
// ΠΏΠ΅ΡΠ΅Π΄Π°ΡΡ ΡΠ΅ΡΠ΅Π· Π½Π΅Π³ΠΎ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ ΡΠ°Π±ΠΎΡΡ.
|
|
417
|
// ΠΏΠ΅ΡΠ΅Π΄Π°ΡΡ ΡΠ΅ΡΠ΅Π· Π½Π΅Π³ΠΎ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ ΡΠ°Π±ΠΎΡΡ.
|
|
408
|
var medium = new Promise<TNew>(this, true);
|
|
418
|
var medium = new Promise<TNew>(this, true);
|
|
409
|
|
|
419
|
|
|
410
|
ResultHandler<T> resultHandler = delegate(T result) {
|
|
420
|
ResultHandler<T> resultHandler = delegate(T result) {
|
|
411
|
if (medium.IsCancelled)
|
|
421
|
if (medium.IsCancelled)
|
|
412
|
return;
|
|
422
|
return;
|
|
413
|
|
|
423
|
|
|
414
|
var promise = chained(result);
|
|
424
|
var promise = chained(result);
|
|
415
|
|
|
425
|
|
|
416
|
// notify chained operation that it's not needed
|
|
426
|
// notify chained operation that it's not needed
|
|
417
|
medium.Cancelled(() => promise.Cancel());
|
|
427
|
medium.Cancelled(() => promise.Cancel());
|
|
418
|
promise.Then(
|
|
428
|
promise.Then(
|
|
419
|
x => medium.Resolve(x),
|
|
429
|
x => medium.Resolve(x),
|
|
420
|
e => medium.Reject(e)
|
|
430
|
e => medium.Reject(e)
|
|
421
|
);
|
|
431
|
);
|
|
422
|
};
|
|
432
|
};
|
|
423
|
|
|
433
|
|
|
424
|
ErrorHandler errorHandler = delegate(Exception e) {
|
|
434
|
ErrorHandler errorHandler = delegate(Exception e) {
|
|
425
|
if (error != null)
|
|
435
|
if (error != null)
|
|
426
|
error(e);
|
|
436
|
error(e);
|
|
427
|
// Π² ΡΠ»ΡΡΠ°Π΅ ΠΎΡΠΈΠ±ΠΊΠΈ Π½ΡΠΆΠ½ΠΎ ΠΏΠ΅ΡΠ΅Π΄Π°ΡΡ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π΄Π°Π»ΡΡΠ΅ ΠΏΠΎ ΡΠ΅ΠΏΠΎΡΠΊΠ΅
|
|
437
|
// Π² ΡΠ»ΡΡΠ°Π΅ ΠΎΡΠΈΠ±ΠΊΠΈ Π½ΡΠΆΠ½ΠΎ ΠΏΠ΅ΡΠ΅Π΄Π°ΡΡ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π΄Π°Π»ΡΡΠ΅ ΠΏΠΎ ΡΠ΅ΠΏΠΎΡΠΊΠ΅
|
|
428
|
medium.Reject(e);
|
|
438
|
medium.Reject(e);
|
|
429
|
};
|
|
439
|
};
|
|
430
|
|
|
440
|
|
|
431
|
AddHandler(
|
|
441
|
AddHandler(
|
|
432
|
resultHandler,
|
|
442
|
resultHandler,
|
|
433
|
errorHandler,
|
|
443
|
errorHandler,
|
|
434
|
medium.InternalCancel
|
|
444
|
medium.InternalCancel
|
|
435
|
);
|
|
445
|
);
|
|
436
|
|
|
446
|
|
|
437
|
return medium;
|
|
447
|
return medium;
|
|
438
|
}
|
|
448
|
}
|
|
439
|
|
|
449
|
|
|
440
|
public Promise<TNew> Chain<TNew>(ChainedOperation<T, TNew> chained) {
|
|
450
|
public IPromise<TNew> Chain<TNew>(ChainedOperation<T, TNew> chained) {
|
|
441
|
return Chain(chained, null);
|
|
451
|
return Chain(chained, null);
|
|
442
|
}
|
|
452
|
}
|
|
443
|
|
|
453
|
|
|
444
|
public Promise<T> Cancelled(Action handler) {
|
|
454
|
public IPromise<T> Cancelled(Action handler) {
|
|
445
|
AddHandler(null, null, handler);
|
|
455
|
AddHandler(null, null, handler);
|
|
446
|
return this;
|
|
456
|
return this;
|
|
447
|
}
|
|
457
|
}
|
|
448
|
|
|
458
|
|
|
449
|
/// <summary>
|
|
459
|
/// <summary>
|
|
450
|
/// Adds the specified handler for all cases (success, error, cancel)
|
|
460
|
/// Adds the specified handler for all cases (success, error, cancel)
|
|
451
|
/// </summary>
|
|
461
|
/// </summary>
|
|
452
|
/// <param name="handler">The handler that will be called anyway</param>
|
|
462
|
/// <param name="handler">The handler that will be called anyway</param>
|
|
453
|
/// <returns>self</returns>
|
|
463
|
/// <returns>self</returns>
|
|
454
|
public Promise<T> Finally(Action handler) {
|
|
464
|
public IPromise<T> Finally(Action handler) {
|
|
455
|
if (handler == null)
|
|
465
|
if (handler == null)
|
|
456
|
throw new ArgumentNullException("handler");
|
|
466
|
throw new ArgumentNullException("handler");
|
|
457
|
AddHandler(
|
|
467
|
AddHandler(
|
|
458
|
x => handler(),
|
|
468
|
x => handler(),
|
|
459
|
e => handler(),
|
|
469
|
e => handler(),
|
|
460
|
handler
|
|
470
|
handler
|
|
461
|
);
|
|
471
|
);
|
|
462
|
return this;
|
|
472
|
return this;
|
|
463
|
}
|
|
473
|
}
|
|
464
|
|
|
474
|
|
|
465
|
/// <summary>
|
|
475
|
/// <summary>
|
|
466
|
/// ΠΠΎΠΆΠΈΠ΄Π°Π΅ΡΡΡ ΠΎΡΠ»ΠΎΠΆΠ΅Π½Π½ΠΎΠ³ΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΈ Π² ΡΠ»ΡΡΠ°Π΅ ΡΡΠΏΠ΅Ρ
Π°, Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅Ρ
|
|
476
|
/// ΠΠΎΠΆΠΈΠ΄Π°Π΅ΡΡΡ ΠΎΡΠ»ΠΎΠΆΠ΅Π½Π½ΠΎΠ³ΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΈ Π² ΡΠ»ΡΡΠ°Π΅ ΡΡΠΏΠ΅Ρ
Π°, Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅Ρ
|
|
467
|
/// Π΅Π³ΠΎ, ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ, Π² ΠΏΡΠΎΡΠΈΠ²Π½ΠΎΠΌ ΡΠ»ΡΡΠ°Π΅ Π±ΡΠΎΡΠ°Π΅Ρ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅.
|
|
477
|
/// Π΅Π³ΠΎ, ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ, Π² ΠΏΡΠΎΡΠΈΠ²Π½ΠΎΠΌ ΡΠ»ΡΡΠ°Π΅ Π±ΡΠΎΡΠ°Π΅Ρ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅.
|
|
468
|
/// </summary>
|
|
478
|
/// </summary>
|
|
469
|
/// <remarks>
|
|
479
|
/// <remarks>
|
|
470
|
/// <para>
|
|
480
|
/// <para>
|
|
471
|
/// ΠΡΠ»ΠΈ ΠΎΠΆΠΈΠ΄Π°Π½ΠΈΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ Π±ΡΠ»ΠΎ ΠΏΡΠ΅ΡΠ²Π°Π½ΠΎ ΠΏΠΎ ΡΠ°ΠΉΠΌΠ°ΡΡΡ, ΡΡΠΎ Π½Π΅ Π·Π½Π°ΡΠΈΡ,
|
|
481
|
/// ΠΡΠ»ΠΈ ΠΎΠΆΠΈΠ΄Π°Π½ΠΈΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ Π±ΡΠ»ΠΎ ΠΏΡΠ΅ΡΠ²Π°Π½ΠΎ ΠΏΠΎ ΡΠ°ΠΉΠΌΠ°ΡΡΡ, ΡΡΠΎ Π½Π΅ Π·Π½Π°ΡΠΈΡ,
|
|
472
|
/// ΡΡΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π±ΡΠ»ΠΎ ΠΎΡΠΌΠ΅Π½Π΅Π½ΠΎ ΠΈΠ»ΠΈ ΡΡΠΎ-ΡΠΎ Π² ΡΡΠΎΠΌ ΡΠΎΠ΄Π΅, ΡΡΠΎ ΡΠΎΠ»ΡΠΊΠΎ
|
|
482
|
/// ΡΡΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π±ΡΠ»ΠΎ ΠΎΡΠΌΠ΅Π½Π΅Π½ΠΎ ΠΈΠ»ΠΈ ΡΡΠΎ-ΡΠΎ Π² ΡΡΠΎΠΌ ΡΠΎΠ΄Π΅, ΡΡΠΎ ΡΠΎΠ»ΡΠΊΠΎ
|
|
473
|
/// ΠΎΠ·Π½Π°ΡΠ°Π΅Ρ, ΡΡΠΎ ΠΌΡ Π΅Π³ΠΎ Π½Π΅ Π΄ΠΎΠΆΠ΄Π°Π»ΠΈΡΡ, ΠΎΠ΄Π½Π°ΠΊΠΎ Π²ΡΠ΅ Π·Π°ΡΠ΅Π³ΠΈΡΡΡΠΈΡΠΎΠ²Π°Π½Π½ΡΠ΅
|
|
483
|
/// ΠΎΠ·Π½Π°ΡΠ°Π΅Ρ, ΡΡΠΎ ΠΌΡ Π΅Π³ΠΎ Π½Π΅ Π΄ΠΎΠΆΠ΄Π°Π»ΠΈΡΡ, ΠΎΠ΄Π½Π°ΠΊΠΎ Π²ΡΠ΅ Π·Π°ΡΠ΅Π³ΠΈΡΡΡΠΈΡΠΎΠ²Π°Π½Π½ΡΠ΅
|
|
474
|
/// ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ, ΠΊΠ°ΠΊ Π±ΡΠ»ΠΈ ΡΠ°ΠΊ ΠΎΡΡΠ°Π»ΠΈΡΡ ΠΈ ΠΎΠ½ΠΈ Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ, ΠΊΠΎΠ³Π΄Π°
|
|
484
|
/// ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ, ΠΊΠ°ΠΊ Π±ΡΠ»ΠΈ ΡΠ°ΠΊ ΠΎΡΡΠ°Π»ΠΈΡΡ ΠΈ ΠΎΠ½ΠΈ Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ, ΠΊΠΎΠ³Π΄Π°
|
|
475
|
/// ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ.
|
|
485
|
/// ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ.
|
|
476
|
/// </para>
|
|
486
|
/// </para>
|
|
477
|
/// <para>
|
|
487
|
/// <para>
|
|
478
|
/// Π’Π°ΠΊΠΎΠ΅ ΠΏΠΎΠ²Π΅Π΄Π΅Π½ΠΈΠ΅ Π²ΠΏΠΎΠ»Π½Π΅ ΠΎΠΏΡΠ°Π²Π΄Π°Π½ΠΎ ΠΏΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΡΠ°ΠΉΠΌΠ°ΡΡ ΠΌΠΎΠΆΠ΅Ρ ΠΈΡΡΠ΅ΡΡ
|
|
488
|
/// Π’Π°ΠΊΠΎΠ΅ ΠΏΠΎΠ²Π΅Π΄Π΅Π½ΠΈΠ΅ Π²ΠΏΠΎΠ»Π½Π΅ ΠΎΠΏΡΠ°Π²Π΄Π°Π½ΠΎ ΠΏΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΡΠ°ΠΉΠΌΠ°ΡΡ ΠΌΠΎΠΆΠ΅Ρ ΠΈΡΡΠ΅ΡΡ
|
|
479
|
/// Π² ΡΠΎΡ ΠΌΠΎΠΌΠ΅Π½Ρ, ΠΊΠΎΠ³Π΄Π° Π½Π°ΡΠ°Π»Π°ΡΡ ΠΎΠ±ΡΠ°Π±ΠΎΡΠΊΠ° ΡΠ΅ΠΏΠΎΡΠΊΠΈ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΎΠ², ΠΈ
|
|
489
|
/// Π² ΡΠΎΡ ΠΌΠΎΠΌΠ΅Π½Ρ, ΠΊΠΎΠ³Π΄Π° Π½Π°ΡΠ°Π»Π°ΡΡ ΠΎΠ±ΡΠ°Π±ΠΎΡΠΊΠ° ΡΠ΅ΠΏΠΎΡΠΊΠΈ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΎΠ², ΠΈ
|
|
480
|
/// ΠΊ ΡΠΎΠΌΡ ΠΆΠ΅ ΡΠ΅ΠΊΡΡΠ΅Π΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ ΡΡΠΎΡΡΡ Π² ΡΠ΅ΠΏΠΎΡΠΊΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ ΠΈ Π΅Π³ΠΎ
|
|
490
|
/// ΠΊ ΡΠΎΠΌΡ ΠΆΠ΅ ΡΠ΅ΠΊΡΡΠ΅Π΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ ΡΡΠΎΡΡΡ Π² ΡΠ΅ΠΏΠΎΡΠΊΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ ΠΈ Π΅Π³ΠΎ
|
|
481
|
/// ΠΎΡΠΊΠ»ΠΎΠ½Π΅Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ ΠΏΡΠΈΠ²Π΅ΡΡΠΈ ΠΊ Π½Π΅ΠΏΡΠΎΠ³Π½ΠΎΠ·ΠΈΡΡΠ΅ΠΌΠΎΠΌΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ.
|
|
491
|
/// ΠΎΡΠΊΠ»ΠΎΠ½Π΅Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ ΠΏΡΠΈΠ²Π΅ΡΡΠΈ ΠΊ Π½Π΅ΠΏΡΠΎΠ³Π½ΠΎΠ·ΠΈΡΡΠ΅ΠΌΠΎΠΌΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ.
|
|
482
|
/// </para>
|
|
492
|
/// </para>
|
|
483
|
/// </remarks>
|
|
493
|
/// </remarks>
|
|
484
|
/// <param name="timeout">ΠΡΠ΅ΠΌΡ ΠΎΠΆΠΈΠ΄Π°Π½ΠΈΡ</param>
|
|
494
|
/// <param name="timeout">ΠΡΠ΅ΠΌΡ ΠΎΠΆΠΈΠ΄Π°Π½ΠΈΡ</param>
|
|
485
|
/// <returns>Π Π΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ</returns>
|
|
495
|
/// <returns>Π Π΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ</returns>
|
|
486
|
public T Join(int timeout) {
|
|
496
|
public T Join(int timeout) {
|
|
487
|
var evt = new ManualResetEvent(false);
|
|
497
|
var evt = new ManualResetEvent(false);
|
|
488
|
Anyway(() => evt.Set());
|
|
498
|
Anyway(() => evt.Set());
|
|
489
|
Cancelled(() => evt.Set());
|
|
499
|
Cancelled(() => evt.Set());
|
|
490
|
|
|
500
|
|
|
491
|
if (!evt.WaitOne(timeout, true))
|
|
501
|
if (!evt.WaitOne(timeout, true))
|
|
492
|
throw new TimeoutException();
|
|
502
|
throw new TimeoutException();
|
|
493
|
|
|
503
|
|
|
494
|
switch (m_state) {
|
|
504
|
switch (m_state) {
|
|
495
|
case SucceededState:
|
|
505
|
case SucceededState:
|
|
496
|
return m_result;
|
|
506
|
return m_result;
|
|
497
|
case CancelledState:
|
|
507
|
case CancelledState:
|
|
498
|
throw new OperationCanceledException();
|
|
508
|
throw new OperationCanceledException();
|
|
499
|
case RejectedState:
|
|
509
|
case RejectedState:
|
|
500
|
throw new TargetInvocationException(m_error);
|
|
510
|
throw new TargetInvocationException(m_error);
|
|
501
|
default:
|
|
511
|
default:
|
|
502
|
throw new ApplicationException(String.Format("Invalid promise state {0}", m_state));
|
|
512
|
throw new ApplicationException(String.Format("Invalid promise state {0}", m_state));
|
|
503
|
}
|
|
513
|
}
|
|
504
|
}
|
|
514
|
}
|
|
505
|
|
|
515
|
|
|
506
|
public T Join() {
|
|
516
|
public T Join() {
|
|
507
|
return Join(Timeout.Infinite);
|
|
517
|
return Join(Timeout.Infinite);
|
|
508
|
}
|
|
518
|
}
|
|
509
|
|
|
519
|
|
|
510
|
void AddHandler(ResultHandler<T> success, ErrorHandler error, Action cancel) {
|
|
520
|
void AddHandler(ResultHandler<T> success, ErrorHandler error, Action cancel) {
|
|
511
|
Interlocked.Increment(ref m_childrenCount);
|
|
521
|
Interlocked.Increment(ref m_childrenCount);
|
|
512
|
|
|
522
|
|
|
513
|
HandlerDescriptor handler = new HandlerDescriptor {
|
|
523
|
HandlerDescriptor handler = new HandlerDescriptor {
|
|
514
|
resultHandler = success,
|
|
524
|
resultHandler = success,
|
|
515
|
errorHandler = error,
|
|
525
|
errorHandler = error,
|
|
516
|
cancellHandler = cancel
|
|
526
|
cancellHandler = cancel
|
|
517
|
};
|
|
527
|
};
|
|
518
|
|
|
528
|
|
|
519
|
bool queued;
|
|
529
|
bool queued;
|
|
520
|
|
|
530
|
|
|
521
|
if (!IsResolved) {
|
|
531
|
if (!IsResolved) {
|
|
522
|
m_handlers.Enqueue(handler);
|
|
532
|
m_handlers.Enqueue(handler);
|
|
523
|
queued = true;
|
|
533
|
queued = true;
|
|
524
|
} else {
|
|
534
|
} else {
|
|
525
|
// the promise is in resolved state, just invoke the handled with minimum overhead
|
|
535
|
// the promise is in resolved state, just invoke the handled with minimum overhead
|
|
526
|
queued = false;
|
|
536
|
queued = false;
|
|
527
|
InvokeHandler(handler);
|
|
537
|
InvokeHandler(handler);
|
|
528
|
}
|
|
538
|
}
|
|
529
|
|
|
539
|
|
|
530
|
if (queued && IsResolved && m_handlers.TryDequeue(out handler))
|
|
540
|
if (queued && IsResolved && m_handlers.TryDequeue(out handler))
|
|
531
|
// if the promise have been resolved while we was adding handler to the queue
|
|
541
|
// if the promise have been resolved while we was adding handler to the queue
|
|
532
|
// we can't guarantee that someone is still processing it
|
|
542
|
// we can't guarantee that someone is still processing it
|
|
533
|
// therefore we will fetch a handler from the queue and execute it
|
|
543
|
// therefore we will fetch a handler from the queue and execute it
|
|
534
|
// note that fetched handler may be not the one we have added
|
|
544
|
// note that fetched handler may be not the one we have added
|
|
535
|
InvokeHandler(handler);
|
|
545
|
InvokeHandler(handler);
|
|
536
|
|
|
546
|
|
|
537
|
}
|
|
547
|
}
|
|
538
|
|
|
548
|
|
|
539
|
void InvokeHandler(HandlerDescriptor handler) {
|
|
549
|
void InvokeHandler(HandlerDescriptor handler) {
|
|
540
|
switch (m_state) {
|
|
550
|
switch (m_state) {
|
|
541
|
case SucceededState:
|
|
551
|
case SucceededState:
|
|
542
|
handler.Resolve(m_result);
|
|
552
|
handler.Resolve(m_result);
|
|
543
|
break;
|
|
553
|
break;
|
|
544
|
case RejectedState:
|
|
554
|
case RejectedState:
|
|
545
|
handler.Reject(m_error);
|
|
555
|
handler.Reject(m_error);
|
|
546
|
break;
|
|
556
|
break;
|
|
547
|
case CancelledState:
|
|
557
|
case CancelledState:
|
|
548
|
handler.Cancel();
|
|
558
|
handler.Cancel();
|
|
549
|
break;
|
|
559
|
break;
|
|
550
|
default:
|
|
560
|
default:
|
|
551
|
// do nothing
|
|
561
|
// do nothing
|
|
552
|
return;
|
|
562
|
return;
|
|
553
|
}
|
|
563
|
}
|
|
554
|
}
|
|
564
|
}
|
|
555
|
|
|
565
|
|
|
556
|
protected virtual void OnStateChanged() {
|
|
566
|
protected virtual void OnStateChanged() {
|
|
557
|
HandlerDescriptor handler;
|
|
567
|
HandlerDescriptor handler;
|
|
558
|
while (m_handlers.TryDequeue(out handler))
|
|
568
|
while (m_handlers.TryDequeue(out handler))
|
|
559
|
InvokeHandler(handler);
|
|
569
|
InvokeHandler(handler);
|
|
560
|
}
|
|
570
|
}
|
|
561
|
|
|
571
|
|
|
562
|
public bool IsExclusive {
|
|
572
|
public bool IsExclusive {
|
|
563
|
get {
|
|
573
|
get {
|
|
564
|
return m_childrenCount <= 1;
|
|
574
|
return m_childrenCount <= 1;
|
|
565
|
}
|
|
575
|
}
|
|
566
|
}
|
|
576
|
}
|
|
567
|
|
|
577
|
|
|
568
|
protected bool Cancel(bool dependencies) {
|
|
578
|
protected bool Cancel(bool dependencies) {
|
|
569
|
if (BeginTransit()) {
|
|
579
|
if (BeginTransit()) {
|
|
570
|
CompleteTransit(CancelledState);
|
|
580
|
CompleteTransit(CancelledState);
|
|
571
|
OnStateChanged();
|
|
581
|
OnStateChanged();
|
|
572
|
|
|
582
|
|
|
573
|
if (dependencies && m_parent != null && m_parent.IsExclusive)
|
|
583
|
if (dependencies && m_parent != null && m_parent.IsExclusive)
|
|
574
|
m_parent.Cancel();
|
|
584
|
m_parent.Cancel();
|
|
575
|
|
|
585
|
|
|
576
|
return true;
|
|
586
|
return true;
|
|
577
|
} else {
|
|
587
|
} else {
|
|
578
|
return false;
|
|
588
|
return false;
|
|
579
|
}
|
|
589
|
}
|
|
580
|
}
|
|
590
|
}
|
|
581
|
|
|
591
|
|
|
582
|
/// <summary>
|
|
592
|
/// <summary>
|
|
583
|
/// ΠΠ±ΡΠ΅Π΄ΠΈΠ½ΡΠ΅Ρ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ Π² ΠΎΠ΄Π½ΠΎ, ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠΌ ΠΊΠΎΡΠΎΡΠΎΠ³ΠΎ ΡΠ²Π»ΡΠ΅ΡΡΡ ΠΌΠ°ΡΡΠΈΠ² ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ² Π΄ΡΡΠ³ΠΈΡ
ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ.
|
|
593
|
/// ΠΠ±ΡΠ΅Π΄ΠΈΠ½ΡΠ΅Ρ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ Π² ΠΎΠ΄Π½ΠΎ, ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠΌ ΠΊΠΎΡΠΎΡΠΎΠ³ΠΎ ΡΠ²Π»ΡΠ΅ΡΡΡ ΠΌΠ°ΡΡΠΈΠ² ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ² Π΄ΡΡΠ³ΠΈΡ
ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ.
|
|
584
|
/// ΠΡΠ»ΠΈ Ρ
ΠΎΡΡΠ±Ρ ΠΎΠ΄Π½ΠΎ ΠΈΠ· ΠΏΠ΅ΡΠ΅Π΄Π°Π½Π½ΡΡ
ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ Π½Π΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ, ΡΠΎ Π½ΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ΅Π½ΠΈΠ΅ ΡΠΎΠΆΠ΅ Π½Π΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ.
|
|
594
|
/// ΠΡΠ»ΠΈ Ρ
ΠΎΡΡΠ±Ρ ΠΎΠ΄Π½ΠΎ ΠΈΠ· ΠΏΠ΅ΡΠ΅Π΄Π°Π½Π½ΡΡ
ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ Π½Π΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ, ΡΠΎ Π½ΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ΅Π½ΠΈΠ΅ ΡΠΎΠΆΠ΅ Π½Π΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ.
|
|
585
|
/// ΠΡΠΈ ΠΎΡΠΌΠ΅Π½Π΅ Π½ΠΎΠ²ΠΎΠ³ΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ, ΠΏΠ΅ΡΠ΅Π΄Π°Π½Π½ΡΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΡΠ°ΠΊΠΆΠ΅ Π±ΡΠ΄ΡΡ ΠΎΡΠΌΠ΅Π½Π΅Π½Ρ, Π΅ΡΠ»ΠΈ Π½ΠΈΠΊΡΠΎ Π±ΠΎΠ»ΡΡΠ΅ Π½Π° Π½ΠΈΡ
Π½Π΅ ΠΏΠΎΠ΄ΠΏΠΈΡΠ°Π½.
|
|
595
|
/// ΠΡΠΈ ΠΎΡΠΌΠ΅Π½Π΅ Π½ΠΎΠ²ΠΎΠ³ΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ, ΠΏΠ΅ΡΠ΅Π΄Π°Π½Π½ΡΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΡΠ°ΠΊΠΆΠ΅ Π±ΡΠ΄ΡΡ ΠΎΡΠΌΠ΅Π½Π΅Π½Ρ, Π΅ΡΠ»ΠΈ Π½ΠΈΠΊΡΠΎ Π±ΠΎΠ»ΡΡΠ΅ Π½Π° Π½ΠΈΡ
Π½Π΅ ΠΏΠΎΠ΄ΠΏΠΈΡΠ°Π½.
|
|
586
|
/// </summary>
|
|
596
|
/// </summary>
|
|
587
|
/// <param name="promises">Π‘ΠΏΠΈΡΠΎΠΊ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ. ΠΡΠ»ΠΈ ΡΠΏΠΈΡΠΎΠΊ ΠΏΡΡΡΠΎΠΉ, ΡΠΎ ΡΠ΅Π·ΡΠ»ΡΡΠΈΡΡΡΡΠ΅Π΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅ΡΡΡ ΡΠΆΠ΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½Π½ΡΠΌ.</param>
|
|
597
|
/// <param name="promises">Π‘ΠΏΠΈΡΠΎΠΊ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ. ΠΡΠ»ΠΈ ΡΠΏΠΈΡΠΎΠΊ ΠΏΡΡΡΠΎΠΉ, ΡΠΎ ΡΠ΅Π·ΡΠ»ΡΡΠΈΡΡΡΡΠ΅Π΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅ΡΡΡ ΡΠΆΠ΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½Π½ΡΠΌ.</param>
|
|
588
|
/// <returns>ΠΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΠΎΠ±ΡΠ΅Π΄ΠΈΠ½ΡΡΡΠ΅Π΅ Π² ΡΠ΅Π±Π΅ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ ΠΏΠ΅ΡΠ΅Π΄Π°Π½Π½ΡΡ
ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ.</returns>
|
|
598
|
/// <returns>ΠΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΠΎΠ±ΡΠ΅Π΄ΠΈΠ½ΡΡΡΠ΅Π΅ Π² ΡΠ΅Π±Π΅ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ ΠΏΠ΅ΡΠ΅Π΄Π°Π½Π½ΡΡ
ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ.</returns>
|
|
589
|
/// <exception cref="ArgumentNullException"><paramref name="promises"/> Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ null</exception>
|
|
599
|
/// <exception cref="ArgumentNullException"><paramref name="promises"/> Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ null</exception>
|
|
590
|
public static Promise<T[]> CreateComposite(IList<Promise<T>> promises) {
|
|
600
|
public static Promise<T[]> CreateComposite(IList<Promise<T>> promises) {
|
|
591
|
if (promises == null)
|
|
601
|
if (promises == null)
|
|
592
|
throw new ArgumentNullException();
|
|
602
|
throw new ArgumentNullException();
|
|
593
|
|
|
603
|
|
|
594
|
// ΡΠΎΠ·Π΄Π°Π΅ΠΌ Π°ΠΊΠΊΡΠΌΡΠ»ΡΡΠΎΡ Π΄Π»Ρ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ² ΠΈ ΡΠ΅Π·ΡΠ»ΡΡΠΈΡΡΡΡΠ΅Π΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅
|
|
604
|
// ΡΠΎΠ·Π΄Π°Π΅ΠΌ Π°ΠΊΠΊΡΠΌΡΠ»ΡΡΠΎΡ Π΄Π»Ρ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ² ΠΈ ΡΠ΅Π·ΡΠ»ΡΡΠΈΡΡΡΡΠ΅Π΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅
|
|
595
|
var result = new T[promises.Count];
|
|
605
|
var result = new T[promises.Count];
|
|
596
|
var promise = new Promise<T[]>();
|
|
606
|
var promise = new Promise<T[]>();
|
|
597
|
|
|
607
|
|
|
598
|
// special case
|
|
608
|
// special case
|
|
599
|
if (promises.Count == 0) {
|
|
609
|
if (promises.Count == 0) {
|
|
600
|
promise.Resolve(result);
|
|
610
|
promise.Resolve(result);
|
|
601
|
return promise;
|
|
611
|
return promise;
|
|
602
|
}
|
|
612
|
}
|
|
603
|
|
|
613
|
|
|
604
|
int pending = promises.Count;
|
|
614
|
int pending = promises.Count;
|
|
605
|
|
|
615
|
|
|
606
|
for (int i = 0; i < promises.Count; i++) {
|
|
616
|
for (int i = 0; i < promises.Count; i++) {
|
|
607
|
var dest = i;
|
|
617
|
var dest = i;
|
|
608
|
|
|
618
|
|
|
609
|
promises[i].Then(
|
|
619
|
promises[i].Then(
|
|
610
|
x => {
|
|
620
|
x => {
|
|
611
|
result[dest] = x;
|
|
621
|
result[dest] = x;
|
|
612
|
if(Interlocked.Decrement(ref pending) == 0)
|
|
622
|
if(Interlocked.Decrement(ref pending) == 0)
|
|
613
|
promise.Resolve(result);
|
|
623
|
promise.Resolve(result);
|
|
614
|
},
|
|
624
|
},
|
|
615
|
e => promise.Reject(e)
|
|
625
|
e => promise.Reject(e)
|
|
616
|
);
|
|
626
|
);
|
|
617
|
}
|
|
627
|
}
|
|
618
|
|
|
628
|
|
|
619
|
promise.Cancelled(
|
|
629
|
promise.Cancelled(
|
|
620
|
() => {
|
|
630
|
() => {
|
|
621
|
foreach(var d in promises)
|
|
631
|
foreach(var d in promises)
|
|
622
|
if(d.IsExclusive)
|
|
632
|
if(d.IsExclusive)
|
|
623
|
d.Cancel();
|
|
633
|
d.Cancel();
|
|
624
|
}
|
|
634
|
}
|
|
625
|
);
|
|
635
|
);
|
|
626
|
|
|
636
|
|
|
627
|
return promise;
|
|
637
|
return promise;
|
|
628
|
}
|
|
638
|
}
|
|
629
|
|
|
639
|
|
|
630
|
public static Promise<T> ResultToPromise(T result) {
|
|
640
|
public static Promise<T> ResultToPromise(T result) {
|
|
631
|
var p = new Promise<T>();
|
|
641
|
var p = new Promise<T>();
|
|
632
|
p.Resolve(result);
|
|
642
|
p.Resolve(result);
|
|
633
|
return p;
|
|
643
|
return p;
|
|
634
|
}
|
|
644
|
}
|
|
635
|
|
|
645
|
|
|
636
|
public static Promise<T> ExceptionToPromise(Exception error) {
|
|
646
|
public static Promise<T> ExceptionToPromise(Exception error) {
|
|
637
|
if (error == null)
|
|
647
|
if (error == null)
|
|
638
|
throw new ArgumentNullException();
|
|
648
|
throw new ArgumentNullException();
|
|
639
|
|
|
649
|
|
|
640
|
var p = new Promise<T>();
|
|
650
|
var p = new Promise<T>();
|
|
641
|
p.Reject(error);
|
|
651
|
p.Reject(error);
|
|
642
|
return p;
|
|
652
|
return p;
|
|
643
|
}
|
|
653
|
}
|
|
644
|
|
|
654
|
|
|
645
|
}
|
|
655
|
}
|
|
646
|
}
|
|
656
|
}
|