145
|
1 using System;
|
|
2 using System.Threading;
|
|
3
|
|
4 namespace Implab {
|
|
5 public class ActionChainTaskBase : AbstractPromise {
|
|
6 readonly Func<Exception, IPromise> m_error;
|
|
7 readonly Func<Exception, IPromise> m_cancel;
|
|
8
|
|
9 int m_cancelationLock;
|
|
10
|
149
|
11 protected ActionChainTaskBase(Func<Exception, IPromise> error, Func<Exception, IPromise> cancel, bool autoCancellable) {
|
145
|
12 m_error = error;
|
|
13 m_cancel = cancel;
|
149
|
14 if (autoCancellable)
|
|
15 CancellationRequested(CancelOperation);
|
145
|
16 }
|
|
17
|
|
18 public void Reject(Exception error) {
|
|
19 if (LockCancelation())
|
|
20 HandleErrorInternal(error);
|
|
21 }
|
|
22
|
|
23
|
|
24
|
|
25 public override void CancelOperation(Exception reason) {
|
149
|
26 if (LockCancelation()) {
|
|
27 if (m_cancel != null) {
|
|
28 try {
|
|
29 m_cancel(reason).On(SetResult, SetError, SetCancelled);
|
|
30 } catch (Exception err) {
|
|
31 HandleErrorInternal(err);
|
|
32 }
|
|
33 } else {
|
|
34 SetCancelled(reason);
|
145
|
35 }
|
|
36 }
|
|
37 }
|
|
38
|
|
39 protected void HandleErrorInternal(Exception error) {
|
|
40 if (m_error != null) {
|
|
41 try {
|
149
|
42 var p = m_error(error);
|
|
43 p.On(SetResult,SetError,SetCancelled);
|
|
44 CancellationRequested(p.Cancel);
|
|
45 } catch (Exception err) {
|
145
|
46 SetError(err);
|
|
47 }
|
|
48 } else {
|
|
49 SetError(error);
|
|
50 }
|
|
51 }
|
|
52
|
|
53 protected bool LockCancelation() {
|
|
54 return 0 == Interlocked.CompareExchange(ref m_cancelationLock, 1, 0);
|
|
55 }
|
|
56 }
|
|
57 }
|
|
58
|