Mercurial > pub > ImplabNet
annotate Implab/ActionChainTaskBase.cs @ 239:eedf4d834e67 v2
fix
| author | cin |
|---|---|
| date | Wed, 13 Dec 2017 19:54:45 +0300 |
| parents | b305c678923a |
| children |
| rev | line source |
|---|---|
| 145 | 1 using System; |
| 2 using System.Threading; | |
| 3 | |
| 4 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:
186
diff
changeset
|
5 public class ActionChainTaskBase : AbstractTask { |
| 145 | 6 readonly Func<Exception, IPromise> m_error; |
| 7 readonly Func<Exception, IPromise> m_cancel; | |
| 8 | |
| 149 | 9 protected ActionChainTaskBase(Func<Exception, IPromise> error, Func<Exception, IPromise> cancel, bool autoCancellable) { |
| 145 | 10 m_error = error; |
| 11 m_cancel = cancel; | |
| 149 | 12 if (autoCancellable) |
| 13 CancellationRequested(CancelOperation); | |
| 145 | 14 } |
| 15 | |
| 16 public void Reject(Exception error) { | |
| 17 if (LockCancelation()) | |
| 18 HandleErrorInternal(error); | |
| 19 } | |
| 20 | |
| 21 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:
186
diff
changeset
|
22 if (LockCancelation()) |
|
dd4a3590f9c6
Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents:
186
diff
changeset
|
23 // отмена вызвана до начала выполнения задачи |
|
dd4a3590f9c6
Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents:
186
diff
changeset
|
24 HandleCancelInternal(reason); |
|
dd4a3590f9c6
Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents:
186
diff
changeset
|
25 } |
|
dd4a3590f9c6
Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents:
186
diff
changeset
|
26 |
|
dd4a3590f9c6
Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents:
186
diff
changeset
|
27 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:
186
diff
changeset
|
28 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:
186
diff
changeset
|
29 try { |
|
dd4a3590f9c6
Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents:
186
diff
changeset
|
30 // вызываем обработчик отмены |
|
dd4a3590f9c6
Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents:
186
diff
changeset
|
31 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:
186
diff
changeset
|
32 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:
186
diff
changeset
|
33 // сообщаем асинхронной операции, что клиент уже не хочет получать результат |
|
dd4a3590f9c6
Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents:
186
diff
changeset
|
34 // т.е. если он инициировал отмену, задача отменилась, вызвался обрабочик отмены |
|
dd4a3590f9c6
Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents:
186
diff
changeset
|
35 // отбработчику сообщили, что результат уже не нужен и уже сам обработчик решает |
|
dd4a3590f9c6
Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents:
186
diff
changeset
|
36 // отдавать ли результат или подтвердить отмену (или вернуть ошибку). |
|
dd4a3590f9c6
Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents:
186
diff
changeset
|
37 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:
186
diff
changeset
|
38 } 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
|
39 SetErrorInternal(err); |
| 145 | 40 } |
|
187
dd4a3590f9c6
Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents:
186
diff
changeset
|
41 } 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
|
42 SetCancelledInternal(reason); |
| 145 | 43 } |
| 44 } | |
| 45 | |
|
187
dd4a3590f9c6
Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents:
186
diff
changeset
|
46 protected void HandleErrorInternal(Exception error) { |
| 145 | 47 if (m_error != null) { |
| 48 try { | |
| 149 | 49 var p = m_error(error); |
|
187
dd4a3590f9c6
Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
cin
parents:
186
diff
changeset
|
50 p.On(SetResult, SetErrorInternal, SetCancelledInternal); |
| 149 | 51 CancellationRequested(p.Cancel); |
| 52 } catch (Exception err) { | |
| 198 | 53 SetErrorInternal(err); |
| 145 | 54 } |
| 55 } else { | |
| 186 | 56 SetErrorInternal(error); |
| 57 } | |
| 58 } | |
| 59 | |
| 145 | 60 } |
| 61 } | |
| 62 |
