Mercurial > pub > ImplabNet
annotate Implab/FuncTaskBase.cs @ 243:b1e0ffdf3451 v3
working on promises
author | cin |
---|---|
date | Wed, 24 Jan 2018 19:24:10 +0300 |
parents | 40d7fed4a09e |
children |
rev | line source |
---|---|
144 | 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 FuncTaskBase<TResult> : AbstractTask<TResult> { |
144 | 5 readonly Func<Exception, TResult> m_cancel; |
6 readonly Func<Exception, TResult> m_error; | |
7 | |
149 | 8 protected FuncTaskBase( Func<Exception, TResult> error, Func<Exception, TResult> cancel, bool autoCancellable) { |
144 | 9 m_error = error; |
10 m_cancel = cancel; | |
149 | 11 if (autoCancellable) |
12 CancellationRequested(CancelOperation); | |
144 | 13 } |
14 | |
15 public void Reject(Exception error) { | |
16 Safe.ArgumentNotNull(error, "error"); | |
17 if (LockCancelation()) | |
18 HandleErrorInternal(error); | |
19 } | |
20 | |
21 protected void HandleErrorInternal(Exception error) { | |
22 if (m_error != null) { | |
23 try { | |
24 SetResult(m_error(error)); | |
25 } 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
|
26 SetErrorInternal(err); |
144 | 27 } |
28 } 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
|
29 SetErrorInternal(error); |
144 | 30 } |
31 } | |
32 | |
33 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
|
34 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
|
35 HandleCancelInternal(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
|
36 } |
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
|
37 |
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
|
38 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
|
39 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
|
40 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
|
41 SetResult(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
|
42 } 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
|
43 SetErrorInternal(err); |
144 | 44 } |
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
|
45 } 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
|
46 SetCancelledInternal(reason); |
144 | 47 } |
48 } | |
49 | |
50 } | |
51 } | |
52 |