Mercurial > pub > ImplabNet
annotate Implab/FuncChainTaskBase.cs @ 223:27ea7f07e2e4 default
Слияние
author | cin |
---|---|
date | Tue, 22 Aug 2017 09:35:54 +0300 |
parents | 40d7fed4a09e |
children |
rev | line source |
---|---|
145 | 1 using System; |
2 | |
3 namespace Implab { | |
187
dd4a3590f9c6
Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents:
149
diff
changeset
|
4 public class FuncChainTaskBase<TResult> : AbstractTask<TResult> { |
145 | 5 readonly Func<Exception, IPromise<TResult>> m_error; |
6 readonly Func<Exception, IPromise<TResult>> m_cancel; | |
7 | |
149 | 8 protected FuncChainTaskBase( Func<Exception, IPromise<TResult>> error, Func<Exception, IPromise<TResult>> cancel, bool autoCancellable) { |
145 | 9 m_error = error; |
10 m_cancel = cancel; | |
149 | 11 if (autoCancellable) |
12 CancellationRequested(CancelOperation); | |
145 | 13 } |
14 | |
15 public void Reject(Exception error) { | |
16 if (LockCancelation()) | |
17 HandleErrorInternal(error); | |
18 } | |
19 | |
20 public override void CancelOperation(Exception reason) { | |
187
dd4a3590f9c6
Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents:
149
diff
changeset
|
21 if (LockCancelation()) |
dd4a3590f9c6
Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents:
149
diff
changeset
|
22 HandleCancelInternal(reason); |
145 | 23 } |
24 | |
25 protected void HandleErrorInternal(Exception error) { | |
26 if (m_error != null) { | |
27 try { | |
187
dd4a3590f9c6
Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents:
149
diff
changeset
|
28 var p = m_error(error); |
dd4a3590f9c6
Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents:
149
diff
changeset
|
29 p.On(SetResult, SetErrorInternal, SetCancelledInternal); |
dd4a3590f9c6
Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents:
149
diff
changeset
|
30 CancellationRequested(p.Cancel); |
145 | 31 } catch(Exception err) { |
187
dd4a3590f9c6
Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents:
149
diff
changeset
|
32 SetErrorInternal(err); |
145 | 33 } |
34 } else { | |
187
dd4a3590f9c6
Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents:
149
diff
changeset
|
35 SetErrorInternal(error); |
145 | 36 } |
37 } | |
38 | |
187
dd4a3590f9c6
Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents:
149
diff
changeset
|
39 protected void HandleCancelInternal(Exception reason) { |
dd4a3590f9c6
Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents:
149
diff
changeset
|
40 if (m_cancel != null) { |
dd4a3590f9c6
Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents:
149
diff
changeset
|
41 try { |
dd4a3590f9c6
Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents:
149
diff
changeset
|
42 var p = m_cancel(reason); |
dd4a3590f9c6
Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents:
149
diff
changeset
|
43 p.On(SetResult, HandleErrorInternal, SetCancelledInternal); |
dd4a3590f9c6
Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents:
149
diff
changeset
|
44 CancellationRequested(p.Cancel); |
dd4a3590f9c6
Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents:
149
diff
changeset
|
45 } catch (Exception err) { |
196
40d7fed4a09e
fixed promise chaining behavior, the error handler doesn't handle result or cancellation handlers exceptions these exceptions are propagated to the next handlers.
cin
parents:
187
diff
changeset
|
46 SetErrorInternal(err); |
187
dd4a3590f9c6
Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents:
149
diff
changeset
|
47 } |
dd4a3590f9c6
Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents:
149
diff
changeset
|
48 } else { |
196
40d7fed4a09e
fixed promise chaining behavior, the error handler doesn't handle result or cancellation handlers exceptions these exceptions are propagated to the next handlers.
cin
parents:
187
diff
changeset
|
49 SetCancelledInternal(reason); |
187
dd4a3590f9c6
Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents:
149
diff
changeset
|
50 } |
145 | 51 } |
52 } | |
53 } | |
54 |