annotate Implab/FuncTaskBase.cs @ 203:4d9830a9bbb8 v2

Added 'Fail' method to RunnableComponent which allows component to move from Running to Failed state. Added PollingComponent a timer based runnable component More tests Added FailPromise a thin class to wrap exceptions Fixed error handling in SuccessPromise classes.
author cin
date Tue, 18 Oct 2016 17:49:54 +0300
parents 40d7fed4a09e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
144
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
1 using System;
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
2
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
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
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
5 readonly Func<Exception, TResult> m_cancel;
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
6 readonly Func<Exception, TResult> m_error;
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
7
149
eb793fbbe4ea fixed promises cancellation
cin
parents: 144
diff changeset
8 protected FuncTaskBase( Func<Exception, TResult> error, Func<Exception, TResult> cancel, bool autoCancellable) {
144
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
9 m_error = error;
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
10 m_cancel = cancel;
149
eb793fbbe4ea fixed promises cancellation
cin
parents: 144
diff changeset
11 if (autoCancellable)
eb793fbbe4ea fixed promises cancellation
cin
parents: 144
diff changeset
12 CancellationRequested(CancelOperation);
144
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
13 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
14
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
15 public void Reject(Exception error) {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
16 Safe.ArgumentNotNull(error, "error");
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
17 if (LockCancelation())
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
18 HandleErrorInternal(error);
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
19 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
20
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
21 protected void HandleErrorInternal(Exception error) {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
22 if (m_error != null) {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
23 try {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
24 SetResult(m_error(error));
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
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
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
27 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
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
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
30 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
31 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
32
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
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
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
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
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
47 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
48 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
49
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
50 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
51 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
52