|
@@
-1,822
+1,914
|
|
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
|
|
|
14
|
|
|
15
|
/// <summary>
|
|
15
|
/// <summary>
|
|
16
|
/// ΠΠ»Π°ΡΡ Π΄Π»Ρ Π°ΡΠΈΠ½Ρ
ΡΠΎΠ½Π½ΠΎΠ³ΠΎ ΠΏΠΎΠ»ΡΡΠ΅Π½ΠΈΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ². Π’Π°ΠΊ Π½Π°Π·ΡΠ²Π°Π΅ΠΌΠΎΠ΅ "ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅".
|
|
16
|
/// ΠΠ»Π°ΡΡ Π΄Π»Ρ Π°ΡΠΈΠ½Ρ
ΡΠΎΠ½Π½ΠΎΠ³ΠΎ ΠΏΠΎΠ»ΡΡΠ΅Π½ΠΈΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ². Π’Π°ΠΊ Π½Π°Π·ΡΠ²Π°Π΅ΠΌΠΎΠ΅ "ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅".
|
|
17
|
/// </summary>
|
|
17
|
/// </summary>
|
|
18
|
/// <typeparam name="T">Π’ΠΈΠΏ ΠΏΠΎΠ»ΡΡΠ°Π΅ΠΌΠΎΠ³ΠΎ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ°</typeparam>
|
|
18
|
/// <typeparam name="T">Π’ΠΈΠΏ ΠΏΠΎΠ»ΡΡΠ°Π΅ΠΌΠΎΠ³ΠΎ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ°</typeparam>
|
|
19
|
/// <remarks>
|
|
19
|
/// <remarks>
|
|
20
|
/// <para>Π‘Π΅ΡΠ²ΠΈΡ ΠΏΡΠΈ ΠΎΠ±ΡΠ°ΡΠ΅Π½ΠΈΠΈ ΠΊ Π΅Π³ΠΎ ΠΌΠ΅ΡΠΎΠ΄Ρ Π΄Π°Π΅Ρ ΠΎΠ±Π΅ΡΠ°ΠΈΠ½ΠΈΠ΅ ΠΎ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ,
|
|
20
|
/// <para>Π‘Π΅ΡΠ²ΠΈΡ ΠΏΡΠΈ ΠΎΠ±ΡΠ°ΡΠ΅Π½ΠΈΠΈ ΠΊ Π΅Π³ΠΎ ΠΌΠ΅ΡΠΎΠ΄Ρ Π΄Π°Π΅Ρ ΠΎΠ±Π΅ΡΠ°ΠΈΠ½ΠΈΠ΅ ΠΎ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ,
|
|
21
|
/// ΠΊΠ»ΠΈΠ΅Π½Ρ ΠΏΠΎΠ»ΡΡΠΈΠ² ΡΠ°ΠΊΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ ΡΡΡΠ°Π½ΠΎΠ²ΠΈΡΡ ΡΡΠ΄ ΠΎΠ±ΡΠ°ΡΠ½ΡΡ
Π²ΡΠ·ΠΎΠ²ΠΎ Π΄Π»Ρ ΠΏΠΎΠ»ΡΡΠ΅Π½ΠΈΡ
|
|
21
|
/// ΠΊΠ»ΠΈΠ΅Π½Ρ ΠΏΠΎΠ»ΡΡΠΈΠ² ΡΠ°ΠΊΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ ΡΡΡΠ°Π½ΠΎΠ²ΠΈΡΡ ΡΡΠ΄ ΠΎΠ±ΡΠ°ΡΠ½ΡΡ
Π²ΡΠ·ΠΎΠ²ΠΎ Π΄Π»Ρ ΠΏΠΎΠ»ΡΡΠ΅Π½ΠΈΡ
|
|
22
|
/// ΡΠΎΠ±ΡΡΠΈΠΉ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ, ΡΠΎΠ΅ΡΡΡ Π·Π°Π²Π΅ΡΡΠ΅Π½ΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΠΈ ΠΏΡΠ΅Π΄ΠΎΡΡΠ°Π²Π»Π΅Π½ΠΈΠΈ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ².</para>
|
|
22
|
/// ΡΠΎΠ±ΡΡΠΈΠΉ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ, ΡΠΎΠ΅ΡΡΡ Π·Π°Π²Π΅ΡΡΠ΅Π½ΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΠΈ ΠΏΡΠ΅Π΄ΠΎΡΡΠ°Π²Π»Π΅Π½ΠΈΠΈ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ².</para>
|
|
23
|
/// <para>
|
|
23
|
/// <para>
|
|
24
|
/// ΠΠ±Π΅ΡΠ΅Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ ΠΊΠ°ΠΊ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ, ΡΠ°ΠΊ ΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ Ρ ΠΎΡΠΈΠ±ΠΊΠΎΠΉ. ΠΠ»Ρ ΠΏΠΎΠ΄ΠΏΠΈΡΠΊΠΈ Π½Π°
|
|
24
|
/// ΠΠ±Π΅ΡΠ΅Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ ΠΊΠ°ΠΊ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ, ΡΠ°ΠΊ ΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ Ρ ΠΎΡΠΈΠ±ΠΊΠΎΠΉ. ΠΠ»Ρ ΠΏΠΎΠ΄ΠΏΠΈΡΠΊΠΈ Π½Π°
|
|
25
|
/// Π΄Π°Π½Π½ΡΠ΅ ΡΠΎΠ±ΡΡΠΈΡ ΠΊΠ»ΠΈΠ΅Π½Ρ Π΄ΠΎΠ»ΠΆΠ΅Π½ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ ΠΌΠ΅ΡΠΎΠ΄Ρ <c>Then</c>.
|
|
25
|
/// Π΄Π°Π½Π½ΡΠ΅ ΡΠΎΠ±ΡΡΠΈΡ ΠΊΠ»ΠΈΠ΅Π½Ρ Π΄ΠΎΠ»ΠΆΠ΅Π½ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ ΠΌΠ΅ΡΠΎΠ΄Ρ <c>Then</c>.
|
|
26
|
/// </para>
|
|
26
|
/// </para>
|
|
27
|
/// <para>
|
|
27
|
/// <para>
|
|
28
|
/// Π‘Π΅ΡΠ²ΠΈΡ, Π² ΡΠ²ΠΎΡ ΠΎΡΠ΅ΡΠ΅Π΄Ρ, ΠΏΠΎ ΠΎΠΊΠΎΠ½ΡΠ°Π½ΠΈΡ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ (Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ Ρ ΠΎΡΠΈΠ±ΠΊΠΎΠΉ),
|
|
28
|
/// Π‘Π΅ΡΠ²ΠΈΡ, Π² ΡΠ²ΠΎΡ ΠΎΡΠ΅ΡΠ΅Π΄Ρ, ΠΏΠΎ ΠΎΠΊΠΎΠ½ΡΠ°Π½ΠΈΡ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ (Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ Ρ ΠΎΡΠΈΠ±ΠΊΠΎΠΉ),
|
|
29
|
/// ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅Ρ ΠΌΠ΅ΡΠΎΠ΄Ρ <c>Resolve</c> Π»ΠΈΠ±ΠΎ <c>Reject</c> Π΄Π»Ρ ΠΎΠΏΠΎΠ²Π΅ΡΠ΅Π½ΠΈΡ ΠΊΠ»ΠΈΠ΅ΡΠ½Π° ΠΎ
|
|
29
|
/// ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅Ρ ΠΌΠ΅ΡΠΎΠ΄Ρ <c>Resolve</c> Π»ΠΈΠ±ΠΎ <c>Reject</c> Π΄Π»Ρ ΠΎΠΏΠΎΠ²Π΅ΡΠ΅Π½ΠΈΡ ΠΊΠ»ΠΈΠ΅ΡΠ½Π° ΠΎ
|
|
30
|
/// Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ.
|
|
30
|
/// Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ.
|
|
31
|
/// </para>
|
|
31
|
/// </para>
|
|
32
|
/// <para>
|
|
32
|
/// <para>
|
|
33
|
/// ΠΡΠ»ΠΈ ΡΠ΅ΡΠ²Π΅Ρ ΡΡΠΏΠ΅Π» Π²ΡΠΏΠΎΠ»Π½ΠΈΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π΅ΡΠ΅ Π΄ΠΎ ΡΠΎΠ³ΠΎ, ΠΊΠ°ΠΊ ΠΊΠ»ΠΈΠ΅Π½Ρ Π½Π° Π½Π΅Π³ΠΎ ΠΏΠΎΠ΄ΠΏΠΈΡΠ°Π»ΡΡ,
|
|
33
|
/// ΠΡΠ»ΠΈ ΡΠ΅ΡΠ²Π΅Ρ ΡΡΠΏΠ΅Π» Π²ΡΠΏΠΎΠ»Π½ΠΈΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π΅ΡΠ΅ Π΄ΠΎ ΡΠΎΠ³ΠΎ, ΠΊΠ°ΠΊ ΠΊΠ»ΠΈΠ΅Π½Ρ Π½Π° Π½Π΅Π³ΠΎ ΠΏΠΎΠ΄ΠΏΠΈΡΠ°Π»ΡΡ,
|
|
34
|
/// ΡΠΎ Π² ΠΌΠΎΠΌΠ΅Π½Ρ ΠΏΠΎΠ΄ΠΏΠΈΡΠΊΠΈ ΠΊΠ»ΠΈΠ΅Π½ΡΠ° Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ ΡΠΎΠΎΡΠ²Π΅ΡΡΠ²ΡΡΡΠΈΠ΅ ΡΠΎΠ±ΡΡΠΈΡ Π² ΡΠΈΠ½Ρ
ΡΠΎΠ½Π½ΠΎΠΌ
|
|
34
|
/// ΡΠΎ Π² ΠΌΠΎΠΌΠ΅Π½Ρ ΠΏΠΎΠ΄ΠΏΠΈΡΠΊΠΈ ΠΊΠ»ΠΈΠ΅Π½ΡΠ° Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ ΡΠΎΠΎΡΠ²Π΅ΡΡΠ²ΡΡΡΠΈΠ΅ ΡΠΎΠ±ΡΡΠΈΡ Π² ΡΠΈΠ½Ρ
ΡΠΎΠ½Π½ΠΎΠΌ
|
|
35
|
/// ΡΠ΅ΠΆΠΈΠΌΠ΅ ΠΈ ΠΊΠ»ΠΈΠ΅Π½Ρ Π±ΡΠ΄Π΅Ρ ΠΎΠΏΠΎΠ²Π΅ΡΠ΅Π½ Π² Π»ΡΠ±ΠΎΠΌ ΡΠ»ΡΡΠ°Π΅. ΠΠ½Π°ΡΠ΅, ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ Π΄ΠΎΠ±Π°Π²Π»ΡΡΡΡΡ Π²
|
|
35
|
/// ΡΠ΅ΠΆΠΈΠΌΠ΅ ΠΈ ΠΊΠ»ΠΈΠ΅Π½Ρ Π±ΡΠ΄Π΅Ρ ΠΎΠΏΠΎΠ²Π΅ΡΠ΅Π½ Π² Π»ΡΠ±ΠΎΠΌ ΡΠ»ΡΡΠ°Π΅. ΠΠ½Π°ΡΠ΅, ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ Π΄ΠΎΠ±Π°Π²Π»ΡΡΡΡΡ Π²
|
|
36
|
/// ΡΠΏΠΈΡΠΎΠΊ Π² ΠΏΠΎΡΡΠ΄ΠΊΠ΅ ΠΏΠΎΠ΄ΠΏΠΈΡΠ°Π½ΠΈΡ ΠΈ Π² ΡΡΠΎΠΌ ΠΆΠ΅ ΠΏΠΎΡΡΠ΄ΠΊΠ΅ ΠΎΠ½ΠΈ Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ
|
|
36
|
/// ΡΠΏΠΈΡΠΎΠΊ Π² ΠΏΠΎΡΡΠ΄ΠΊΠ΅ ΠΏΠΎΠ΄ΠΏΠΈΡΠ°Π½ΠΈΡ ΠΈ Π² ΡΡΠΎΠΌ ΠΆΠ΅ ΠΏΠΎΡΡΠ΄ΠΊΠ΅ ΠΎΠ½ΠΈ Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ
|
|
37
|
/// ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ.
|
|
37
|
/// ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ.
|
|
38
|
/// </para>
|
|
38
|
/// </para>
|
|
39
|
/// <para>
|
|
39
|
/// <para>
|
|
40
|
/// ΠΠ±ΡΠ°Π±Π°ΡΡΠ²Π°Ρ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΌΠΎΠΆΠ½ΠΎ ΠΏΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²ΡΠ²Π°ΡΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ Π»ΠΈΠ±ΠΎ ΠΈΠ½ΠΈΡΠΈΠΈΡΠΎΠ²Π°ΡΡ
|
|
40
|
/// ΠΠ±ΡΠ°Π±Π°ΡΡΠ²Π°Ρ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΌΠΎΠΆΠ½ΠΎ ΠΏΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²ΡΠ²Π°ΡΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ Π»ΠΈΠ±ΠΎ ΠΈΠ½ΠΈΡΠΈΠΈΡΠΎΠ²Π°ΡΡ
|
|
41
|
/// ΡΠ²ΡΠ·Π°Π½Π½ΡΠ΅ Π°ΡΠΈΠ½Ρ
ΡΠΎΠ½Π½ΡΠ΅ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ, ΠΊΠΎΡΠΎΡΡΠ΅ ΡΠ°ΠΊΠΆΠ΅ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°ΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ. ΠΠ»Ρ ΡΡΠΎΠ³ΠΎ ΡΠ»Π΅Π΄ΡΠ΅Ρ
|
|
41
|
/// ΡΠ²ΡΠ·Π°Π½Π½ΡΠ΅ Π°ΡΠΈΠ½Ρ
ΡΠΎΠ½Π½ΡΠ΅ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ, ΠΊΠΎΡΠΎΡΡΠ΅ ΡΠ°ΠΊΠΆΠ΅ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°ΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ. ΠΠ»Ρ ΡΡΠΎΠ³ΠΎ ΡΠ»Π΅Π΄ΡΠ΅Ρ
|
|
42
|
/// ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ ΡΠΎΠΎΡΠ²Π΅ΡΡΡΠ²ΡΡΡΡΡ ΡΠΎΡΠΌΡ ΠΌΠ΅ΡΠΎΠ΄Π΅ <c>Then</c>.
|
|
42
|
/// ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ ΡΠΎΠΎΡΠ²Π΅ΡΡΡΠ²ΡΡΡΡΡ ΡΠΎΡΠΌΡ ΠΌΠ΅ΡΠΎΠ΄Π΅ <c>Then</c>.
|
|
43
|
/// </para>
|
|
43
|
/// </para>
|
|
44
|
/// <para>
|
|
44
|
/// <para>
|
|
45
|
/// Π’Π°ΠΊΠΆΠ΅ Ρ
ΠΎΡΠΎΡΠΈΠΌ ΠΏΡΠ°Π²ΠΈΠ»ΠΎΠΌ ΡΠ²Π»ΡΠ΅ΡΡΡ ΡΠΎ, ΡΡΠΎ <c>Resolve</c> ΠΈ <c>Reject</c> Π΄ΠΎΠ»ΠΆΠ΅Π½ Π²ΡΠ·ΡΠ²Π°ΡΡ
|
|
45
|
/// Π’Π°ΠΊΠΆΠ΅ Ρ
ΠΎΡΠΎΡΠΈΠΌ ΠΏΡΠ°Π²ΠΈΠ»ΠΎΠΌ ΡΠ²Π»ΡΠ΅ΡΡΡ ΡΠΎ, ΡΡΠΎ <c>Resolve</c> ΠΈ <c>Reject</c> Π΄ΠΎΠ»ΠΆΠ΅Π½ Π²ΡΠ·ΡΠ²Π°ΡΡ
|
|
46
|
/// ΡΠΎΠ»ΡΠΊΠΎ ΠΈΠ½ΠΈΡΠΈΠ°ΡΠΎΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΈΠ½Π°ΡΠ΅ ΠΌΠΎΠ³ΡΡ Π²ΠΎΠ·Π½ΠΈΠΊΠ½ΡΡΡ ΠΏΡΠΎΡΠΈΠ²ΠΎΡΠ΅ΡΠΈΡ.
|
|
46
|
/// ΡΠΎΠ»ΡΠΊΠΎ ΠΈΠ½ΠΈΡΠΈΠ°ΡΠΎΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΈΠ½Π°ΡΠ΅ ΠΌΠΎΠ³ΡΡ Π²ΠΎΠ·Π½ΠΈΠΊΠ½ΡΡΡ ΠΏΡΠΎΡΠΈΠ²ΠΎΡΠ΅ΡΠΈΡ.
|
|
47
|
/// </para>
|
|
47
|
/// </para>
|
|
48
|
/// </remarks>
|
|
48
|
/// </remarks>
|
|
49
|
public class Promise<T> : IPromise<T> {
|
|
49
|
public class Promise<T> : IPromise<T> {
|
|
50
|
|
|
50
|
|
|
51
|
protected struct HandlerDescriptor {
|
|
51
|
protected struct HandlerDescriptor {
|
|
52
|
public ResultHandler<T> resultHandler;
|
|
52
|
public ResultHandler<T> resultHandler;
|
|
53
|
public ErrorHandler<T> errorHandler;
|
|
53
|
public ErrorHandler<T> errorHandler;
|
|
54
|
public Action cancellHandler;
|
|
54
|
public Action cancellHandler;
|
|
55
|
public Promise<T> medium;
|
|
55
|
public Promise<T> medium;
|
|
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
|
return;
|
|
63
|
return;
|
|
64
|
}
|
|
64
|
}
|
|
65
|
}
|
|
65
|
}
|
|
66
|
if (medium != null)
|
|
66
|
if (medium != null)
|
|
67
|
medium.Resolve(result);
|
|
67
|
medium.Resolve(result);
|
|
68
|
}
|
|
68
|
}
|
|
69
|
|
|
69
|
|
|
70
|
public void Reject(Exception err) {
|
|
70
|
public void Reject(Exception err) {
|
|
71
|
if (errorHandler != null) {
|
|
71
|
if (errorHandler != null) {
|
|
72
|
try {
|
|
72
|
try {
|
|
73
|
var res = errorHandler(err);
|
|
73
|
var res = errorHandler(err);
|
|
74
|
if (medium != null)
|
|
74
|
if (medium != null)
|
|
75
|
medium.Resolve(res);
|
|
75
|
medium.Resolve(res);
|
|
76
|
} catch (TransientPromiseException err2) {
|
|
76
|
} catch (TransientPromiseException err2) {
|
|
77
|
if (medium != null)
|
|
77
|
if (medium != null)
|
|
78
|
medium.Reject(err2.InnerException);
|
|
78
|
medium.Reject(err2.InnerException);
|
|
79
|
} catch (Exception err2) {
|
|
79
|
} catch (Exception err2) {
|
|
80
|
if (medium != null)
|
|
80
|
if (medium != null)
|
|
81
|
medium.Reject(err2);
|
|
81
|
medium.Reject(err2);
|
|
82
|
}
|
|
82
|
}
|
|
83
|
} else if (medium != null)
|
|
83
|
} else if (medium != null)
|
|
84
|
medium.Reject(err);
|
|
84
|
medium.Reject(err);
|
|
85
|
}
|
|
85
|
}
|
|
86
|
|
|
86
|
|
|
87
|
public void Cancel() {
|
|
87
|
public void Cancel() {
|
|
88
|
if (cancellHandler != null) {
|
|
88
|
if (cancellHandler != null) {
|
|
89
|
try {
|
|
89
|
try {
|
|
90
|
cancellHandler();
|
|
90
|
cancellHandler();
|
|
91
|
} catch (Exception err) {
|
|
91
|
} catch (Exception err) {
|
|
92
|
Reject(err);
|
|
92
|
Reject(err);
|
|
93
|
return;
|
|
93
|
return;
|
|
94
|
}
|
|
94
|
}
|
|
95
|
}
|
|
95
|
}
|
|
96
|
if (medium != null)
|
|
96
|
if (medium != null)
|
|
97
|
medium.Cancel();
|
|
97
|
medium.Cancel();
|
|
98
|
}
|
|
98
|
}
|
|
99
|
}
|
|
99
|
}
|
|
100
|
|
|
100
|
|
|
101
|
const int UNRESOLVED_SATE = 0;
|
|
101
|
const int UNRESOLVED_SATE = 0;
|
|
102
|
const int TRANSITIONAL_STATE = 1;
|
|
102
|
const int TRANSITIONAL_STATE = 1;
|
|
103
|
const int SUCCEEDED_STATE = 2;
|
|
103
|
const int SUCCEEDED_STATE = 2;
|
|
104
|
const int REJECTED_STATE = 3;
|
|
104
|
const int REJECTED_STATE = 3;
|
|
105
|
const int CANCELLED_STATE = 4;
|
|
105
|
const int CANCELLED_STATE = 4;
|
|
106
|
|
|
106
|
|
|
107
|
readonly bool m_cancellable;
|
|
107
|
readonly bool m_cancellable;
|
|
108
|
|
|
108
|
|
|
109
|
int m_childrenCount = 0;
|
|
109
|
int m_childrenCount = 0;
|
|
110
|
int m_state;
|
|
110
|
int m_state;
|
|
111
|
T m_result;
|
|
111
|
T m_result;
|
|
112
|
Exception m_error;
|
|
112
|
Exception m_error;
|
|
113
|
|
|
113
|
|
|
114
|
readonly MTQueue<HandlerDescriptor> m_handlers = new MTQueue<HandlerDescriptor>();
|
|
114
|
readonly MTQueue<HandlerDescriptor> m_handlers = new MTQueue<HandlerDescriptor>();
|
|
115
|
|
|
115
|
|
|
116
|
public Promise() {
|
|
116
|
public Promise() {
|
|
117
|
m_cancellable = true;
|
|
117
|
m_cancellable = true;
|
|
118
|
}
|
|
118
|
}
|
|
119
|
|
|
119
|
|
|
120
|
public Promise(IPromise parent, bool cancellable) {
|
|
120
|
public Promise(IPromise parent, bool cancellable) {
|
|
121
|
m_cancellable = cancellable;
|
|
121
|
m_cancellable = cancellable;
|
|
122
|
if (parent != null)
|
|
122
|
if (parent != null)
|
|
123
|
AddHandler(
|
|
123
|
AddHandler(
|
|
124
|
null,
|
|
124
|
null,
|
|
125
|
null,
|
|
125
|
null,
|
|
126
|
() => {
|
|
126
|
() => {
|
|
127
|
if (parent.IsExclusive)
|
|
127
|
if (parent.IsExclusive)
|
|
128
|
parent.Cancel();
|
|
128
|
parent.Cancel();
|
|
129
|
},
|
|
129
|
},
|
|
130
|
null
|
|
130
|
null
|
|
131
|
);
|
|
131
|
);
|
|
132
|
}
|
|
132
|
}
|
|
133
|
|
|
133
|
|
|
134
|
bool BeginTransit() {
|
|
134
|
bool BeginTransit() {
|
|
135
|
return UNRESOLVED_SATE == Interlocked.CompareExchange(ref m_state, TRANSITIONAL_STATE, UNRESOLVED_SATE);
|
|
135
|
return UNRESOLVED_SATE == Interlocked.CompareExchange(ref m_state, TRANSITIONAL_STATE, UNRESOLVED_SATE);
|
|
136
|
}
|
|
136
|
}
|
|
137
|
|
|
137
|
|
|
138
|
void CompleteTransit(int state) {
|
|
138
|
void CompleteTransit(int state) {
|
|
139
|
if (TRANSITIONAL_STATE != Interlocked.CompareExchange(ref m_state, state, TRANSITIONAL_STATE))
|
|
139
|
if (TRANSITIONAL_STATE != Interlocked.CompareExchange(ref m_state, state, TRANSITIONAL_STATE))
|
|
140
|
throw new InvalidOperationException("Can't complete transition when the object isn't in the transitional state");
|
|
140
|
throw new InvalidOperationException("Can't complete transition when the object isn't in the transitional state");
|
|
141
|
}
|
|
141
|
}
|
|
142
|
|
|
142
|
|
|
143
|
void WaitTransition() {
|
|
143
|
void WaitTransition() {
|
|
144
|
while (m_state == TRANSITIONAL_STATE) {
|
|
144
|
while (m_state == TRANSITIONAL_STATE) {
|
|
145
|
Thread.MemoryBarrier();
|
|
145
|
Thread.MemoryBarrier();
|
|
146
|
}
|
|
146
|
}
|
|
147
|
}
|
|
147
|
}
|
|
148
|
|
|
148
|
|
|
149
|
public bool IsResolved {
|
|
149
|
public bool IsResolved {
|
|
150
|
get {
|
|
150
|
get {
|
|
151
|
Thread.MemoryBarrier();
|
|
151
|
Thread.MemoryBarrier();
|
|
152
|
return m_state > 1;
|
|
152
|
return m_state > 1;
|
|
153
|
}
|
|
153
|
}
|
|
154
|
}
|
|
154
|
}
|
|
155
|
|
|
155
|
|
|
156
|
public bool IsCancelled {
|
|
156
|
public bool IsCancelled {
|
|
157
|
get {
|
|
157
|
get {
|
|
158
|
Thread.MemoryBarrier();
|
|
158
|
Thread.MemoryBarrier();
|
|
159
|
return m_state == CANCELLED_STATE;
|
|
159
|
return m_state == CANCELLED_STATE;
|
|
160
|
}
|
|
160
|
}
|
|
161
|
}
|
|
161
|
}
|
|
162
|
|
|
162
|
|
|
163
|
public Type PromiseType {
|
|
163
|
public Type PromiseType {
|
|
164
|
get { return typeof(T); }
|
|
164
|
get { return typeof(T); }
|
|
165
|
}
|
|
165
|
}
|
|
166
|
|
|
166
|
|
|
167
|
/// <summary>
|
|
167
|
/// <summary>
|
|
168
|
/// ΠΡΠΏΠΎΠ»Π½ΡΠ΅Ρ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΡΠΎΠΎΠ±ΡΠ°Ρ ΠΎΠ± ΡΡΠΏΠ΅ΡΠ½ΠΎΠΌ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ.
|
|
168
|
/// ΠΡΠΏΠΎΠ»Π½ΡΠ΅Ρ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΡΠΎΠΎΠ±ΡΠ°Ρ ΠΎΠ± ΡΡΠΏΠ΅ΡΠ½ΠΎΠΌ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ.
|
|
169
|
/// </summary>
|
|
169
|
/// </summary>
|
|
170
|
/// <param name="result">Π Π΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ.</param>
|
|
170
|
/// <param name="result">Π Π΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ.</param>
|
|
171
|
/// <exception cref="InvalidOperationException">ΠΠ°Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΡΠΆΠ΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ</exception>
|
|
171
|
/// <exception cref="InvalidOperationException">ΠΠ°Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΡΠΆΠ΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ</exception>
|
|
172
|
public void Resolve(T result) {
|
|
172
|
public void Resolve(T result) {
|
|
173
|
if (BeginTransit()) {
|
|
173
|
if (BeginTransit()) {
|
|
174
|
m_result = result;
|
|
174
|
m_result = result;
|
|
175
|
CompleteTransit(SUCCEEDED_STATE);
|
|
175
|
CompleteTransit(SUCCEEDED_STATE);
|
|
176
|
OnStateChanged();
|
|
176
|
OnStateChanged();
|
|
177
|
} else {
|
|
177
|
} else {
|
|
178
|
WaitTransition();
|
|
178
|
WaitTransition();
|
|
179
|
if (m_state != CANCELLED_STATE)
|
|
179
|
if (m_state != CANCELLED_STATE)
|
|
180
|
throw new InvalidOperationException("The promise is already resolved");
|
|
180
|
throw new InvalidOperationException("The promise is already resolved");
|
|
181
|
}
|
|
181
|
}
|
|
182
|
}
|
|
182
|
}
|
|
183
|
|
|
183
|
|
|
184
|
/// <summary>
|
|
184
|
/// <summary>
|
|
185
|
/// ΠΡΠΏΠΎΠ»Π½ΡΠ΅Ρ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΡΠΎΠΎΠ±ΡΠ°Ρ ΠΎΠ± ΡΡΠΏΠ΅ΡΠ½ΠΎΠΌ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ. Π Π΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠΌ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ Π±ΡΠ΄Π΅Ρ ΠΏΡΡΡΠΎΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΡ.
|
|
185
|
/// ΠΡΠΏΠΎΠ»Π½ΡΠ΅Ρ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΡΠΎΠΎΠ±ΡΠ°Ρ ΠΎΠ± ΡΡΠΏΠ΅ΡΠ½ΠΎΠΌ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ. Π Π΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠΌ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ Π±ΡΠ΄Π΅Ρ ΠΏΡΡΡΠΎΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΡ.
|
|
186
|
/// </summary>
|
|
186
|
/// </summary>
|
|
187
|
/// <remarks>
|
|
187
|
/// <remarks>
|
|
188
|
/// ΠΠ°Π½Π½ΡΠΉ Π²Π°ΡΠΈΠ°Π½Ρ ΡΠ΄ΠΎΠ±Π΅Π½ Π² ΡΠ»ΡΡΠ°ΡΡ
, ΠΊΠΎΠ³Π΄Π° ΠΈΠ½ΡΠ΅ΡΠ΅ΡΠ΅Π½ ΡΠ°ΠΊΡ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ, Π½Π΅ΠΆΠ΅Π»ΠΈ ΠΏΠΎΠ»ΡΡΠ΅Π½Π½ΠΎΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅.
|
|
188
|
/// ΠΠ°Π½Π½ΡΠΉ Π²Π°ΡΠΈΠ°Π½Ρ ΡΠ΄ΠΎΠ±Π΅Π½ Π² ΡΠ»ΡΡΠ°ΡΡ
, ΠΊΠΎΠ³Π΄Π° ΠΈΠ½ΡΠ΅ΡΠ΅ΡΠ΅Π½ ΡΠ°ΠΊΡ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ, Π½Π΅ΠΆΠ΅Π»ΠΈ ΠΏΠΎΠ»ΡΡΠ΅Π½Π½ΠΎΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅.
|
|
189
|
/// </remarks>
|
|
189
|
/// </remarks>
|
|
190
|
public void Resolve() {
|
|
190
|
public void Resolve() {
|
|
191
|
Resolve(default(T));
|
|
191
|
Resolve(default(T));
|
|
192
|
}
|
|
192
|
}
|
|
193
|
|
|
193
|
|
|
194
|
/// <summary>
|
|
194
|
/// <summary>
|
|
195
|
/// ΠΡΠΏΠΎΠ»Π½ΡΠ΅Ρ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΡΠΎΠΎΠ±ΡΠ°Ρ ΠΎΠ± ΠΎΡΠΈΠ±ΠΊΠ΅
|
|
195
|
/// ΠΡΠΏΠΎΠ»Π½ΡΠ΅Ρ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΡΠΎΠΎΠ±ΡΠ°Ρ ΠΎΠ± ΠΎΡΠΈΠ±ΠΊΠ΅
|
|
196
|
/// </summary>
|
|
196
|
/// </summary>
|
|
197
|
/// <remarks>
|
|
197
|
/// <remarks>
|
|
198
|
/// ΠΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π΄ΠΎΠ»ΠΆΠ½ΠΎ ΡΠ°Π±ΠΎΡΠ°ΡΡ Π² ΠΌΠ½ΠΎΠ³ΠΎΠΏΡΠΎΡΠ½ΠΎΠΉ ΡΡΠ΅Π΄Π΅, ΠΏΡΠΈ Π΅Π³ΠΎ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΡΡΠ°Π·Ρ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΏΠΎΡΠΎΠΊΠΎΠ²
|
|
198
|
/// ΠΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π΄ΠΎΠ»ΠΆΠ½ΠΎ ΡΠ°Π±ΠΎΡΠ°ΡΡ Π² ΠΌΠ½ΠΎΠ³ΠΎΠΏΡΠΎΡΠ½ΠΎΠΉ ΡΡΠ΅Π΄Π΅, ΠΏΡΠΈ Π΅Π³ΠΎ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΡΡΠ°Π·Ρ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΏΠΎΡΠΎΠΊΠΎΠ²
|
|
199
|
/// ΠΌΠΎΠ³Ρ Π²Π΅ΡΠ½ΡΡΡ ΠΎΡΠΈΠ±ΠΊΡ, ΠΏΡΠΈ ΡΡΠΎΠΌ ΡΠΎΠ»ΡΠΊΠΎ ΠΏΠ΅ΡΠ²Π°Ρ Π±ΡΠ΄Π΅Ρ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½Π° Π² ΠΊΠ°ΡΠ΅ΡΡΠ²Π΅ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ°, ΠΎΡΡΠ°Π»ΡΠ½ΡΠ΅
|
|
199
|
/// ΠΌΠΎΠ³Ρ Π²Π΅ΡΠ½ΡΡΡ ΠΎΡΠΈΠ±ΠΊΡ, ΠΏΡΠΈ ΡΡΠΎΠΌ ΡΠΎΠ»ΡΠΊΠΎ ΠΏΠ΅ΡΠ²Π°Ρ Π±ΡΠ΄Π΅Ρ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½Π° Π² ΠΊΠ°ΡΠ΅ΡΡΠ²Π΅ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ°, ΠΎΡΡΠ°Π»ΡΠ½ΡΠ΅
|
|
200
|
/// Π±ΡΠ΄ΡΡ ΠΏΡΠΎΠΈΠ³Π½ΠΎΡΠΈΡΠΎΠ²Π°Π½Ρ.
|
|
200
|
/// Π±ΡΠ΄ΡΡ ΠΏΡΠΎΠΈΠ³Π½ΠΎΡΠΈΡΠΎΠ²Π°Π½Ρ.
|
|
201
|
/// </remarks>
|
|
201
|
/// </remarks>
|
|
202
|
/// <param name="error">ΠΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π²ΠΎΠ·Π½ΠΈΠΊΡΠ΅Π΅ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ</param>
|
|
202
|
/// <param name="error">ΠΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π²ΠΎΠ·Π½ΠΈΠΊΡΠ΅Π΅ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ</param>
|
|
203
|
/// <exception cref="InvalidOperationException">ΠΠ°Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΡΠΆΠ΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ</exception>
|
|
203
|
/// <exception cref="InvalidOperationException">ΠΠ°Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΡΠΆΠ΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ</exception>
|
|
204
|
public void Reject(Exception error) {
|
|
204
|
public void Reject(Exception error) {
|
|
205
|
if (BeginTransit()) {
|
|
205
|
if (BeginTransit()) {
|
|
206
|
m_error = error;
|
|
206
|
m_error = error;
|
|
207
|
CompleteTransit(REJECTED_STATE);
|
|
207
|
CompleteTransit(REJECTED_STATE);
|
|
208
|
OnStateChanged();
|
|
208
|
OnStateChanged();
|
|
209
|
} else {
|
|
209
|
} else {
|
|
210
|
WaitTransition();
|
|
210
|
WaitTransition();
|
|
211
|
if (m_state == SUCCEEDED_STATE)
|
|
211
|
if (m_state == SUCCEEDED_STATE)
|
|
212
|
throw new InvalidOperationException("The promise is already resolved");
|
|
212
|
throw new InvalidOperationException("The promise is already resolved");
|
|
213
|
}
|
|
213
|
}
|
|
214
|
}
|
|
214
|
}
|
|
215
|
|
|
215
|
|
|
216
|
/// <summary>
|
|
216
|
/// <summary>
|
|
217
|
/// ΠΡΠΌΠ΅Π½ΡΠ΅Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, Π΅ΡΠ»ΠΈ ΡΡΠΎ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ.
|
|
217
|
/// ΠΡΠΌΠ΅Π½ΡΠ΅Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, Π΅ΡΠ»ΠΈ ΡΡΠΎ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ.
|
|
218
|
/// </summary>
|
|
218
|
/// </summary>
|
|
219
|
/// <remarks>ΠΠ»Ρ ΠΎΠΏΡΠ΅Π΄Π΅Π»Π΅Π½ΠΈΡ Π±ΡΠ»Π° Π»ΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ ΠΎΡΠΌΠ΅Π½Π΅Π½Π° ΡΠ»Π΅Π΄ΡΠ΅Ρ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ ΡΠ²ΠΎΠΉΡΡΠ²ΠΎ <see cref="IsCancelled"/>.</remarks>
|
|
219
|
/// <remarks>ΠΠ»Ρ ΠΎΠΏΡΠ΅Π΄Π΅Π»Π΅Π½ΠΈΡ Π±ΡΠ»Π° Π»ΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ ΠΎΡΠΌΠ΅Π½Π΅Π½Π° ΡΠ»Π΅Π΄ΡΠ΅Ρ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ ΡΠ²ΠΎΠΉΡΡΠ²ΠΎ <see cref="IsCancelled"/>.</remarks>
|
|
220
|
public void Cancel() {
|
|
220
|
public void Cancel() {
|
|
221
|
if (m_cancellable && BeginTransit()) {
|
|
221
|
if (m_cancellable && BeginTransit()) {
|
|
222
|
CompleteTransit(CANCELLED_STATE);
|
|
222
|
CompleteTransit(CANCELLED_STATE);
|
|
223
|
OnStateChanged();
|
|
223
|
OnStateChanged();
|
|
224
|
}
|
|
224
|
}
|
|
225
|
}
|
|
225
|
}
|
|
226
|
|
|
226
|
|
|
227
|
public IPromise<T> Then(ResultHandler<T> success, ErrorHandler<T> error, Action cancel) {
|
|
227
|
public IPromise<T> Then(ResultHandler<T> success, ErrorHandler<T> error, Action cancel) {
|
|
228
|
if (success == null && error == null && cancel == null)
|
|
228
|
if (success == null && error == null && cancel == null)
|
|
229
|
return this;
|
|
229
|
return this;
|
|
230
|
|
|
230
|
|
|
231
|
var medium = new Promise<T>(this, true);
|
|
231
|
var medium = new Promise<T>(this, true);
|
|
232
|
|
|
232
|
|
|
233
|
AddHandler(success, error, cancel, medium);
|
|
233
|
AddHandler(success, error, cancel, medium);
|
|
234
|
|
|
234
|
|
|
235
|
return medium;
|
|
235
|
return medium;
|
|
236
|
}
|
|
236
|
}
|
|
237
|
|
|
237
|
|
|
238
|
/// <summary>
|
|
238
|
/// <summary>
|
|
239
|
/// Adds new handlers to this promise.
|
|
239
|
/// Adds new handlers to this promise.
|
|
240
|
/// </summary>
|
|
240
|
/// </summary>
|
|
241
|
/// <param name="success">The handler of the successfully completed operation.
|
|
241
|
/// <param name="success">The handler of the successfully completed operation.
|
|
242
|
/// This handler will recieve an operation result as a parameter.</param>
|
|
242
|
/// This handler will recieve an operation result as a parameter.</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>
|
|
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>
|
|
244
|
/// <returns>The new promise chained to this one.</returns>
|
|
244
|
/// <returns>The new promise chained to this one.</returns>
|
|
245
|
public IPromise<T> Then(ResultHandler<T> success, ErrorHandler<T> error) {
|
|
245
|
public IPromise<T> Then(ResultHandler<T> success, ErrorHandler<T> error) {
|
|
246
|
if (success == null && error == null)
|
|
246
|
if (success == null && error == null)
|
|
247
|
return this;
|
|
247
|
return this;
|
|
248
|
|
|
248
|
|
|
249
|
var medium = new Promise<T>(this, true);
|
|
249
|
var medium = new Promise<T>(this, true);
|
|
250
|
|
|
250
|
|
|
251
|
AddHandler(success, error, null, medium);
|
|
251
|
AddHandler(success, error, null, medium);
|
|
252
|
|
|
252
|
|
|
253
|
return medium;
|
|
253
|
return medium;
|
|
254
|
}
|
|
254
|
}
|
|
255
|
|
|
255
|
|
|
256
|
|
|
256
|
|
|
257
|
|
|
257
|
|
|
258
|
|
|
258
|
|
|
259
|
public IPromise<T> Then(ResultHandler<T> success) {
|
|
259
|
public IPromise<T> Then(ResultHandler<T> success) {
|
|
260
|
if (success == null)
|
|
260
|
if (success == null)
|
|
261
|
return this;
|
|
261
|
return this;
|
|
262
|
|
|
262
|
|
|
263
|
var medium = new Promise<T>(this, true);
|
|
263
|
var medium = new Promise<T>(this, true);
|
|
264
|
|
|
264
|
|
|
265
|
AddHandler(success, null, null, medium);
|
|
265
|
AddHandler(success, null, null, medium);
|
|
266
|
|
|
266
|
|
|
267
|
return medium;
|
|
267
|
return medium;
|
|
268
|
}
|
|
268
|
}
|
|
269
|
|
|
269
|
|
|
270
|
/// <summary>
|
|
270
|
/// <summary>
|
|
271
|
/// ΠΠΎΡΠ»Π΅Π΄Π½ΠΈΠΉ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ Π² ΡΠ΅ΠΏΠΎΡΠΊΠΈ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ.
|
|
271
|
/// ΠΠΎΡΠ»Π΅Π΄Π½ΠΈΠΉ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ Π² ΡΠ΅ΠΏΠΎΡΠΊΠΈ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ.
|
|
272
|
/// </summary>
|
|
272
|
/// </summary>
|
|
273
|
/// <param name="success"></param>
|
|
273
|
/// <param name="success"></param>
|
|
274
|
/// <param name="error"></param>
|
|
274
|
/// <param name="error"></param>
|
|
275
|
/// <param name="cancel"></param>
|
|
275
|
/// <param name="cancel"></param>
|
|
276
|
/// <remarks>
|
|
276
|
/// <remarks>
|
|
277
|
/// <para>
|
|
277
|
/// <para>
|
|
278
|
/// ΠΠ°Π½Π½ΡΠΉ ΠΌΠ΅ΡΠΎΠ΄ Π½Π΅ ΡΠΎΠ·Π΄Π°Π΅Ρ ΡΠ²ΡΠ·Π°Π½Π½ΠΎΠ³ΠΎ Ρ ΡΠ΅ΠΊΡΡΠΈΠΌ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΈ ΠΏΡΠ΅Π΄Π½Π°Π·Π½Π°ΡΠ΅Π½ Π΄Π»Ρ ΠΎΠΊΠΎΠ½ΡΠ°Π½ΠΈΡ
|
|
278
|
/// ΠΠ°Π½Π½ΡΠΉ ΠΌΠ΅ΡΠΎΠ΄ Π½Π΅ ΡΠΎΠ·Π΄Π°Π΅Ρ ΡΠ²ΡΠ·Π°Π½Π½ΠΎΠ³ΠΎ Ρ ΡΠ΅ΠΊΡΡΠΈΠΌ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΈ ΠΏΡΠ΅Π΄Π½Π°Π·Π½Π°ΡΠ΅Π½ Π΄Π»Ρ ΠΎΠΊΠΎΠ½ΡΠ°Π½ΠΈΡ
|
|
279
|
/// ΡΡΠΈΠ½Ρ
ΡΠΎΠ½Π½ΠΎΠΉ ΡΠ΅ΠΏΠΎΡΠΊΠΈ.
|
|
279
|
/// ΡΡΠΈΠ½Ρ
ΡΠΎΠ½Π½ΠΎΠΉ ΡΠ΅ΠΏΠΎΡΠΊΠΈ.
|
|
280
|
/// </para>
|
|
280
|
/// </para>
|
|
281
|
/// <para>
|
|
281
|
/// <para>
|
|
282
|
/// ΠΡΠ»ΠΈ Π΄Π°Π½Π½ΡΠΉ ΠΌΠ΅ΡΠΎΠ΄ Π²ΡΠ·Π²Π°ΡΡ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΡΠ°Π·, Π»ΠΈΠ±ΠΎ Π΄ΠΎΠ±Π°Π²ΠΈΡΡ Π΄ΡΡΠ³ΠΈΠ΅ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ, ΡΠΎ ΡΠ΅ΠΏΠΎΡΠΊΠ°
|
|
282
|
/// ΠΡΠ»ΠΈ Π΄Π°Π½Π½ΡΠΉ ΠΌΠ΅ΡΠΎΠ΄ Π²ΡΠ·Π²Π°ΡΡ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΡΠ°Π·, Π»ΠΈΠ±ΠΎ Π΄ΠΎΠ±Π°Π²ΠΈΡΡ Π΄ΡΡΠ³ΠΈΠ΅ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ, ΡΠΎ ΡΠ΅ΠΏΠΎΡΠΊΠ°
|
|
283
|
/// Π½Π΅ Π±ΡΠ΄Π΅Ρ ΠΎΠ΄ΠΈΠ½ΠΎΡΠ½ΠΎΠΉ <see cref="IsExclusive"/> ΠΈ, ΠΊΠ°ΠΊ ΡΠ»Π΅Π΄ΡΡΠ²ΠΈΠ΅, Π±ΡΠ΄Π΅Ρ Π½Π΅Π²ΠΎΠ·ΠΌΠΎΠΆΠ½Π° ΠΎΡΠΌΠ΅Π½Π°
|
|
283
|
/// Π½Π΅ Π±ΡΠ΄Π΅Ρ ΠΎΠ΄ΠΈΠ½ΠΎΡΠ½ΠΎΠΉ <see cref="IsExclusive"/> ΠΈ, ΠΊΠ°ΠΊ ΡΠ»Π΅Π΄ΡΡΠ²ΠΈΠ΅, Π±ΡΠ΄Π΅Ρ Π½Π΅Π²ΠΎΠ·ΠΌΠΎΠΆΠ½Π° ΠΎΡΠΌΠ΅Π½Π°
|
|
284
|
/// Π²ΡΠ΅ΠΉ ΡΠ΅ΠΏΠΈ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ ΡΠ½ΠΈΠ·Ρ (Ρ ΡΠ°ΠΌΠΎΠ³ΠΎ ΠΏΠΎΡΠ»Π΅Π΄Π½Π΅Π³ΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ).
|
|
284
|
/// Π²ΡΠ΅ΠΉ ΡΠ΅ΠΏΠΈ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ ΡΠ½ΠΈΠ·Ρ (Ρ ΡΠ°ΠΌΠΎΠ³ΠΎ ΠΏΠΎΡΠ»Π΅Π΄Π½Π΅Π³ΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ).
|
|
285
|
/// </para>
|
|
285
|
/// </para>
|
|
286
|
/// </remarks>
|
|
286
|
/// </remarks>
|
|
287
|
public void Last(ResultHandler<T> success, ErrorHandler error, Action cancel) {
|
|
287
|
public void Last(ResultHandler<T> success, ErrorHandler error, Action cancel) {
|
|
288
|
if (success == null && error == null && cancel == null)
|
|
288
|
if (success == null && error == null && cancel == null)
|
|
289
|
return;
|
|
289
|
return;
|
|
290
|
|
|
290
|
|
|
291
|
ErrorHandler<T> errorHandler = null;
|
|
291
|
ErrorHandler<T> errorHandler = null;
|
|
292
|
if (error != null)
|
|
292
|
if (error != null)
|
|
293
|
errorHandler = err => {
|
|
293
|
errorHandler = err => {
|
|
294
|
error(err);
|
|
294
|
error(err);
|
|
295
|
return default(T);
|
|
295
|
return default(T);
|
|
296
|
};
|
|
296
|
};
|
|
297
|
AddHandler(success, errorHandler, cancel, null);
|
|
297
|
AddHandler(success, errorHandler, cancel, null);
|
|
298
|
}
|
|
298
|
}
|
|
299
|
|
|
299
|
|
|
300
|
public void Last(ResultHandler<T> success, ErrorHandler error) {
|
|
300
|
public void Last(ResultHandler<T> success, ErrorHandler error) {
|
|
301
|
Last(success, error, null);
|
|
301
|
Last(success, error, null);
|
|
302
|
}
|
|
302
|
}
|
|
303
|
|
|
303
|
|
|
304
|
public void Last(ResultHandler<T> success) {
|
|
304
|
public void Last(ResultHandler<T> success) {
|
|
305
|
Last(success, null, null);
|
|
305
|
Last(success, null, null);
|
|
306
|
}
|
|
306
|
}
|
|
307
|
|
|
307
|
|
|
308
|
public IPromise Error(ErrorHandler error) {
|
|
308
|
public IPromise Error(ErrorHandler error) {
|
|
309
|
if (error == null)
|
|
309
|
if (error == null)
|
|
310
|
return this;
|
|
310
|
return this;
|
|
311
|
|
|
311
|
|
|
312
|
var medium = new Promise<T>(this, true);
|
|
312
|
var medium = new Promise<T>(this, true);
|
|
313
|
|
|
313
|
|
|
314
|
AddHandler(
|
|
314
|
AddHandler(
|
|
315
|
null,
|
|
315
|
null,
|
|
316
|
e => {
|
|
316
|
e => {
|
|
317
|
error(e);
|
|
317
|
error(e);
|
|
318
|
return default(T);
|
|
318
|
return default(T);
|
|
319
|
},
|
|
319
|
},
|
|
320
|
null,
|
|
320
|
null,
|
|
321
|
medium
|
|
321
|
medium
|
|
322
|
);
|
|
322
|
);
|
|
323
|
|
|
323
|
|
|
324
|
return medium;
|
|
324
|
return medium;
|
|
325
|
}
|
|
325
|
}
|
|
326
|
|
|
326
|
|
|
327
|
/// <summary>
|
|
327
|
/// <summary>
|
|
328
|
/// Handles error and allows to keep the promise.
|
|
328
|
/// Handles error and allows to keep the promise.
|
|
329
|
/// </summary>
|
|
329
|
/// </summary>
|
|
330
|
/// <remarks>
|
|
330
|
/// <remarks>
|
|
331
|
/// If the specified handler throws an exception, this exception will be used to reject the promise.
|
|
331
|
/// If the specified handler throws an exception, this exception will be used to reject the promise.
|
|
332
|
/// </remarks>
|
|
332
|
/// </remarks>
|
|
333
|
/// <param name="handler">The error handler which returns the result of the promise.</param>
|
|
333
|
/// <param name="handler">The error handler which returns the result of the promise.</param>
|
|
334
|
/// <returns>New promise.</returns>
|
|
334
|
/// <returns>New promise.</returns>
|
|
335
|
public IPromise<T> Error(ErrorHandler<T> handler) {
|
|
335
|
public IPromise<T> Error(ErrorHandler<T> handler) {
|
|
336
|
if (handler == null)
|
|
336
|
if (handler == null)
|
|
337
|
return this;
|
|
337
|
return this;
|
|
338
|
|
|
338
|
|
|
339
|
var medium = new Promise<T>(this, true);
|
|
339
|
var medium = new Promise<T>(this, true);
|
|
340
|
|
|
340
|
|
|
341
|
AddHandler(null, handler, null, medium);
|
|
341
|
AddHandler(null, handler, null, medium);
|
|
342
|
|
|
342
|
|
|
343
|
return medium;
|
|
343
|
return medium;
|
|
344
|
}
|
|
344
|
}
|
|
345
|
|
|
345
|
|
|
346
|
/// <summary>
|
|
346
|
/// <summary>
|
|
347
|
/// ΠΠΎΠ·Π²ΠΎΠ»ΡΠ΅Ρ ΠΏΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²Π°ΡΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΠΏΠΎΠ»Π΅Π½ΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΠΊ Π½ΠΎΠ²ΠΎΠΌΡ ΡΠΈΠΏΡ.
|
|
347
|
/// ΠΠΎΠ·Π²ΠΎΠ»ΡΠ΅Ρ ΠΏΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²Π°ΡΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΠΏΠΎΠ»Π΅Π½ΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΠΊ Π½ΠΎΠ²ΠΎΠΌΡ ΡΠΈΠΏΡ.
|
|
348
|
/// </summary>
|
|
348
|
/// </summary>
|
|
349
|
/// <typeparam name="TNew">ΠΠΎΠ²ΡΠΉ ΡΠΈΠΏ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ°.</typeparam>
|
|
349
|
/// <typeparam name="TNew">ΠΠΎΠ²ΡΠΉ ΡΠΈΠΏ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ°.</typeparam>
|
|
350
|
/// <param name="mapper">ΠΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²Π°Π½ΠΈΠ΅ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ° ΠΊ Π½ΠΎΠ²ΠΎΠΌΡ ΡΠΈΠΏΡ.</param>
|
|
350
|
/// <param name="mapper">ΠΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²Π°Π½ΠΈΠ΅ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ° ΠΊ Π½ΠΎΠ²ΠΎΠΌΡ ΡΠΈΠΏΡ.</param>
|
|
351
|
/// <param name="error">ΠΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΎΡΠΈΠ±ΠΊΠΈ. ΠΠ°Π½Π½ΡΠΉ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΏΠΎΠ»ΡΡΠΈΡ
|
|
351
|
/// <param name="error">ΠΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΎΡΠΈΠ±ΠΊΠΈ. ΠΠ°Π½Π½ΡΠΉ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΏΠΎΠ»ΡΡΠΈΡ
|
|
352
|
/// ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π²ΠΎΠ·Π½ΠΈΠΊΡΠ΅Π΅ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</param>
|
|
352
|
/// ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π²ΠΎΠ·Π½ΠΈΠΊΡΠ΅Π΅ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</param>
|
|
353
|
/// <returns>ΠΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΠΊΠΎΡΠΎΡΠΎΠ΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΈΡΡ
ΠΎΠ΄Π½ΠΎΠ³ΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ.</returns>
|
|
353
|
/// <returns>ΠΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΠΊΠΎΡΠΎΡΠΎΠ΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΈΡΡ
ΠΎΠ΄Π½ΠΎΠ³ΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ.</returns>
|
|
|
|
|
354
|
/// <param name = "cancel"></param>
|
|
354
|
public IPromise<TNew> Then<TNew>(ResultMapper<T, TNew> mapper, ErrorHandler<TNew> error, Action cancel) {
|
|
355
|
public IPromise<TNew> Then<TNew>(ResultMapper<T, TNew> mapper, ErrorHandler<TNew> error, Action cancel) {
|
|
355
|
Safe.ArgumentNotNull(mapper, "mapper");
|
|
356
|
Safe.ArgumentNotNull(mapper, "mapper");
|
|
356
|
|
|
357
|
|
|
357
|
// ΡΠΎΠ·Π΄Π°Π΅ΠΌ ΠΏΡΠΈΡΠ΅ΠΏΠ»Π΅Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅
|
|
358
|
// ΡΠΎΠ·Π΄Π°Π΅ΠΌ ΠΏΡΠΈΡΠ΅ΠΏΠ»Π΅Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅
|
|
358
|
var medium = new Promise<TNew>(this, true);
|
|
359
|
var medium = new Promise<TNew>(this, true);
|
|
359
|
|
|
360
|
|
|
360
|
ResultHandler<T> resultHandler = result => medium.Resolve(mapper(result));
|
|
361
|
ResultHandler<T> resultHandler = result => medium.Resolve(mapper(result));
|
|
361
|
ErrorHandler<T> errorHandler;
|
|
362
|
ErrorHandler<T> errorHandler;
|
|
362
|
if (error != null)
|
|
363
|
if (error != null)
|
|
363
|
errorHandler = e => {
|
|
364
|
errorHandler = e => {
|
|
364
|
try {
|
|
365
|
try {
|
|
365
|
medium.Resolve(error(e));
|
|
366
|
medium.Resolve(error(e));
|
|
366
|
} catch (Exception e2) {
|
|
367
|
} catch (Exception e2) {
|
|
367
|
// Π² ΡΠ»ΡΡΠ°Π΅ ΠΎΡΠΈΠ±ΠΊΠΈ Π½ΡΠΆΠ½ΠΎ ΠΏΠ΅ΡΠ΅Π΄Π°ΡΡ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π΄Π°Π»ΡΡΠ΅ ΠΏΠΎ ΡΠ΅ΠΏΠΎΡΠΊΠ΅
|
|
368
|
// Π² ΡΠ»ΡΡΠ°Π΅ ΠΎΡΠΈΠ±ΠΊΠΈ Π½ΡΠΆΠ½ΠΎ ΠΏΠ΅ΡΠ΅Π΄Π°ΡΡ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π΄Π°Π»ΡΡΠ΅ ΠΏΠΎ ΡΠ΅ΠΏΠΎΡΠΊΠ΅
|
|
368
|
medium.Reject(e2);
|
|
369
|
medium.Reject(e2);
|
|
369
|
}
|
|
370
|
}
|
|
370
|
return default(T);
|
|
371
|
return default(T);
|
|
371
|
};
|
|
372
|
};
|
|
372
|
else
|
|
373
|
else
|
|
373
|
errorHandler = e => {
|
|
374
|
errorHandler = e => {
|
|
374
|
medium.Reject(e);
|
|
375
|
medium.Reject(e);
|
|
375
|
return default(T);
|
|
376
|
return default(T);
|
|
376
|
};
|
|
377
|
};
|
|
377
|
|
|
378
|
|
|
378
|
Action cancelHandler;
|
|
379
|
Action cancelHandler;
|
|
379
|
if (cancel != null)
|
|
380
|
if (cancel != null)
|
|
380
|
cancelHandler = () => {
|
|
381
|
cancelHandler = () => {
|
|
381
|
cancel();
|
|
382
|
cancel();
|
|
382
|
medium.Cancel();
|
|
383
|
medium.Cancel();
|
|
383
|
};
|
|
384
|
};
|
|
384
|
else
|
|
385
|
else
|
|
385
|
cancelHandler = medium.Cancel;
|
|
386
|
cancelHandler = medium.Cancel;
|
|
386
|
|
|
387
|
|
|
387
|
|
|
388
|
|
|
388
|
AddHandler(
|
|
389
|
AddHandler(
|
|
389
|
resultHandler,
|
|
390
|
resultHandler,
|
|
390
|
errorHandler,
|
|
391
|
errorHandler,
|
|
391
|
cancelHandler,
|
|
392
|
cancelHandler,
|
|
392
|
null
|
|
393
|
null
|
|
393
|
);
|
|
394
|
);
|
|
394
|
|
|
395
|
|
|
395
|
return medium;
|
|
396
|
return medium;
|
|
396
|
}
|
|
397
|
}
|
|
397
|
|
|
398
|
|
|
398
|
public IPromise<TNew> Then<TNew>(ResultMapper<T, TNew> mapper, ErrorHandler<TNew> error) {
|
|
399
|
public IPromise<TNew> Then<TNew>(ResultMapper<T, TNew> mapper, ErrorHandler<TNew> error) {
|
|
399
|
return Then(mapper, error, null);
|
|
400
|
return Then(mapper, error, null);
|
|
400
|
}
|
|
401
|
}
|
|
401
|
|
|
402
|
|
|
402
|
public IPromise<TNew> Then<TNew>(ResultMapper<T, TNew> mapper) {
|
|
403
|
public IPromise<TNew> Then<TNew>(ResultMapper<T, TNew> mapper) {
|
|
403
|
return Then(mapper, null, null);
|
|
404
|
return Then(mapper, null, null);
|
|
404
|
}
|
|
405
|
}
|
|
405
|
|
|
406
|
|
|
406
|
/// <summary>
|
|
407
|
/// <summary>
|
|
407
|
/// Π‘ΡΠ΅ΠΏΠ»ΡΠ΅Ρ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ Π°ΡΠ½Ρ
ΡΠΎΠ½Π½ΡΡ
ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ. Π£ΠΊΠ°Π·Π°Π½Π½Π°Ρ Π°ΡΠΈΠ½Ρ
ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ Π±ΡΠ΄Π΅Ρ Π²ΡΠ·Π²Π°Π½Π° ΠΏΠΎΡΠ»Π΅
|
|
408
|
/// Π‘ΡΠ΅ΠΏΠ»ΡΠ΅Ρ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ Π°ΡΠ½Ρ
ΡΠΎΠ½Π½ΡΡ
ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ. Π£ΠΊΠ°Π·Π°Π½Π½Π°Ρ Π°ΡΠΈΠ½Ρ
ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ Π±ΡΠ΄Π΅Ρ Π²ΡΠ·Π²Π°Π½Π° ΠΏΠΎΡΠ»Π΅
|
|
408
|
/// Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΡΠ΅ΠΊΡΡΠ΅ΠΉ, Π° ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ ΡΠ΅ΠΊΡΡΠ΅ΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ Π΄Π»Ρ ΠΈΠ½ΠΈΡΠΈΠ°Π»ΠΈΠ·Π°ΡΠΈΠΈ
|
|
409
|
/// Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΡΠ΅ΠΊΡΡΠ΅ΠΉ, Π° ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ ΡΠ΅ΠΊΡΡΠ΅ΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ Π΄Π»Ρ ΠΈΠ½ΠΈΡΠΈΠ°Π»ΠΈΠ·Π°ΡΠΈΠΈ
|
|
409
|
/// Π½ΠΎΠ²ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.
|
|
410
|
/// Π½ΠΎΠ²ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.
|
|
410
|
/// </summary>
|
|
411
|
/// </summary>
|
|
411
|
/// <typeparam name="TNew">Π’ΠΈΠΏ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ° ΡΠΊΠ°Π·Π°Π½Π½ΠΎΠΉ Π°ΡΠΈΠ½Ρ
ΡΠΎΠ½Π½ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</typeparam>
|
|
412
|
/// <typeparam name="TNew">Π’ΠΈΠΏ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ° ΡΠΊΠ°Π·Π°Π½Π½ΠΎΠΉ Π°ΡΠΈΠ½Ρ
ΡΠΎΠ½Π½ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</typeparam>
|
|
412
|
/// <param name="chained">ΠΡΠΈΠ½Ρ
ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, ΠΊΠΎΡΠΎΡΠ°Ρ Π΄ΠΎΠ»ΠΆΠ½Π° Π±ΡΠ΄Π΅Ρ Π½Π°ΡΠ°ΡΡΡΡ ΠΏΠΎΡΠ»Π΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΡΠ΅ΠΊΡΡΠ΅ΠΉ.</param>
|
|
413
|
/// <param name="chained">ΠΡΠΈΠ½Ρ
ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, ΠΊΠΎΡΠΎΡΠ°Ρ Π΄ΠΎΠ»ΠΆΠ½Π° Π±ΡΠ΄Π΅Ρ Π½Π°ΡΠ°ΡΡΡΡ ΠΏΠΎΡΠ»Π΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΡΠ΅ΠΊΡΡΠ΅ΠΉ.</param>
|
|
413
|
/// <param name="error">ΠΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΎΡΠΈΠ±ΠΊΠΈ. ΠΠ°Π½Π½ΡΠΉ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΏΠΎΠ»ΡΡΠΈΡ
|
|
414
|
/// <param name="error">ΠΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΎΡΠΈΠ±ΠΊΠΈ. ΠΠ°Π½Π½ΡΠΉ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΏΠΎΠ»ΡΡΠΈΡ
|
|
414
|
/// ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π²ΠΎΠ·Π½ΠΈΠΊΡΠ΅Π΅ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΡΠ΅ΠΊΡΠ΅ΡΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</param>
|
|
415
|
/// ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π²ΠΎΠ·Π½ΠΈΠΊΡΠ΅Π΅ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΡΠ΅ΠΊΡΠ΅ΡΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</param>
|
|
415
|
/// <returns>ΠΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΠΊΠΎΡΠΎΡΠΎΠ΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ ΠΏΠΎ ΠΎΠΊΠΎΠ½ΡΠ°Π½ΠΈΡ ΡΠΊΠ°Π·Π°Π½Π½ΠΎΠΉ Π°ΡΠ½Ρ
ΡΠΎΠ½Π½ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</returns>
|
|
416
|
/// <returns>ΠΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΠΊΠΎΡΠΎΡΠΎΠ΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ ΠΏΠΎ ΠΎΠΊΠΎΠ½ΡΠ°Π½ΠΈΡ ΡΠΊΠ°Π·Π°Π½Π½ΠΎΠΉ Π°ΡΠ½Ρ
ΡΠΎΠ½Π½ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</returns>
|
|
|
|
|
417
|
/// <param name = "cancel"></param>
|
|
416
|
public IPromise<TNew> Chain<TNew>(ResultMapper<T, IPromise<TNew>> chained, ErrorHandler<IPromise<TNew>> error, Action cancel) {
|
|
418
|
public IPromise<TNew> Chain<TNew>(ResultMapper<T, IPromise<TNew>> chained, ErrorHandler<IPromise<TNew>> error, Action cancel) {
|
|
417
|
|
|
419
|
|
|
418
|
Safe.ArgumentNotNull(chained, "chained");
|
|
420
|
Safe.ArgumentNotNull(chained, "chained");
|
|
419
|
|
|
421
|
|
|
420
|
// ΠΏΡΠΎΠ±Π»Π΅ΠΌΠ° Π² ΡΠΎΠΌ, ΡΡΠΎ Π½Π° ΠΌΠΎΠΌΠ΅Π½Ρ ΡΠ²ΡΠ·ΡΠ²Π°Π½ΠΈΡ Π΅ΡΠ΅ Π½Π΅ Π½Π°ΡΠ°ΡΠ° Π°ΡΠΈΠ½Ρ
ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, ΠΏΠΎΡΡΠΎΠΌΡ Π½ΡΠΆΠ½ΠΎ
|
|
422
|
// ΠΏΡΠΎΠ±Π»Π΅ΠΌΠ° Π² ΡΠΎΠΌ, ΡΡΠΎ Π½Π° ΠΌΠΎΠΌΠ΅Π½Ρ ΡΠ²ΡΠ·ΡΠ²Π°Π½ΠΈΡ Π΅ΡΠ΅ Π½Π΅ Π½Π°ΡΠ°ΡΠ° Π°ΡΠΈΠ½Ρ
ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, ΠΏΠΎΡΡΠΎΠΌΡ Π½ΡΠΆΠ½ΠΎ
|
|
421
|
// ΡΠΎΠ·Π΄Π°ΡΡ ΠΏΠΎΡΡΠ΅Π΄Π½ΠΈΠΊΠ°, ΠΊ ΠΊΠΎΡΠΎΡΠΎΠΌΡ Π±ΡΠ΄ΡΡ ΠΏΠΎΠ΄Π²ΡΠ·ΡΠ²Π°ΡΡΡΡ ΡΠ»Π΅Π΄ΡΡΡΠΈΠ΅ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ.
|
|
423
|
// ΡΠΎΠ·Π΄Π°ΡΡ ΠΏΠΎΡΡΠ΅Π΄Π½ΠΈΠΊΠ°, ΠΊ ΠΊΠΎΡΠΎΡΠΎΠΌΡ Π±ΡΠ΄ΡΡ ΠΏΠΎΠ΄Π²ΡΠ·ΡΠ²Π°ΡΡΡΡ ΡΠ»Π΅Π΄ΡΡΡΠΈΠ΅ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ.
|
|
422
|
// ΠΊΠΎΠ³Π΄Π° Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½Π° ΡΠ΅Π°Π»ΡΠ½Π°Ρ Π°ΡΠΈΠ½Ρ
ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, ΠΎΠ½Π° ΠΎΠ±ΡΠ°ΡΠΈΡΡΡΡ ΠΊ ΠΏΠΎΡΡΠ΅Π΄Π½ΠΈΠΊΡ, ΡΡΠΎΠ±Ρ
|
|
424
|
// ΠΊΠΎΠ³Π΄Π° Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½Π° ΡΠ΅Π°Π»ΡΠ½Π°Ρ Π°ΡΠΈΠ½Ρ
ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, ΠΎΠ½Π° ΠΎΠ±ΡΠ°ΡΠΈΡΡΡΡ ΠΊ ΠΏΠΎΡΡΠ΅Π΄Π½ΠΈΠΊΡ, ΡΡΠΎΠ±Ρ
|
|
423
|
// ΠΏΠ΅ΡΠ΅Π΄Π°ΡΡ ΡΠ΅ΡΠ΅Π· Π½Π΅Π³ΠΎ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ ΡΠ°Π±ΠΎΡΡ.
|
|
425
|
// ΠΏΠ΅ΡΠ΅Π΄Π°ΡΡ ΡΠ΅ΡΠ΅Π· Π½Π΅Π³ΠΎ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ ΡΠ°Π±ΠΎΡΡ.
|
|
424
|
var medium = new Promise<TNew>(this, true);
|
|
426
|
var medium = new Promise<TNew>(this, true);
|
|
425
|
|
|
427
|
|
|
426
|
ResultHandler<T> resultHandler = delegate(T result) {
|
|
428
|
ResultHandler<T> resultHandler = delegate(T result) {
|
|
427
|
if (medium.IsCancelled)
|
|
429
|
if (medium.IsCancelled)
|
|
428
|
return;
|
|
430
|
return;
|
|
429
|
|
|
431
|
|
|
430
|
var promise = chained(result);
|
|
432
|
var promise = chained(result);
|
|
431
|
|
|
433
|
|
|
432
|
promise.Last(
|
|
434
|
promise.Last(
|
|
433
|
medium.Resolve,
|
|
435
|
medium.Resolve,
|
|
434
|
medium.Reject,
|
|
436
|
medium.Reject,
|
|
435
|
() => medium.Reject(new OperationCanceledException()) // Π²Π½Π΅ΡΠ½ΡΡ ΠΎΡΠΌΠ΅Π½Π° ΡΠ²ΡΠ·Π°Π½Π½ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΡΠ°ΡΡΠΌΠ°ΡΡΠΈΠ²Π°Π΅ΡΡΡ ΠΊΠ°ΠΊ ΠΎΡΠΈΠ±ΠΊΠ°
|
|
437
|
() => medium.Reject(new OperationCanceledException()) // Π²Π½Π΅ΡΠ½ΡΡ ΠΎΡΠΌΠ΅Π½Π° ΡΠ²ΡΠ·Π°Π½Π½ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΡΠ°ΡΡΠΌΠ°ΡΡΠΈΠ²Π°Π΅ΡΡΡ ΠΊΠ°ΠΊ ΠΎΡΠΈΠ±ΠΊΠ°
|
|
436
|
);
|
|
438
|
);
|
|
437
|
|
|
439
|
|
|
438
|
// notify chained operation that it's not needed anymore
|
|
440
|
// notify chained operation that it's not needed anymore
|
|
439
|
// ΠΏΠΎΡΡΠ΄ΠΎΠΊ Π²ΡΠ·ΠΎΠ²Π° Then, Cancelled Π²Π°ΠΆΠ΅Π½, ΠΏΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΠΎΡ ΡΡΠΎΠ³ΠΎ
|
|
441
|
// ΠΏΠΎΡΡΠ΄ΠΎΠΊ Π²ΡΠ·ΠΎΠ²Π° Then, Cancelled Π²Π°ΠΆΠ΅Π½, ΠΏΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΠΎΡ ΡΡΠΎΠ³ΠΎ
|
|
440
|
// Π·Π°Π²ΠΈΡΠΈΡ IsExclusive
|
|
442
|
// Π·Π°Π²ΠΈΡΠΈΡ IsExclusive
|
|
441
|
medium.Cancelled(() => {
|
|
443
|
medium.Cancelled(() => {
|
|
442
|
if (promise.IsExclusive)
|
|
444
|
if (promise.IsExclusive)
|
|
443
|
promise.Cancel();
|
|
445
|
promise.Cancel();
|
|
444
|
});
|
|
446
|
});
|
|
445
|
};
|
|
447
|
};
|
|
446
|
|
|
448
|
|
|
447
|
ErrorHandler<T> errorHandler;
|
|
449
|
ErrorHandler<T> errorHandler;
|
|
448
|
|
|
450
|
|
|
449
|
if (error != null)
|
|
451
|
if (error != null)
|
|
450
|
errorHandler = delegate(Exception e) {
|
|
452
|
errorHandler = delegate(Exception e) {
|
|
451
|
try {
|
|
453
|
try {
|
|
452
|
var promise = error(e);
|
|
454
|
var promise = error(e);
|
|
453
|
|
|
455
|
|
|
454
|
promise.Last(
|
|
456
|
promise.Last(
|
|
455
|
medium.Resolve,
|
|
457
|
medium.Resolve,
|
|
456
|
medium.Reject,
|
|
458
|
medium.Reject,
|
|
457
|
() => medium.Reject(new OperationCanceledException()) // Π²Π½Π΅ΡΠ½ΡΡ ΠΎΡΠΌΠ΅Π½Π° ΡΠ²ΡΠ·Π°Π½Π½ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΡΠ°ΡΡΠΌΠ°ΡΡΠΈΠ²Π°Π΅ΡΡΡ ΠΊΠ°ΠΊ ΠΎΡΠΈΠ±ΠΊΠ°
|
|
459
|
() => medium.Reject(new OperationCanceledException()) // Π²Π½Π΅ΡΠ½ΡΡ ΠΎΡΠΌΠ΅Π½Π° ΡΠ²ΡΠ·Π°Π½Π½ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΡΠ°ΡΡΠΌΠ°ΡΡΠΈΠ²Π°Π΅ΡΡΡ ΠΊΠ°ΠΊ ΠΎΡΠΈΠ±ΠΊΠ°
|
|
458
|
);
|
|
460
|
);
|
|
459
|
|
|
461
|
|
|
460
|
// notify chained operation that it's not needed anymore
|
|
462
|
// notify chained operation that it's not needed anymore
|
|
461
|
// ΠΏΠΎΡΡΠ΄ΠΎΠΊ Π²ΡΠ·ΠΎΠ²Π° Then, Cancelled Π²Π°ΠΆΠ΅Π½, ΠΏΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΠΎΡ ΡΡΠΎΠ³ΠΎ
|
|
463
|
// ΠΏΠΎΡΡΠ΄ΠΎΠΊ Π²ΡΠ·ΠΎΠ²Π° Then, Cancelled Π²Π°ΠΆΠ΅Π½, ΠΏΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΠΎΡ ΡΡΠΎΠ³ΠΎ
|
|
462
|
// Π·Π°Π²ΠΈΡΠΈΡ IsExclusive
|
|
464
|
// Π·Π°Π²ΠΈΡΠΈΡ IsExclusive
|
|
463
|
medium.Cancelled(() => {
|
|
465
|
medium.Cancelled(() => {
|
|
464
|
if (promise.IsExclusive)
|
|
466
|
if (promise.IsExclusive)
|
|
465
|
promise.Cancel();
|
|
467
|
promise.Cancel();
|
|
466
|
});
|
|
468
|
});
|
|
467
|
} catch (Exception e2) {
|
|
469
|
} catch (Exception e2) {
|
|
468
|
medium.Reject(e2);
|
|
470
|
medium.Reject(e2);
|
|
469
|
}
|
|
471
|
}
|
|
470
|
return default(T);
|
|
472
|
return default(T);
|
|
471
|
};
|
|
473
|
};
|
|
472
|
else
|
|
474
|
else
|
|
473
|
errorHandler = err => {
|
|
475
|
errorHandler = err => {
|
|
474
|
medium.Reject(err);
|
|
476
|
medium.Reject(err);
|
|
475
|
return default(T);
|
|
477
|
return default(T);
|
|
476
|
};
|
|
478
|
};
|
|
477
|
|
|
479
|
|
|
478
|
|
|
480
|
|
|
479
|
Action cancelHandler;
|
|
481
|
Action cancelHandler;
|
|
480
|
if (cancel != null)
|
|
482
|
if (cancel != null)
|
|
481
|
cancelHandler = () => {
|
|
483
|
cancelHandler = () => {
|
|
482
|
if (cancel != null)
|
|
484
|
if (cancel != null)
|
|
483
|
cancel();
|
|
485
|
cancel();
|
|
484
|
medium.Cancel();
|
|
486
|
medium.Cancel();
|
|
485
|
};
|
|
487
|
};
|
|
486
|
else
|
|
488
|
else
|
|
487
|
cancelHandler = medium.Cancel;
|
|
489
|
cancelHandler = medium.Cancel;
|
|
488
|
|
|
490
|
|
|
489
|
AddHandler(
|
|
491
|
AddHandler(
|
|
490
|
resultHandler,
|
|
492
|
resultHandler,
|
|
491
|
errorHandler,
|
|
493
|
errorHandler,
|
|
492
|
cancelHandler,
|
|
494
|
cancelHandler,
|
|
493
|
null
|
|
495
|
null
|
|
494
|
);
|
|
496
|
);
|
|
495
|
|
|
497
|
|
|
496
|
return medium;
|
|
498
|
return medium;
|
|
497
|
}
|
|
499
|
}
|
|
498
|
|
|
500
|
|
|
499
|
public IPromise<TNew> Chain<TNew>(ResultMapper<T, IPromise<TNew>> chained, ErrorHandler<IPromise<TNew>> error) {
|
|
501
|
public IPromise<TNew> Chain<TNew>(ResultMapper<T, IPromise<TNew>> chained, ErrorHandler<IPromise<TNew>> error) {
|
|
500
|
return Chain(chained, error, null);
|
|
502
|
return Chain(chained, error, null);
|
|
501
|
}
|
|
503
|
}
|
|
502
|
|
|
504
|
|
|
503
|
public IPromise<TNew> Chain<TNew>(ResultMapper<T, IPromise<TNew>> chained) {
|
|
505
|
public IPromise<TNew> Chain<TNew>(ResultMapper<T, IPromise<TNew>> chained) {
|
|
504
|
return Chain(chained, null, null);
|
|
506
|
return Chain(chained, null, null);
|
|
505
|
}
|
|
507
|
}
|
|
506
|
|
|
508
|
|
|
507
|
public IPromise<T> Cancelled(Action handler) {
|
|
509
|
public IPromise<T> Cancelled(Action handler) {
|
|
508
|
var medium = new Promise<T>(this,true);
|
|
510
|
var medium = new Promise<T>(this,true);
|
|
509
|
AddHandler(null, null, handler, medium);
|
|
511
|
AddHandler(null, null, handler, medium);
|
|
510
|
return medium;
|
|
512
|
return medium;
|
|
511
|
}
|
|
513
|
}
|
|
512
|
|
|
514
|
|
|
513
|
/// <summary>
|
|
515
|
/// <summary>
|
|
514
|
/// Adds the specified handler for all cases (success, error, cancel)
|
|
516
|
/// Adds the specified handler for all cases (success, error, cancel)
|
|
515
|
/// </summary>
|
|
517
|
/// </summary>
|
|
516
|
/// <param name="handler">The handler that will be called anyway</param>
|
|
518
|
/// <param name="handler">The handler that will be called anyway</param>
|
|
517
|
/// <returns>self</returns>
|
|
519
|
/// <returns>self</returns>
|
|
518
|
public IPromise<T> Anyway(Action handler) {
|
|
520
|
public IPromise<T> Anyway(Action handler) {
|
|
519
|
Safe.ArgumentNotNull(handler, "handler");
|
|
521
|
Safe.ArgumentNotNull(handler, "handler");
|
|
520
|
|
|
522
|
|
|
521
|
AddHandler(
|
|
523
|
AddHandler(
|
|
522
|
x => handler(),
|
|
524
|
x => handler(),
|
|
523
|
e => {
|
|
525
|
e => {
|
|
524
|
handler();
|
|
526
|
handler();
|
|
525
|
throw new TransientPromiseException(e);
|
|
527
|
throw new TransientPromiseException(e);
|
|
526
|
},
|
|
528
|
},
|
|
527
|
handler,
|
|
529
|
handler,
|
|
528
|
null
|
|
530
|
null
|
|
529
|
);
|
|
531
|
);
|
|
530
|
return this;
|
|
532
|
return this;
|
|
531
|
}
|
|
533
|
}
|
|
532
|
|
|
534
|
|
|
533
|
/// <summary>
|
|
535
|
/// <summary>
|
|
534
|
/// ΠΡΠ΅ΠΎΠ±ΡΠ°Π·ΡΠ΅Ρ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΊ Π½ΡΠΆΠ½ΠΎΠΌΡ ΡΠΈΠΏΡ
|
|
536
|
/// ΠΡΠ΅ΠΎΠ±ΡΠ°Π·ΡΠ΅Ρ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΊ Π½ΡΠΆΠ½ΠΎΠΌΡ ΡΠΈΠΏΡ
|
|
535
|
/// </summary>
|
|
537
|
/// </summary>
|
|
536
|
/// <typeparam name="T2"></typeparam>
|
|
538
|
/// <typeparam name="T2"></typeparam>
|
|
537
|
/// <returns></returns>
|
|
539
|
/// <returns></returns>
|
|
538
|
public IPromise<T2> Cast<T2>() {
|
|
540
|
public IPromise<T2> Cast<T2>() {
|
|
539
|
return Then(x => (T2)(object)x, null);
|
|
541
|
return Then(x => (T2)(object)x, null);
|
|
540
|
}
|
|
542
|
}
|
|
541
|
|
|
543
|
|
|
542
|
/// <summary>
|
|
544
|
/// <summary>
|
|
543
|
/// ΠΠΎΠΆΠΈΠ΄Π°Π΅ΡΡΡ ΠΎΡΠ»ΠΎΠΆΠ΅Π½Π½ΠΎΠ³ΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΈ Π² ΡΠ»ΡΡΠ°Π΅ ΡΡΠΏΠ΅Ρ
Π°, Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅Ρ
|
|
545
|
/// ΠΠΎΠΆΠΈΠ΄Π°Π΅ΡΡΡ ΠΎΡΠ»ΠΎΠΆΠ΅Π½Π½ΠΎΠ³ΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΈ Π² ΡΠ»ΡΡΠ°Π΅ ΡΡΠΏΠ΅Ρ
Π°, Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅Ρ
|
|
544
|
/// Π΅Π³ΠΎ, ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ, Π² ΠΏΡΠΎΡΠΈΠ²Π½ΠΎΠΌ ΡΠ»ΡΡΠ°Π΅ Π±ΡΠΎΡΠ°Π΅Ρ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅.
|
|
546
|
/// Π΅Π³ΠΎ, ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ, Π² ΠΏΡΠΎΡΠΈΠ²Π½ΠΎΠΌ ΡΠ»ΡΡΠ°Π΅ Π±ΡΠΎΡΠ°Π΅Ρ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅.
|
|
545
|
/// </summary>
|
|
547
|
/// </summary>
|
|
546
|
/// <remarks>
|
|
548
|
/// <remarks>
|
|
547
|
/// <para>
|
|
549
|
/// <para>
|
|
548
|
/// ΠΡΠ»ΠΈ ΠΎΠΆΠΈΠ΄Π°Π½ΠΈΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ Π±ΡΠ»ΠΎ ΠΏΡΠ΅ΡΠ²Π°Π½ΠΎ ΠΏΠΎ ΡΠ°ΠΉΠΌΠ°ΡΡΡ, ΡΡΠΎ Π½Π΅ Π·Π½Π°ΡΠΈΡ,
|
|
550
|
/// ΠΡΠ»ΠΈ ΠΎΠΆΠΈΠ΄Π°Π½ΠΈΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ Π±ΡΠ»ΠΎ ΠΏΡΠ΅ΡΠ²Π°Π½ΠΎ ΠΏΠΎ ΡΠ°ΠΉΠΌΠ°ΡΡΡ, ΡΡΠΎ Π½Π΅ Π·Π½Π°ΡΠΈΡ,
|
|
549
|
/// ΡΡΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π±ΡΠ»ΠΎ ΠΎΡΠΌΠ΅Π½Π΅Π½ΠΎ ΠΈΠ»ΠΈ ΡΡΠΎ-ΡΠΎ Π² ΡΡΠΎΠΌ ΡΠΎΠ΄Π΅, ΡΡΠΎ ΡΠΎΠ»ΡΠΊΠΎ
|
|
551
|
/// ΡΡΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π±ΡΠ»ΠΎ ΠΎΡΠΌΠ΅Π½Π΅Π½ΠΎ ΠΈΠ»ΠΈ ΡΡΠΎ-ΡΠΎ Π² ΡΡΠΎΠΌ ΡΠΎΠ΄Π΅, ΡΡΠΎ ΡΠΎΠ»ΡΠΊΠΎ
|
|
550
|
/// ΠΎΠ·Π½Π°ΡΠ°Π΅Ρ, ΡΡΠΎ ΠΌΡ Π΅Π³ΠΎ Π½Π΅ Π΄ΠΎΠΆΠ΄Π°Π»ΠΈΡΡ, ΠΎΠ΄Π½Π°ΠΊΠΎ Π²ΡΠ΅ Π·Π°ΡΠ΅Π³ΠΈΡΡΡΠΈΡΠΎΠ²Π°Π½Π½ΡΠ΅
|
|
552
|
/// ΠΎΠ·Π½Π°ΡΠ°Π΅Ρ, ΡΡΠΎ ΠΌΡ Π΅Π³ΠΎ Π½Π΅ Π΄ΠΎΠΆΠ΄Π°Π»ΠΈΡΡ, ΠΎΠ΄Π½Π°ΠΊΠΎ Π²ΡΠ΅ Π·Π°ΡΠ΅Π³ΠΈΡΡΡΠΈΡΠΎΠ²Π°Π½Π½ΡΠ΅
|
|
551
|
/// ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ, ΠΊΠ°ΠΊ Π±ΡΠ»ΠΈ ΡΠ°ΠΊ ΠΎΡΡΠ°Π»ΠΈΡΡ ΠΈ ΠΎΠ½ΠΈ Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ, ΠΊΠΎΠ³Π΄Π°
|
|
553
|
/// ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ, ΠΊΠ°ΠΊ Π±ΡΠ»ΠΈ ΡΠ°ΠΊ ΠΎΡΡΠ°Π»ΠΈΡΡ ΠΈ ΠΎΠ½ΠΈ Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ, ΠΊΠΎΠ³Π΄Π°
|
|
552
|
/// ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ.
|
|
554
|
/// ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ.
|
|
553
|
/// </para>
|
|
555
|
/// </para>
|
|
554
|
/// <para>
|
|
556
|
/// <para>
|
|
555
|
/// Π’Π°ΠΊΠΎΠ΅ ΠΏΠΎΠ²Π΅Π΄Π΅Π½ΠΈΠ΅ Π²ΠΏΠΎΠ»Π½Π΅ ΠΎΠΏΡΠ°Π²Π΄Π°Π½ΠΎ ΠΏΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΡΠ°ΠΉΠΌΠ°ΡΡ ΠΌΠΎΠΆΠ΅Ρ ΠΈΡΡΠ΅ΡΡ
|
|
557
|
/// Π’Π°ΠΊΠΎΠ΅ ΠΏΠΎΠ²Π΅Π΄Π΅Π½ΠΈΠ΅ Π²ΠΏΠΎΠ»Π½Π΅ ΠΎΠΏΡΠ°Π²Π΄Π°Π½ΠΎ ΠΏΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΡΠ°ΠΉΠΌΠ°ΡΡ ΠΌΠΎΠΆΠ΅Ρ ΠΈΡΡΠ΅ΡΡ
|
|
556
|
/// Π² ΡΠΎΡ ΠΌΠΎΠΌΠ΅Π½Ρ, ΠΊΠΎΠ³Π΄Π° Π½Π°ΡΠ°Π»Π°ΡΡ ΠΎΠ±ΡΠ°Π±ΠΎΡΠΊΠ° ΡΠ΅ΠΏΠΎΡΠΊΠΈ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΎΠ², ΠΈ
|
|
558
|
/// Π² ΡΠΎΡ ΠΌΠΎΠΌΠ΅Π½Ρ, ΠΊΠΎΠ³Π΄Π° Π½Π°ΡΠ°Π»Π°ΡΡ ΠΎΠ±ΡΠ°Π±ΠΎΡΠΊΠ° ΡΠ΅ΠΏΠΎΡΠΊΠΈ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΎΠ², ΠΈ
|
|
557
|
/// ΠΊ ΡΠΎΠΌΡ ΠΆΠ΅ ΡΠ΅ΠΊΡΡΠ΅Π΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ ΡΡΠΎΡΡΡ Π² ΡΠ΅ΠΏΠΎΡΠΊΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ ΠΈ Π΅Π³ΠΎ
|
|
559
|
/// ΠΊ ΡΠΎΠΌΡ ΠΆΠ΅ ΡΠ΅ΠΊΡΡΠ΅Π΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ ΡΡΠΎΡΡΡ Π² ΡΠ΅ΠΏΠΎΡΠΊΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ ΠΈ Π΅Π³ΠΎ
|
|
558
|
/// ΠΎΡΠΊΠ»ΠΎΠ½Π΅Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ ΠΏΡΠΈΠ²Π΅ΡΡΠΈ ΠΊ Π½Π΅ΠΏΡΠΎΠ³Π½ΠΎΠ·ΠΈΡΡΠ΅ΠΌΠΎΠΌΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ.
|
|
560
|
/// ΠΎΡΠΊΠ»ΠΎΠ½Π΅Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ ΠΏΡΠΈΠ²Π΅ΡΡΠΈ ΠΊ Π½Π΅ΠΏΡΠΎΠ³Π½ΠΎΠ·ΠΈΡΡΠ΅ΠΌΠΎΠΌΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ.
|
|
559
|
/// </para>
|
|
561
|
/// </para>
|
|
560
|
/// </remarks>
|
|
562
|
/// </remarks>
|
|
561
|
/// <param name="timeout">ΠΡΠ΅ΠΌΡ ΠΎΠΆΠΈΠ΄Π°Π½ΠΈΡ</param>
|
|
563
|
/// <param name="timeout">ΠΡΠ΅ΠΌΡ ΠΎΠΆΠΈΠ΄Π°Π½ΠΈΡ</param>
|
|
562
|
/// <returns>Π Π΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ</returns>
|
|
564
|
/// <returns>Π Π΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ</returns>
|
|
563
|
public T Join(int timeout) {
|
|
565
|
public T Join(int timeout) {
|
|
564
|
var evt = new ManualResetEvent(false);
|
|
566
|
var evt = new ManualResetEvent(false);
|
|
565
|
Anyway(() => evt.Set());
|
|
567
|
Anyway(() => evt.Set());
|
|
566
|
|
|
568
|
|
|
567
|
if (!evt.WaitOne(timeout, true))
|
|
569
|
if (!evt.WaitOne(timeout, true))
|
|
568
|
throw new TimeoutException();
|
|
570
|
throw new TimeoutException();
|
|
569
|
|
|
571
|
|
|
570
|
switch (m_state) {
|
|
572
|
switch (m_state) {
|
|
571
|
case SUCCEEDED_STATE:
|
|
573
|
case SUCCEEDED_STATE:
|
|
572
|
return m_result;
|
|
574
|
return m_result;
|
|
573
|
case CANCELLED_STATE:
|
|
575
|
case CANCELLED_STATE:
|
|
574
|
throw new OperationCanceledException();
|
|
576
|
throw new OperationCanceledException();
|
|
575
|
case REJECTED_STATE:
|
|
577
|
case REJECTED_STATE:
|
|
576
|
throw new TargetInvocationException(m_error);
|
|
578
|
throw new TargetInvocationException(m_error);
|
|
577
|
default:
|
|
579
|
default:
|
|
578
|
throw new ApplicationException(String.Format("Invalid promise state {0}", m_state));
|
|
580
|
throw new ApplicationException(String.Format("Invalid promise state {0}", m_state));
|
|
579
|
}
|
|
581
|
}
|
|
580
|
}
|
|
582
|
}
|
|
581
|
|
|
583
|
|
|
582
|
public T Join() {
|
|
584
|
public T Join() {
|
|
583
|
return Join(Timeout.Infinite);
|
|
585
|
return Join(Timeout.Infinite);
|
|
584
|
}
|
|
586
|
}
|
|
585
|
|
|
587
|
|
|
586
|
void AddHandler(ResultHandler<T> success, ErrorHandler<T> error, Action cancel, Promise<T> medium) {
|
|
588
|
void AddHandler(ResultHandler<T> success, ErrorHandler<T> error, Action cancel, Promise<T> medium) {
|
|
587
|
if (success != null || error != null)
|
|
589
|
if (success != null || error != null)
|
|
588
|
Interlocked.Increment(ref m_childrenCount);
|
|
590
|
Interlocked.Increment(ref m_childrenCount);
|
|
589
|
|
|
591
|
|
|
590
|
var handler = new HandlerDescriptor {
|
|
592
|
var handler = new HandlerDescriptor {
|
|
591
|
resultHandler = success,
|
|
593
|
resultHandler = success,
|
|
592
|
errorHandler = error,
|
|
594
|
errorHandler = error,
|
|
593
|
cancellHandler = cancel,
|
|
595
|
cancellHandler = cancel,
|
|
594
|
medium = medium
|
|
596
|
medium = medium
|
|
595
|
};
|
|
597
|
};
|
|
596
|
|
|
598
|
|
|
597
|
bool queued;
|
|
599
|
bool queued;
|
|
598
|
|
|
600
|
|
|
599
|
if (!IsResolved) {
|
|
601
|
if (!IsResolved) {
|
|
600
|
m_handlers.Enqueue(handler);
|
|
602
|
m_handlers.Enqueue(handler);
|
|
601
|
queued = true;
|
|
603
|
queued = true;
|
|
602
|
} else {
|
|
604
|
} else {
|
|
603
|
// the promise is in resolved state, just invoke the handled with minimum overhead
|
|
605
|
// the promise is in resolved state, just invoke the handled with minimum overhead
|
|
604
|
queued = false;
|
|
606
|
queued = false;
|
|
605
|
InvokeHandler(handler);
|
|
607
|
InvokeHandler(handler);
|
|
606
|
}
|
|
608
|
}
|
|
607
|
|
|
609
|
|
|
608
|
if (queued && IsResolved && m_handlers.TryDequeue(out handler))
|
|
610
|
if (queued && IsResolved && m_handlers.TryDequeue(out handler))
|
|
609
|
// if the promise have been resolved while we was adding handler to the queue
|
|
611
|
// if the promise have been resolved while we was adding handler to the queue
|
|
610
|
// we can't guarantee that someone is still processing it
|
|
612
|
// we can't guarantee that someone is still processing it
|
|
611
|
// therefore we will fetch a handler from the queue and execute it
|
|
613
|
// therefore we will fetch a handler from the queue and execute it
|
|
612
|
// note that fetched handler may be not the one that we have added
|
|
614
|
// note that fetched handler may be not the one that we have added
|
|
613
|
// even we can fetch no handlers at all :)
|
|
615
|
// even we can fetch no handlers at all :)
|
|
614
|
InvokeHandler(handler);
|
|
616
|
InvokeHandler(handler);
|
|
615
|
}
|
|
617
|
}
|
|
616
|
|
|
618
|
|
|
617
|
protected virtual void InvokeHandler(HandlerDescriptor handler) {
|
|
619
|
protected virtual void InvokeHandler(HandlerDescriptor handler) {
|
|
618
|
switch (m_state) {
|
|
620
|
switch (m_state) {
|
|
619
|
case SUCCEEDED_STATE:
|
|
621
|
case SUCCEEDED_STATE:
|
|
620
|
handler.Resolve(m_result);
|
|
622
|
handler.Resolve(m_result);
|
|
621
|
break;
|
|
623
|
break;
|
|
622
|
case REJECTED_STATE:
|
|
624
|
case REJECTED_STATE:
|
|
623
|
handler.Reject(m_error);
|
|
625
|
handler.Reject(m_error);
|
|
624
|
break;
|
|
626
|
break;
|
|
625
|
case CANCELLED_STATE:
|
|
627
|
case CANCELLED_STATE:
|
|
626
|
handler.Cancel();
|
|
628
|
handler.Cancel();
|
|
627
|
break;
|
|
629
|
break;
|
|
628
|
default:
|
|
630
|
default:
|
|
629
|
// do nothing
|
|
631
|
// do nothing
|
|
630
|
return;
|
|
632
|
return;
|
|
631
|
}
|
|
633
|
}
|
|
632
|
}
|
|
634
|
}
|
|
633
|
|
|
635
|
|
|
634
|
void OnStateChanged() {
|
|
636
|
void OnStateChanged() {
|
|
635
|
HandlerDescriptor handler;
|
|
637
|
HandlerDescriptor handler;
|
|
636
|
while (m_handlers.TryDequeue(out handler))
|
|
638
|
while (m_handlers.TryDequeue(out handler))
|
|
637
|
InvokeHandler(handler);
|
|
639
|
InvokeHandler(handler);
|
|
638
|
}
|
|
640
|
}
|
|
639
|
|
|
641
|
|
|
640
|
public bool IsExclusive {
|
|
642
|
public bool IsExclusive {
|
|
641
|
get {
|
|
643
|
get {
|
|
642
|
return m_childrenCount <= 1;
|
|
644
|
return m_childrenCount <= 1;
|
|
643
|
}
|
|
645
|
}
|
|
644
|
}
|
|
646
|
}
|
|
645
|
|
|
647
|
|
|
646
|
/// <summary>
|
|
648
|
/// <summary>
|
|
647
|
/// ΠΠ±ΡΠ΅Π΄ΠΈΠ½ΡΠ΅Ρ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ Π² ΠΎΠ΄Π½ΠΎ, ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠΌ ΠΊΠΎΡΠΎΡΠΎΠ³ΠΎ ΡΠ²Π»ΡΠ΅ΡΡΡ ΠΌΠ°ΡΡΠΈΠ² ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ² Π΄ΡΡΠ³ΠΈΡ
ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ.
|
|
649
|
/// ΠΠ±ΡΠ΅Π΄ΠΈΠ½ΡΠ΅Ρ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ Π² ΠΎΠ΄Π½ΠΎ, ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠΌ ΠΊΠΎΡΠΎΡΠΎΠ³ΠΎ ΡΠ²Π»ΡΠ΅ΡΡΡ ΠΌΠ°ΡΡΠΈΠ² ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ² Π΄ΡΡΠ³ΠΈΡ
ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ.
|
|
648
|
/// ΠΡΠ»ΠΈ Ρ
ΠΎΡΡΠ±Ρ ΠΎΠ΄Π½ΠΎ ΠΈΠ· ΠΏΠ΅ΡΠ΅Π΄Π°Π½Π½ΡΡ
ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ Π½Π΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ, ΡΠΎ Π½ΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ΅Π½ΠΈΠ΅ ΡΠΎΠΆΠ΅ Π½Π΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ.
|
|
650
|
/// ΠΡΠ»ΠΈ Ρ
ΠΎΡΡΠ±Ρ ΠΎΠ΄Π½ΠΎ ΠΈΠ· ΠΏΠ΅ΡΠ΅Π΄Π°Π½Π½ΡΡ
ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ Π½Π΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ, ΡΠΎ Π½ΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ΅Π½ΠΈΠ΅ ΡΠΎΠΆΠ΅ Π½Π΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ.
|
|
649
|
/// ΠΡΠΈ ΠΎΡΠΌΠ΅Π½Π΅ Π½ΠΎΠ²ΠΎΠ³ΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ, ΠΏΠ΅ΡΠ΅Π΄Π°Π½Π½ΡΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΡΠ°ΠΊΠΆΠ΅ Π±ΡΠ΄ΡΡ ΠΎΡΠΌΠ΅Π½Π΅Π½Ρ, Π΅ΡΠ»ΠΈ Π½ΠΈΠΊΡΠΎ Π±ΠΎΠ»ΡΡΠ΅ Π½Π° Π½ΠΈΡ
Π½Π΅ ΠΏΠΎΠ΄ΠΏΠΈΡΠ°Π½.
|
|
651
|
/// ΠΡΠΈ ΠΎΡΠΌΠ΅Π½Π΅ Π½ΠΎΠ²ΠΎΠ³ΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ, ΠΏΠ΅ΡΠ΅Π΄Π°Π½Π½ΡΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΡΠ°ΠΊΠΆΠ΅ Π±ΡΠ΄ΡΡ ΠΎΡΠΌΠ΅Π½Π΅Π½Ρ, Π΅ΡΠ»ΠΈ Π½ΠΈΠΊΡΠΎ Π±ΠΎΠ»ΡΡΠ΅ Π½Π° Π½ΠΈΡ
Π½Π΅ ΠΏΠΎΠ΄ΠΏΠΈΡΠ°Π½.
|
|
650
|
/// </summary>
|
|
652
|
/// </summary>
|
|
651
|
/// <param name="promises">Π‘ΠΏΠΈΡΠΎΠΊ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ. ΠΡΠ»ΠΈ ΡΠΏΠΈΡΠΎΠΊ ΠΏΡΡΡΠΎΠΉ, ΡΠΎ ΡΠ΅Π·ΡΠ»ΡΡΠΈΡΡΡΡΠ΅Π΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅ΡΡΡ ΡΠΆΠ΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½Π½ΡΠΌ.</param>
|
|
653
|
/// <param name="promises">Π‘ΠΏΠΈΡΠΎΠΊ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ. ΠΡΠ»ΠΈ ΡΠΏΠΈΡΠΎΠΊ ΠΏΡΡΡΠΎΠΉ, ΡΠΎ ΡΠ΅Π·ΡΠ»ΡΡΠΈΡΡΡΡΠ΅Π΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅ΡΡΡ ΡΠΆΠ΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½Π½ΡΠΌ.</param>
|
|
652
|
/// <returns>ΠΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΠΎΠ±ΡΠ΅Π΄ΠΈΠ½ΡΡΡΠ΅Π΅ Π² ΡΠ΅Π±Π΅ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ ΠΏΠ΅ΡΠ΅Π΄Π°Π½Π½ΡΡ
ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ.</returns>
|
|
654
|
/// <returns>ΠΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΠΎΠ±ΡΠ΅Π΄ΠΈΠ½ΡΡΡΠ΅Π΅ Π² ΡΠ΅Π±Π΅ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ ΠΏΠ΅ΡΠ΅Π΄Π°Π½Π½ΡΡ
ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ.</returns>
|
|
653
|
/// <exception cref="ArgumentNullException"><paramref name="promises"/> Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ null</exception>
|
|
655
|
/// <exception cref="ArgumentNullException"><paramref name="promises"/> Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ null</exception>
|
|
654
|
public static IPromise<T[]> CreateComposite(IList<IPromise<T>> promises) {
|
|
656
|
public static IPromise<T[]> CreateComposite(IList<IPromise<T>> promises) {
|
|
655
|
if (promises == null)
|
|
657
|
if (promises == null)
|
|
656
|
throw new ArgumentNullException();
|
|
658
|
throw new ArgumentNullException();
|
|
657
|
|
|
659
|
|
|
658
|
// ΡΠΎΠ·Π΄Π°Π΅ΠΌ Π°ΠΊΠΊΡΠΌΡΠ»ΡΡΠΎΡ Π΄Π»Ρ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ² ΠΈ ΡΠ΅Π·ΡΠ»ΡΡΠΈΡΡΡΡΠ΅Π΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅
|
|
660
|
// ΡΠΎΠ·Π΄Π°Π΅ΠΌ Π°ΠΊΠΊΡΠΌΡΠ»ΡΡΠΎΡ Π΄Π»Ρ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ² ΠΈ ΡΠ΅Π·ΡΠ»ΡΡΠΈΡΡΡΡΠ΅Π΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅
|
|
659
|
var result = new T[promises.Count];
|
|
661
|
var result = new T[promises.Count];
|
|
660
|
var promise = new Promise<T[]>();
|
|
662
|
var promise = new Promise<T[]>();
|
|
661
|
|
|
663
|
|
|
662
|
// special case
|
|
664
|
// special case
|
|
663
|
if (promises.Count == 0) {
|
|
665
|
if (promises.Count == 0) {
|
|
664
|
promise.Resolve(result);
|
|
666
|
promise.Resolve(result);
|
|
665
|
return promise;
|
|
667
|
return promise;
|
|
666
|
}
|
|
668
|
}
|
|
667
|
|
|
669
|
|
|
668
|
int pending = promises.Count;
|
|
670
|
int pending = promises.Count;
|
|
669
|
|
|
671
|
|
|
670
|
for (int i = 0; i < promises.Count; i++) {
|
|
672
|
for (int i = 0; i < promises.Count; i++) {
|
|
671
|
var dest = i;
|
|
673
|
var dest = i;
|
|
672
|
|
|
674
|
|
|
673
|
if (promises[i] != null) {
|
|
675
|
if (promises[i] != null) {
|
|
674
|
promises[i].Then(
|
|
676
|
promises[i].Then(
|
|
675
|
x => {
|
|
677
|
x => {
|
|
676
|
result[dest] = x;
|
|
678
|
result[dest] = x;
|
|
677
|
if (Interlocked.Decrement(ref pending) == 0)
|
|
679
|
if (Interlocked.Decrement(ref pending) == 0)
|
|
678
|
promise.Resolve(result);
|
|
680
|
promise.Resolve(result);
|
|
679
|
},
|
|
681
|
},
|
|
680
|
e => {
|
|
682
|
e => {
|
|
681
|
promise.Reject(e);
|
|
683
|
promise.Reject(e);
|
|
682
|
return default(T);
|
|
684
|
return default(T);
|
|
683
|
}
|
|
685
|
}
|
|
684
|
);
|
|
686
|
);
|
|
685
|
} else {
|
|
687
|
} else {
|
|
686
|
if (Interlocked.Decrement(ref pending) == 0)
|
|
688
|
if (Interlocked.Decrement(ref pending) == 0)
|
|
687
|
promise.Resolve(result);
|
|
689
|
promise.Resolve(result);
|
|
688
|
}
|
|
690
|
}
|
|
689
|
}
|
|
691
|
}
|
|
690
|
|
|
692
|
|
|
691
|
promise.Cancelled(
|
|
693
|
promise.Cancelled(
|
|
692
|
() => {
|
|
694
|
() => {
|
|
693
|
foreach (var d in promises)
|
|
695
|
foreach (var d in promises)
|
|
694
|
if (d != null && d.IsExclusive)
|
|
696
|
if (d != null && d.IsExclusive)
|
|
695
|
d.Cancel();
|
|
697
|
d.Cancel();
|
|
696
|
}
|
|
698
|
}
|
|
697
|
);
|
|
699
|
);
|
|
698
|
|
|
700
|
|
|
699
|
return promise;
|
|
701
|
return promise;
|
|
700
|
}
|
|
702
|
}
|
|
701
|
|
|
703
|
|
|
702
|
/// <summary>
|
|
704
|
/// <summary>
|
|
703
|
/// ΠΠ±ΡΠ΅Π΄ΠΈΠ½ΡΠ΅Ρ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ Π² ΠΎΠ΄Π½ΠΎ. Π Π΅Π·ΡΠ»ΡΡΠΈΡΡΡΡΠ΅Π΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ ΠΏΡΠΈ
|
|
705
|
/// ΠΠ±ΡΠ΅Π΄ΠΈΠ½ΡΠ΅Ρ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ Π² ΠΎΠ΄Π½ΠΎ. Π Π΅Π·ΡΠ»ΡΡΠΈΡΡΡΡΠ΅Π΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ ΠΏΡΠΈ
|
|
704
|
/// Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ Π²ΡΠ΅Ρ
ΡΠΊΠ°Π·Π°Π½Π½ΡΡ
ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ. ΠΡΠΈ ΡΡΠΎΠΌ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅ΠΌΡΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΡ ΠΏΠ΅ΡΠ²ΠΈΡΠ½ΡΡ
ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ
|
|
706
|
/// Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ Π²ΡΠ΅Ρ
ΡΠΊΠ°Π·Π°Π½Π½ΡΡ
ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ. ΠΡΠΈ ΡΡΠΎΠΌ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅ΠΌΡΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΡ ΠΏΠ΅ΡΠ²ΠΈΡΠ½ΡΡ
ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ
|
|
705
|
/// ΠΈΠ³Π½ΠΎΡΠΈΡΡΡΡΡΡ.
|
|
707
|
/// ΠΈΠ³Π½ΠΎΡΠΈΡΡΡΡΡΡ.
|
|
706
|
/// </summary>
|
|
708
|
/// </summary>
|
|
707
|
/// <param name="promises">ΠΠΎΠ»Π»Π΅ΠΊΡΠΈΡ ΠΏΠ΅ΡΠ²ΠΈΡΠ½ΡΡ
ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ, ΠΊΠΎΡΠΎΡΡΠ΅ Π±ΡΠ΄ΡΡ ΠΎΠ±ΡΠ΅Π΄Π΅Π½Π΅Π½Ρ Π² ΠΎΠ΄Π½ΠΎ.</param>
|
|
709
|
/// <param name="promises">ΠΠΎΠ»Π»Π΅ΠΊΡΠΈΡ ΠΏΠ΅ΡΠ²ΠΈΡΠ½ΡΡ
ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ, ΠΊΠΎΡΠΎΡΡΠ΅ Π±ΡΠ΄ΡΡ ΠΎΠ±ΡΠ΅Π΄Π΅Π½Π΅Π½Ρ Π² ΠΎΠ΄Π½ΠΎ.</param>
|
|
708
|
/// <returns>ΠΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΠΎΠ±ΡΠ΅Π΄ΠΈΠ½ΡΡΡΠ΅Π΅ Π² ΡΠ΅Π±Π΅ ΠΏΠ΅ΡΠ΅Π΄Π°Π½Π½ΡΠ΅.</returns>
|
|
710
|
/// <returns>ΠΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΠΎΠ±ΡΠ΅Π΄ΠΈΠ½ΡΡΡΠ΅Π΅ Π² ΡΠ΅Π±Π΅ ΠΏΠ΅ΡΠ΅Π΄Π°Π½Π½ΡΠ΅.</returns>
|
|
709
|
/// <remarks>
|
|
711
|
/// <remarks>
|
|
710
|
/// ΠΡΠ»ΠΈ Π² ΠΊΠΎΠ»Π»Π΅ΠΊΡΠΈΠΈ Π²ΡΡΡΠ΅ΡΠ°ΡΡΡΡ <c>null</c>, ΡΠΎ ΠΎΠ½ΠΈ Π²ΠΎΡΠΏΡΠΈΠ½ΠΈΠΌΠ°ΡΡΡΡ ΠΊΠ°ΠΊ Π²ΡΠΏΠΎΠ»Π½Π΅Π½Π½ΡΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ.
|
|
712
|
/// ΠΡΠ»ΠΈ Π² ΠΊΠΎΠ»Π»Π΅ΠΊΡΠΈΠΈ Π²ΡΡΡΠ΅ΡΠ°ΡΡΡΡ <c>null</c>, ΡΠΎ ΠΎΠ½ΠΈ Π²ΠΎΡΠΏΡΠΈΠ½ΠΈΠΌΠ°ΡΡΡΡ ΠΊΠ°ΠΊ Π²ΡΠΏΠΎΠ»Π½Π΅Π½Π½ΡΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ.
|
|
711
|
/// </remarks>
|
|
713
|
/// </remarks>
|
|
712
|
public static IPromise CreateComposite(ICollection<IPromise> promises) {
|
|
714
|
public static IPromise CreateComposite(ICollection<IPromise> promises) {
|
|
713
|
if (promises == null)
|
|
715
|
if (promises == null)
|
|
714
|
throw new ArgumentNullException();
|
|
716
|
throw new ArgumentNullException();
|
|
715
|
if (promises.Count == 0)
|
|
717
|
if (promises.Count == 0)
|
|
716
|
return Promise<object>.ResultToPromise(null);
|
|
718
|
return Promise<object>.ResultToPromise(null);
|
|
717
|
|
|
719
|
|
|
718
|
int countdown = promises.Count;
|
|
720
|
int countdown = promises.Count;
|
|
719
|
|
|
721
|
|
|
720
|
var result = new Promise<object>();
|
|
722
|
var result = new Promise<object>();
|
|
721
|
|
|
723
|
|
|
722
|
foreach (var d in promises) {
|
|
724
|
foreach (var d in promises) {
|
|
723
|
if (d == null) {
|
|
725
|
if (d == null) {
|
|
724
|
if (Interlocked.Decrement(ref countdown) == 0)
|
|
726
|
if (Interlocked.Decrement(ref countdown) == 0)
|
|
725
|
result.Resolve(null);
|
|
727
|
result.Resolve(null);
|
|
726
|
} else {
|
|
728
|
} else {
|
|
727
|
d.Then(() => {
|
|
729
|
d.Then(() => {
|
|
728
|
if (Interlocked.Decrement(ref countdown) == 0)
|
|
730
|
if (Interlocked.Decrement(ref countdown) == 0)
|
|
729
|
result.Resolve(null);
|
|
731
|
result.Resolve(null);
|
|
730
|
});
|
|
732
|
});
|
|
731
|
}
|
|
733
|
}
|
|
732
|
}
|
|
734
|
}
|
|
733
|
|
|
735
|
|
|
734
|
result.Cancelled(() => {
|
|
736
|
result.Cancelled(() => {
|
|
735
|
foreach (var d in promises)
|
|
737
|
foreach (var d in promises)
|
|
736
|
if (d != null && d.IsExclusive)
|
|
738
|
if (d != null && d.IsExclusive)
|
|
737
|
d.Cancel();
|
|
739
|
d.Cancel();
|
|
738
|
});
|
|
740
|
});
|
|
739
|
|
|
741
|
|
|
740
|
return result;
|
|
742
|
return result;
|
|
741
|
}
|
|
743
|
}
|
|
742
|
|
|
744
|
|
|
743
|
public static Promise<T> ResultToPromise(T result) {
|
|
745
|
public static Promise<T> ResultToPromise(T result) {
|
|
744
|
var p = new Promise<T>();
|
|
746
|
var p = new Promise<T>();
|
|
745
|
p.Resolve(result);
|
|
747
|
p.Resolve(result);
|
|
746
|
return p;
|
|
748
|
return p;
|
|
747
|
}
|
|
749
|
}
|
|
748
|
|
|
750
|
|
|
749
|
public static Promise<T> ExceptionToPromise(Exception error) {
|
|
751
|
public static Promise<T> ExceptionToPromise(Exception error) {
|
|
750
|
if (error == null)
|
|
752
|
if (error == null)
|
|
751
|
throw new ArgumentNullException();
|
|
753
|
throw new ArgumentNullException();
|
|
752
|
|
|
754
|
|
|
753
|
var p = new Promise<T>();
|
|
755
|
var p = new Promise<T>();
|
|
754
|
p.Reject(error);
|
|
756
|
p.Reject(error);
|
|
755
|
return p;
|
|
757
|
return p;
|
|
756
|
}
|
|
758
|
}
|
|
757
|
|
|
759
|
|
|
758
|
#region IPromiseBase explicit implementation
|
|
760
|
#region IPromiseBase explicit implementation
|
|
759
|
|
|
761
|
|
|
760
|
IPromise IPromise.Then(Action success, ErrorHandler error, Action cancel) {
|
|
762
|
IPromise IPromise.Then(Action success, ErrorHandler error, Action cancel) {
|
|
761
|
return Then(
|
|
763
|
return Then(
|
|
762
|
x => success(),
|
|
764
|
success != null ? new ResultHandler<T>(x => success()) : null,
|
|
763
|
e => {
|
|
765
|
error != null ? new ErrorHandler<T>(e => {
|
|
764
|
error(e);
|
|
766
|
error(e);
|
|
765
|
return default(T);
|
|
767
|
return default(T);
|
|
766
|
},
|
|
768
|
}) : null,
|
|
767
|
cancel
|
|
769
|
cancel
|
|
768
|
);
|
|
770
|
);
|
|
769
|
}
|
|
771
|
}
|
|
770
|
|
|
772
|
|
|
771
|
IPromise IPromise.Then(Action success, ErrorHandler error) {
|
|
773
|
IPromise IPromise.Then(Action success, ErrorHandler error) {
|
|
772
|
return Then(
|
|
774
|
return Then(
|
|
773
|
x => success(),
|
|
775
|
success != null ? new ResultHandler<T>(x => success()) : null,
|
|
774
|
e => {
|
|
776
|
error != null ? new ErrorHandler<T>(e => {
|
|
775
|
error(e);
|
|
777
|
error(e);
|
|
776
|
return default(T);
|
|
778
|
return default(T);
|
|
777
|
}
|
|
779
|
}) : null
|
|
778
|
);
|
|
780
|
);
|
|
779
|
}
|
|
781
|
}
|
|
780
|
|
|
782
|
|
|
781
|
IPromise IPromise.Then(Action success) {
|
|
783
|
IPromise IPromise.Then(Action success) {
|
|
|
|
|
784
|
Safe.ArgumentNotNull(success, "success");
|
|
782
|
return Then(x => success());
|
|
785
|
return Then(x => success());
|
|
783
|
}
|
|
786
|
}
|
|
784
|
|
|
787
|
|
|
|
|
|
788
|
IPromise IPromise.Chain(Func<IPromise> chained, ErrorHandler<IPromise> error, Action cancel) {
|
|
|
|
|
789
|
return ChainNoResult(chained, error, cancel);
|
|
|
|
|
790
|
}
|
|
|
|
|
791
|
|
|
|
|
|
792
|
IPromise ChainNoResult(Func<IPromise> chained, ErrorHandler<IPromise> error, Action cancel) {
|
|
|
|
|
793
|
Safe.ArgumentNotNull(chained, "chained");
|
|
|
|
|
794
|
|
|
|
|
|
795
|
var medium = new Promise<object>(this, true);
|
|
|
|
|
796
|
|
|
|
|
|
797
|
ResultHandler<T> resultHandler = delegate(T result) {
|
|
|
|
|
798
|
if (medium.IsCancelled)
|
|
|
|
|
799
|
return;
|
|
|
|
|
800
|
|
|
|
|
|
801
|
var promise = chained();
|
|
|
|
|
802
|
|
|
|
|
|
803
|
promise.Last(
|
|
|
|
|
804
|
medium.Resolve,
|
|
|
|
|
805
|
medium.Reject,
|
|
|
|
|
806
|
() => medium.Reject(new OperationCanceledException()) // Π²Π½Π΅ΡΠ½ΡΡ ΠΎΡΠΌΠ΅Π½Π° ΡΠ²ΡΠ·Π°Π½Π½ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΡΠ°ΡΡΠΌΠ°ΡΡΠΈΠ²Π°Π΅ΡΡΡ ΠΊΠ°ΠΊ ΠΎΡΠΈΠ±ΠΊΠ°
|
|
|
|
|
807
|
);
|
|
|
|
|
808
|
|
|
|
|
|
809
|
// notify chained operation that it's not needed anymore
|
|
|
|
|
810
|
// ΠΏΠΎΡΡΠ΄ΠΎΠΊ Π²ΡΠ·ΠΎΠ²Π° Then, Cancelled Π²Π°ΠΆΠ΅Π½, ΠΏΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΠΎΡ ΡΡΠΎΠ³ΠΎ
|
|
|
|
|
811
|
// Π·Π°Π²ΠΈΡΠΈΡ IsExclusive
|
|
|
|
|
812
|
medium.Cancelled(() => {
|
|
|
|
|
813
|
if (promise.IsExclusive)
|
|
|
|
|
814
|
promise.Cancel();
|
|
|
|
|
815
|
});
|
|
|
|
|
816
|
};
|
|
|
|
|
817
|
|
|
|
|
|
818
|
ErrorHandler<T> errorHandler;
|
|
|
|
|
819
|
|
|
|
|
|
820
|
if (error != null)
|
|
|
|
|
821
|
errorHandler = delegate(Exception e) {
|
|
|
|
|
822
|
try {
|
|
|
|
|
823
|
var promise = error(e);
|
|
|
|
|
824
|
|
|
|
|
|
825
|
promise.Last(
|
|
|
|
|
826
|
medium.Resolve,
|
|
|
|
|
827
|
medium.Reject,
|
|
|
|
|
828
|
() => medium.Reject(new OperationCanceledException()) // Π²Π½Π΅ΡΠ½ΡΡ ΠΎΡΠΌΠ΅Π½Π° ΡΠ²ΡΠ·Π°Π½Π½ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΡΠ°ΡΡΠΌΠ°ΡΡΠΈΠ²Π°Π΅ΡΡΡ ΠΊΠ°ΠΊ ΠΎΡΠΈΠ±ΠΊΠ°
|
|
|
|
|
829
|
);
|
|
|
|
|
830
|
|
|
|
|
|
831
|
// notify chained operation that it's not needed anymore
|
|
|
|
|
832
|
// ΠΏΠΎΡΡΠ΄ΠΎΠΊ Π²ΡΠ·ΠΎΠ²Π° Then, Cancelled Π²Π°ΠΆΠ΅Π½, ΠΏΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΠΎΡ ΡΡΠΎΠ³ΠΎ
|
|
|
|
|
833
|
// Π·Π°Π²ΠΈΡΠΈΡ IsExclusive
|
|
|
|
|
834
|
medium.Cancelled(() => {
|
|
|
|
|
835
|
if (promise.IsExclusive)
|
|
|
|
|
836
|
promise.Cancel();
|
|
|
|
|
837
|
});
|
|
|
|
|
838
|
} catch (Exception e2) {
|
|
|
|
|
839
|
medium.Reject(e2);
|
|
|
|
|
840
|
}
|
|
|
|
|
841
|
return default(T);
|
|
|
|
|
842
|
};
|
|
|
|
|
843
|
else
|
|
|
|
|
844
|
errorHandler = err => {
|
|
|
|
|
845
|
medium.Reject(err);
|
|
|
|
|
846
|
return default(T);
|
|
|
|
|
847
|
};
|
|
|
|
|
848
|
|
|
|
|
|
849
|
|
|
|
|
|
850
|
Action cancelHandler;
|
|
|
|
|
851
|
if (cancel != null)
|
|
|
|
|
852
|
cancelHandler = () => {
|
|
|
|
|
853
|
if (cancel != null)
|
|
|
|
|
854
|
cancel();
|
|
|
|
|
855
|
medium.Cancel();
|
|
|
|
|
856
|
};
|
|
|
|
|
857
|
else
|
|
|
|
|
858
|
cancelHandler = medium.Cancel;
|
|
|
|
|
859
|
|
|
|
|
|
860
|
AddHandler(
|
|
|
|
|
861
|
resultHandler,
|
|
|
|
|
862
|
errorHandler,
|
|
|
|
|
863
|
cancelHandler,
|
|
|
|
|
864
|
null
|
|
|
|
|
865
|
);
|
|
|
|
|
866
|
|
|
|
|
|
867
|
return medium;
|
|
|
|
|
868
|
}
|
|
|
|
|
869
|
IPromise IPromise.Chain(Func<IPromise> chained, ErrorHandler<IPromise> error) {
|
|
|
|
|
870
|
return ChainNoResult(chained, error, null);
|
|
|
|
|
871
|
}
|
|
|
|
|
872
|
IPromise IPromise.Chain(Func<IPromise> chained) {
|
|
|
|
|
873
|
return ChainNoResult(chained, null, null);
|
|
|
|
|
874
|
}
|
|
|
|
|
875
|
|
|
|
|
|
876
|
|
|
785
|
void IPromise.Last(Action success, ErrorHandler error, Action cancel) {
|
|
877
|
void IPromise.Last(Action success, ErrorHandler error, Action cancel) {
|
|
786
|
Last(x => success(), error, cancel);
|
|
878
|
Last(x => success(), error, cancel);
|
|
787
|
}
|
|
879
|
}
|
|
788
|
|
|
880
|
|
|
789
|
void IPromise.Last(Action success, ErrorHandler error) {
|
|
881
|
void IPromise.Last(Action success, ErrorHandler error) {
|
|
790
|
Last(x => success(), error, null);
|
|
882
|
Last(x => success(), error, null);
|
|
791
|
}
|
|
883
|
}
|
|
792
|
|
|
884
|
|
|
793
|
void IPromise.Last(Action success) {
|
|
885
|
void IPromise.Last(Action success) {
|
|
794
|
Last(x => success(), null, null);
|
|
886
|
Last(x => success(), null, null);
|
|
795
|
}
|
|
887
|
}
|
|
796
|
|
|
888
|
|
|
797
|
IPromise IPromise.Error(ErrorHandler error) {
|
|
889
|
IPromise IPromise.Error(ErrorHandler error) {
|
|
798
|
return Error(error);
|
|
890
|
return Error(error);
|
|
799
|
}
|
|
891
|
}
|
|
800
|
|
|
892
|
|
|
801
|
IPromise IPromise.Anyway(Action handler) {
|
|
893
|
IPromise IPromise.Anyway(Action handler) {
|
|
802
|
return Anyway(handler);
|
|
894
|
return Anyway(handler);
|
|
803
|
}
|
|
895
|
}
|
|
804
|
|
|
896
|
|
|
805
|
IPromise IPromise.Cancelled(Action handler) {
|
|
897
|
IPromise IPromise.Cancelled(Action handler) {
|
|
806
|
return Cancelled(handler);
|
|
898
|
return Cancelled(handler);
|
|
807
|
}
|
|
899
|
}
|
|
808
|
|
|
900
|
|
|
809
|
void IPromise.Join() {
|
|
901
|
void IPromise.Join() {
|
|
810
|
Join();
|
|
902
|
Join();
|
|
811
|
}
|
|
903
|
}
|
|
812
|
|
|
904
|
|
|
813
|
void IPromise.Join(int timeout) {
|
|
905
|
void IPromise.Join(int timeout) {
|
|
814
|
Join(timeout);
|
|
906
|
Join(timeout);
|
|
815
|
}
|
|
907
|
}
|
|
816
|
|
|
908
|
|
|
817
|
#endregion
|
|
909
|
#endregion
|
|
818
|
|
|
910
|
|
|
819
|
|
|
911
|
|
|
820
|
|
|
912
|
|
|
821
|
}
|
|
913
|
}
|
|
822
|
}
|
|
914
|
}
|