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