##// END OF EJS Templates
major refactoring, added tasks support
cin -
r75:4439140706d0 v2
parent child
Show More
@@ -31,6 +31,23
31 31 <ErrorReport>prompt</ErrorReport>
32 32 <WarningLevel>4</WarningLevel>
33 33 </PropertyGroup>
34 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug 4.5|AnyCPU' ">
35 <DebugSymbols>true</DebugSymbols>
36 <DebugType>full</DebugType>
37 <Optimize>false</Optimize>
38 <OutputPath>bin\Debug\</OutputPath>
39 <DefineConstants>DEBUG;TRACE</DefineConstants>
40 <ErrorReport>prompt</ErrorReport>
41 <WarningLevel>4</WarningLevel>
42 </PropertyGroup>
43 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release 4.5|AnyCPU' ">
44 <DebugType>pdbonly</DebugType>
45 <Optimize>true</Optimize>
46 <OutputPath>bin\Release\</OutputPath>
47 <DefineConstants>TRACE</DefineConstants>
48 <ErrorReport>prompt</ErrorReport>
49 <WarningLevel>4</WarningLevel>
50 </PropertyGroup>
34 51 <ItemGroup>
35 52 <Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
36 53 <Reference Include="System" />
@@ -30,6 +30,23
30 30 <ErrorReport>prompt</ErrorReport>
31 31 <WarningLevel>4</WarningLevel>
32 32 </PropertyGroup>
33 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug 4.5|AnyCPU' ">
34 <DebugSymbols>true</DebugSymbols>
35 <DebugType>full</DebugType>
36 <Optimize>false</Optimize>
37 <OutputPath>bin\Debug\</OutputPath>
38 <DefineConstants>DEBUG;TRACE</DefineConstants>
39 <ErrorReport>prompt</ErrorReport>
40 <WarningLevel>4</WarningLevel>
41 </PropertyGroup>
42 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release 4.5|AnyCPU' ">
43 <DebugType>pdbonly</DebugType>
44 <Optimize>true</Optimize>
45 <OutputPath>bin\Release\</OutputPath>
46 <DefineConstants>TRACE</DefineConstants>
47 <ErrorReport>prompt</ErrorReport>
48 <WarningLevel>4</WarningLevel>
49 </PropertyGroup>
33 50 <ItemGroup>
34 51 <Reference Include="System" />
35 52 <Reference Include="System.Core" />
@@ -31,6 +31,23
31 31 <ErrorReport>prompt</ErrorReport>
32 32 <WarningLevel>4</WarningLevel>
33 33 </PropertyGroup>
34 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug 4.5|AnyCPU' ">
35 <DebugSymbols>true</DebugSymbols>
36 <DebugType>full</DebugType>
37 <Optimize>false</Optimize>
38 <OutputPath>bin\Debug\</OutputPath>
39 <DefineConstants>DEBUG;TRACE</DefineConstants>
40 <ErrorReport>prompt</ErrorReport>
41 <WarningLevel>4</WarningLevel>
42 </PropertyGroup>
43 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release 4.5|AnyCPU' ">
44 <DebugType>pdbonly</DebugType>
45 <Optimize>true</Optimize>
46 <OutputPath>bin\Release\</OutputPath>
47 <DefineConstants>TRACE</DefineConstants>
48 <ErrorReport>prompt</ErrorReport>
49 <WarningLevel>4</WarningLevel>
50 </PropertyGroup>
34 51 <ItemGroup>
35 52 <Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
36 53 <Reference Include="System" />
@@ -20,22 +20,40 Global
20 20 GlobalSection(SolutionConfigurationPlatforms) = preSolution
21 21 Debug|Any CPU = Debug|Any CPU
22 22 Release|Any CPU = Release|Any CPU
23 Debug 4.5|Any CPU = Debug 4.5|Any CPU
24 Release 4.5|Any CPU = Release 4.5|Any CPU
23 25 EndGlobalSection
24 26 GlobalSection(ProjectConfigurationPlatforms) = postSolution
27 {06E706F8-6881-43EB-927E-FFC503AF6ABC}.Debug 4.5|Any CPU.ActiveCfg = Debug 4.5|Any CPU
28 {06E706F8-6881-43EB-927E-FFC503AF6ABC}.Debug 4.5|Any CPU.Build.0 = Debug 4.5|Any CPU
25 29 {06E706F8-6881-43EB-927E-FFC503AF6ABC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
26 30 {06E706F8-6881-43EB-927E-FFC503AF6ABC}.Debug|Any CPU.Build.0 = Debug|Any CPU
31 {06E706F8-6881-43EB-927E-FFC503AF6ABC}.Release 4.5|Any CPU.ActiveCfg = Release 4.5|Any CPU
32 {06E706F8-6881-43EB-927E-FFC503AF6ABC}.Release 4.5|Any CPU.Build.0 = Release 4.5|Any CPU
27 33 {06E706F8-6881-43EB-927E-FFC503AF6ABC}.Release|Any CPU.ActiveCfg = Release|Any CPU
28 34 {06E706F8-6881-43EB-927E-FFC503AF6ABC}.Release|Any CPU.Build.0 = Release|Any CPU
35 {2F31E405-E267-4195-A05D-574093C21209}.Debug 4.5|Any CPU.ActiveCfg = Debug 4.5|Any CPU
36 {2F31E405-E267-4195-A05D-574093C21209}.Debug 4.5|Any CPU.Build.0 = Debug 4.5|Any CPU
29 37 {2F31E405-E267-4195-A05D-574093C21209}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
30 38 {2F31E405-E267-4195-A05D-574093C21209}.Debug|Any CPU.Build.0 = Debug|Any CPU
39 {2F31E405-E267-4195-A05D-574093C21209}.Release 4.5|Any CPU.ActiveCfg = Release 4.5|Any CPU
40 {2F31E405-E267-4195-A05D-574093C21209}.Release 4.5|Any CPU.Build.0 = Release 4.5|Any CPU
31 41 {2F31E405-E267-4195-A05D-574093C21209}.Release|Any CPU.ActiveCfg = Release|Any CPU
32 42 {2F31E405-E267-4195-A05D-574093C21209}.Release|Any CPU.Build.0 = Release|Any CPU
43 {63F92C0C-61BF-48C0-A377-8D67C3C661D0}.Debug 4.5|Any CPU.ActiveCfg = Debug 4.5|Any CPU
44 {63F92C0C-61BF-48C0-A377-8D67C3C661D0}.Debug 4.5|Any CPU.Build.0 = Debug 4.5|Any CPU
33 45 {63F92C0C-61BF-48C0-A377-8D67C3C661D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
34 46 {63F92C0C-61BF-48C0-A377-8D67C3C661D0}.Debug|Any CPU.Build.0 = Debug|Any CPU
47 {63F92C0C-61BF-48C0-A377-8D67C3C661D0}.Release 4.5|Any CPU.ActiveCfg = Release 4.5|Any CPU
48 {63F92C0C-61BF-48C0-A377-8D67C3C661D0}.Release 4.5|Any CPU.Build.0 = Release 4.5|Any CPU
35 49 {63F92C0C-61BF-48C0-A377-8D67C3C661D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
36 50 {63F92C0C-61BF-48C0-A377-8D67C3C661D0}.Release|Any CPU.Build.0 = Release|Any CPU
51 {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Debug 4.5|Any CPU.ActiveCfg = Debug 4.5|Any CPU
52 {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Debug 4.5|Any CPU.Build.0 = Debug 4.5|Any CPU
37 53 {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
38 54 {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Debug|Any CPU.Build.0 = Debug|Any CPU
55 {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Release 4.5|Any CPU.ActiveCfg = Release 4.5|Any CPU
56 {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Release 4.5|Any CPU.Build.0 = Release 4.5|Any CPU
39 57 {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Release|Any CPU.ActiveCfg = Release|Any CPU
40 58 {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Release|Any CPU.Build.0 = Release|Any CPU
41 59 EndGlobalSection
@@ -27,16 +27,26 namespace Implab {
27 27 /// </summary>
28 28 bool IsCancelled { get; }
29 29
30 IPromise Then(Action success,ErrorHandler error);
30 IPromise Then(Action success, ErrorHandler error, Action cancel);
31 IPromise Then(Action success, ErrorHandler error);
31 32 IPromise Then(Action success);
33
34 /// <summary>
35 /// ДобавляСт послСднй ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ Π² Ρ†Π΅ΠΏΠΎΡ‡ΠΊΡƒ ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠΉ, Π½Π΅ создаСт ΠΏΡ€ΠΎΠΌΠ΅ΠΆΡƒΡ‚ΠΎΡ‡Π½Ρ‹Ρ… ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠΉ.
36 /// </summary>
37 /// <param name="success">Success.</param>
38 /// <param name="error">Error.</param>
39 /// <param name="cancel">Cancel.</param>
40 void Last(Action success, ErrorHandler error, Action cancel);
41 void Last(Action success, ErrorHandler error);
42 void Last(Action success);
43
32 44 IPromise Error(ErrorHandler error);
33 45 /// <summary>
34 46 /// ΠžΠ±Ρ€Π°Π±Π°Ρ‚Ρ‹Π²Π°Π΅Ρ‚ Π»ΠΈΠ±ΠΎ ΠΎΡˆΠΈΠ±ΠΊΡƒ, Π»ΠΈΠ±ΠΎ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚. Π‘ΠΎΠ±Ρ‹Ρ‚ΠΈΠ΅ ΠΎΡ‚ΠΌΠ΅Π½Ρ‹ Π½Π΅ обрабатываСтся.
35 47 /// </summary>
36 48 /// <param name="handler">ΠžΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ.</param>
37 49 /// <remarks>ПослС ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚ΠΊΠ΅ ошибки, ΠΎΠ½Π° пСрСдаСтся дальшС.</remarks>
38 IPromise Anyway(Action handler);
39
40 50 /// <summary>
41 51 /// ΠžΠ±Ρ€Π°Π±Π°Ρ‚Ρ‹Π²Π°Π΅Ρ‚ Π»ΠΈΠ±ΠΎ ΠΎΡˆΠΈΠ±ΠΊΡƒ, Π»ΠΈΠ±ΠΎ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚, Π»ΠΈΠ±ΠΎ ΠΎΡ‚ΠΌΠ΅Π½Ρƒ обСщания.
42 52 /// </summary>
@@ -3,27 +3,35 using System.Collections.Generic;
3 3 using System.Linq;
4 4 using System.Text;
5 5
6 namespace Implab
7 {
8 public interface IPromise<T>: IPromise
9 {
6 namespace Implab {
7 public interface IPromise<T>: IPromise {
10 8
11 9 new T Join();
10
12 11 new T Join(int timeout);
13 12
13 IPromise<T> Then(ResultHandler<T> success, ErrorHandler<T> error, Action cancel);
14
14 15 IPromise<T> Then(ResultHandler<T> success, ErrorHandler<T> error);
16
15 17 IPromise<T> Then(ResultHandler<T> success);
18
19 void Last(ResultHandler<T> success, ErrorHandler error, Action cancel);
20 void Last(ResultHandler<T> success, ErrorHandler error);
21 void Last(ResultHandler<T> success);
22
16 23 IPromise<T> Error(ErrorHandler<T> error);
17 24
18 IPromise<T2> Map<T2>(ResultMapper<T,T2> mapper, ErrorHandler<T> error);
19 IPromise<T2> Map<T2>(ResultMapper<T, T2> mapper);
25 IPromise<T2> Then<T2>(ResultMapper<T,T2> mapper, ErrorHandler<T> error);
26
27 IPromise<T2> Then<T2>(ResultMapper<T,T2> mapper);
20 28
21 IPromise<T2> Chain<T2>(ChainedOperation<T, T2> chained, ErrorHandler<T> error);
22 IPromise<T2> Chain<T2>(ChainedOperation<T, T2> chained);
29 IPromise<T2> Then<T2>(ChainedOperation<T, T2> chained, ErrorHandler<T> error);
30
31 IPromise<T2> Then<T2>(ChainedOperation<T, T2> chained);
23 32
24 33 new IPromise<T> Cancelled(Action handler);
34
25 35 new IPromise<T> Finally(Action handler);
26 new IPromise<T> Anyway(Action handler);
27
28 36 }
29 37 }
@@ -29,6 +29,25
29 29 <WarningLevel>4</WarningLevel>
30 30 <ConsolePause>false</ConsolePause>
31 31 </PropertyGroup>
32 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug 4.5|AnyCPU' ">
33 <DebugSymbols>true</DebugSymbols>
34 <DebugType>full</DebugType>
35 <Optimize>false</Optimize>
36 <OutputPath>bin\Debug</OutputPath>
37 <DefineConstants>TRACE;DEBUG;NET_4_5</DefineConstants>
38 <ErrorReport>prompt</ErrorReport>
39 <WarningLevel>4</WarningLevel>
40 <RunCodeAnalysis>true</RunCodeAnalysis>
41 <ConsolePause>false</ConsolePause>
42 </PropertyGroup>
43 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release 4.5|AnyCPU' ">
44 <Optimize>true</Optimize>
45 <OutputPath>bin\Release</OutputPath>
46 <ErrorReport>prompt</ErrorReport>
47 <WarningLevel>4</WarningLevel>
48 <ConsolePause>false</ConsolePause>
49 <DefineConstants>NET_4_5</DefineConstants>
50 </PropertyGroup>
32 51 <ItemGroup>
33 52 <Reference Include="System" />
34 53 <Reference Include="System.Xml" />
@@ -29,8 +29,7 namespace Implab.Parallels {
29 29 m_pending = source.Length;
30 30 m_action = action;
31 31
32 m_promise.Anyway(() => Dispose());
33 m_promise.Cancelled(() => Dispose());
32 m_promise.Finally(Dispose);
34 33
35 34 InitPool();
36 35 }
@@ -48,7 +47,7 namespace Implab.Parallels {
48 47
49 48 protected override bool TryDequeue(out int unit) {
50 49 unit = Interlocked.Increment(ref m_next) - 1;
51 return unit >= m_source.Length ? false : true;
50 return unit < m_source.Length;
52 51 }
53 52
54 53 protected override void InvokeUnit(int unit) {
@@ -86,8 +85,7 namespace Implab.Parallels {
86 85 m_transform = transform;
87 86 m_traceContext = TraceContext.Snapshot();
88 87
89 m_promise.Anyway(() => Dispose());
90 m_promise.Cancelled(() => Dispose());
88 m_promise.Finally(Dispose);
91 89
92 90 InitPool();
93 91 }
@@ -157,16 +155,17 namespace Implab.Parallels {
157 155
158 156 var semaphore = new Semaphore(threads, threads);
159 157
158 // Analysis disable AccessToDisposedClosure
160 159 AsyncPool.InvokeNewThread(() => {
161 160 for (int i = 0; i < source.Length; i++) {
162 161 if(promise.IsResolved)
163 162 break; // stop processing in case of error or cancellation
164 163 var idx = i;
164
165 165 semaphore.WaitOne();
166 166 try {
167 167 var p1 = transform(source[i]);
168 p1.Anyway(() => semaphore.Release());
169 p1.Cancelled(() => semaphore.Release());
168 p1.Finally(() => semaphore.Release());
170 169 p1.Then(
171 170 x => {
172 171 res[idx] = x;
@@ -187,7 +186,7 namespace Implab.Parallels {
187 186 return 0;
188 187 });
189 188
190 return promise.Anyway(() => semaphore.Dispose());
189 return promise.Finally(semaphore.Dispose);
191 190 }
192 191
193 192 }
@@ -225,6 +225,18 namespace Implab {
225 225 Cancel();
226 226 }
227 227
228
229 public IPromise<T> Then(ResultHandler<T> success, ErrorHandler<T> error, Action cancel) {
230 if (success == null && error == null && cancel == null)
231 return this;
232
233 var medium = new Promise<T>(this, true);
234
235 AddHandler(success, error, cancel, medium);
236
237 return medium;
238 }
239
228 240 /// <summary>
229 241 /// Adds new handlers to this promise.
230 242 /// </summary>
@@ -243,6 +255,17 namespace Implab {
243 255 return medium;
244 256 }
245 257
258 public IPromise Then(Action success, ErrorHandler error, Action cancel) {
259 return Then(
260 x => success(),
261 e => {
262 error(e);
263 return default(T);
264 },
265 cancel
266 );
267 }
268
246 269 public IPromise Then(Action success, ErrorHandler error) {
247 270 return Then(
248 271 x => success(),
@@ -269,6 +292,39 namespace Implab {
269 292 return medium;
270 293 }
271 294
295 public void Last(ResultHandler<T> success, ErrorHandler error, Action cancel) {
296 if (success == null && error == null && cancel == null)
297 return;
298
299 ErrorHandler<T> errorHandler = null;
300 if (error != null)
301 errorHandler = err => {
302 error(err);
303 return default(T);
304 };
305 AddHandler(success, errorHandler, cancel, null);
306 }
307
308 public void Last(ResultHandler<T> success, ErrorHandler error) {
309 Last(success, error, null);
310 }
311
312 public void Last(ResultHandler<T> success) {
313 Last(success, null, null);
314 }
315
316 public void Last(Action success,ErrorHandler error, Action cancel) {
317 Last(x => success(), error, cancel);
318 }
319
320 public void Last(Action success,ErrorHandler error) {
321 Last(x => success(), error, null);
322 }
323
324 public void Last(Action success) {
325 Last(x => success(), null, null);
326 }
327
272 328 public IPromise Error(ErrorHandler error) {
273 329 if (error == null)
274 330 return this;
@@ -307,25 +363,6 namespace Implab {
307 363 return medium;
308 364 }
309 365
310 public IPromise<T> Anyway(Action handler) {
311 if (handler == null)
312 return this;
313
314 var medium = new Promise<T>(this, true);
315
316 AddHandler(
317 x => handler(),
318 e => {
319 handler();
320 throw new TransientPromiseException(e);
321 },
322 null,
323 medium
324 );
325
326 return medium;
327 }
328
329 366 /// <summary>
330 367 /// ΠŸΠΎΠ·Π²ΠΎΠ»ΡΠ΅Ρ‚ ΠΏΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΠΎΠ²Π°Ρ‚ΡŒ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ выполСния ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΈ ΠΊ Π½ΠΎΠ²ΠΎΠΌΡƒ Ρ‚ΠΈΠΏΡƒ.
331 368 /// </summary>
@@ -334,7 +371,7 namespace Implab {
334 371 /// <param name="error">ΠžΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ ошибки. Π”Π°Π½Π½Ρ‹ΠΉ ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ ΠΏΠΎΠ»ΡƒΡ‡ΠΈΡ‚
335 372 /// ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ возникшСС ΠΏΡ€ΠΈ Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΈ.</param>
336 373 /// <returns>НовоС ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠ΅, ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ΅ Π±ΡƒΠ΄Π΅Ρ‚ Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΎ ΠΏΡ€ΠΈ Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ исходного обСщания.</returns>
337 public IPromise<TNew> Map<TNew>(ResultMapper<T, TNew> mapper, ErrorHandler<T> error) {
374 public IPromise<TNew> Then<TNew>(ResultMapper<T, TNew> mapper, ErrorHandler<T> error) {
338 375 if (mapper == null)
339 376 throw new ArgumentNullException("mapper");
340 377
@@ -370,8 +407,8 namespace Implab {
370 407 return chained;
371 408 }
372 409
373 public IPromise<TNew> Map<TNew>(ResultMapper<T, TNew> mapper) {
374 return Map(mapper, null);
410 public IPromise<TNew> Then<TNew>(ResultMapper<T, TNew> mapper) {
411 return Then(mapper, null);
375 412 }
376 413
377 414 /// <summary>
@@ -384,7 +421,7 namespace Implab {
384 421 /// <param name="error">ΠžΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ ошибки. Π”Π°Π½Π½Ρ‹ΠΉ ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ ΠΏΠΎΠ»ΡƒΡ‡ΠΈΡ‚
385 422 /// ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ возникшСС ΠΏΡ€ΠΈ Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ Ρ‚Π΅ΠΊΡƒΠ΅Ρ‰ΠΉ ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΈ.</param>
386 423 /// <returns>НовоС ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠ΅, ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ΅ Π±ΡƒΠ΄Π΅Ρ‚ Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΎ ΠΏΠΎ ΠΎΠΊΠΎΠ½Ρ‡Π°Π½ΠΈΡŽ ΡƒΠΊΠ°Π·Π°Π½Π½ΠΎΠΉ аснхронной ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΈ.</returns>
387 public IPromise<TNew> Chain<TNew>(ChainedOperation<T, TNew> chained, ErrorHandler<T> error) {
424 public IPromise<TNew> Then<TNew>(ChainedOperation<T, TNew> chained, ErrorHandler<T> error) {
388 425
389 426 // ΠΏΡ€ΠΎΠ±Π»Π΅ΠΌΠ° Π² Ρ‚ΠΎΠΌ, Ρ‡Ρ‚ΠΎ Π½Π° ΠΌΠΎΠΌΠ΅Π½Ρ‚ связывания Π΅Ρ‰Π΅ Π½Π΅ Π½Π°Ρ‡Π°Ρ‚Π° асинхронная опСрация, поэтому Π½ΡƒΠΆΠ½ΠΎ
390 427 // ΡΠΎΠ·Π΄Π°Ρ‚ΡŒ посрСдника, ΠΊ ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΌΡƒ Π±ΡƒΠ΄ΡƒΡ‚ ΠΏΠΎΠ΄Π²Ρ‹Π·ΡΠ²Π°Ρ‚ΡŒΡΡ ΡΠ»Π΅Π΄ΡƒΡŽΡ‰ΠΈΠ΅ ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΠΈ.
@@ -442,8 +479,8 namespace Implab {
442 479 return medium;
443 480 }
444 481
445 public IPromise<TNew> Chain<TNew>(ChainedOperation<T, TNew> chained) {
446 return Chain(chained, null);
482 public IPromise<TNew> Then<TNew>(ChainedOperation<T, TNew> chained) {
483 return Then(chained, null);
447 484 }
448 485
449 486 public IPromise<T> Cancelled(Action handler) {
@@ -478,7 +515,7 namespace Implab {
478 515 /// <typeparam name="T2"></typeparam>
479 516 /// <returns></returns>
480 517 public IPromise<T2> Cast<T2>() {
481 return Map(x => (T2)(object)x, null);
518 return Then(x => (T2)(object)x, null);
482 519 }
483 520
484 521 /// <summary>
@@ -504,8 +541,7 namespace Implab {
504 541 /// <returns>Π Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ выполнСния обСщания</returns>
505 542 public T Join(int timeout) {
506 543 var evt = new ManualResetEvent(false);
507 Anyway(() => evt.Set());
508 Cancelled(() => evt.Set());
544 Finally(() => evt.Set());
509 545
510 546 if (!evt.WaitOne(timeout, true))
511 547 throw new TimeoutException();
@@ -704,10 +740,6 namespace Implab {
704 740 return Error(error);
705 741 }
706 742
707 IPromise IPromise.Anyway(Action handler) {
708 return Anyway(handler);
709 }
710
711 743 IPromise IPromise.Finally(Action handler) {
712 744 return Finally(handler);
713 745 }
@@ -1,8 +1,13
1 1 using System.Threading;
2 using System;
3 #if NET_4_5
4 using System.Threading.Tasks;
5 #endif
2 6
3 7 namespace Implab {
4 8 public static class PromiseExtensions {
5 9 public static IPromise<T> DispatchToCurrentContext<T>(this IPromise<T> that) {
10 Safe.ArgumentNotNull(that, "that");
6 11 var context = SynchronizationContext.Current;
7 12 if (context == null)
8 13 return that;
@@ -20,6 +25,7 namespace Implab {
20 25 }
21 26
22 27 public static IPromise<T> DispatchToContext<T>(this IPromise<T> that, SynchronizationContext context) {
28 Safe.ArgumentNotNull(that, "that");
23 29 Safe.ArgumentNotNull(context, "context");
24 30
25 31 var p = new SyncContextPromise<T>(context, that, true);
@@ -33,6 +39,31 namespace Implab {
33 39 );
34 40 return p;
35 41 }
42
43 public static AsyncCallback AsyncCallback<T>(this Promise<T> that, Func<IAsyncResult,T> callback) {
44 Safe.ArgumentNotNull(that, "that");
45 Safe.ArgumentNotNull(callback, "callback");
46 return ar => {
47 try {
48 that.Resolve(callback(ar));
49 } catch (Exception err) {
50 that.Reject(err);
51 }
52 };
53 }
54
55 #if NET_4_5
56
57 public static Task<T> GetTask<T>(this IPromise<T> that) {
58 Safe.ArgumentNotNull(that, "that");
59 var tcs = new TaskCompletionSource<T>();
60
61 that.Last(tcs.SetResult, tcs.SetException, tcs.SetCanceled);
62
63 return tcs.Task;
64 }
65
66 #endif
36 67 }
37 68 }
38 69
General Comments 0
You need to be logged in to leave comments. Login now