comparison Implab/FailedPromise.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
children cbe10ac0731e
comparison
equal deleted inserted replaced
202:2651cb9a4250 203:4d9830a9bbb8
1 using System;
2 using System.Reflection;
3
4 namespace Implab {
5 public class FailedPromise : IPromise {
6 readonly Exception m_error;
7 public FailedPromise(Exception error) {
8 Safe.ArgumentNotNull(error, "error");
9 m_error = error;
10 }
11
12 #region IPromise implementation
13
14 public IPromise On(Action success, Action<Exception> error, Action<Exception> cancel) {
15 if (error != null) {
16 try {
17 error(m_error);
18 // Analysis disable once EmptyGeneralCatchClause
19 } catch {
20 }
21 }
22 return this;
23 }
24
25 public IPromise On(Action success, Action<Exception> error) {
26 if (error != null) {
27 try {
28 error(m_error);
29 // Analysis disable once EmptyGeneralCatchClause
30 } catch {
31 }
32 }
33 return this;
34 }
35
36 public IPromise On(Action success) {
37 return this;
38 }
39
40 public IPromise On(Action handler, PromiseEventType events) {
41 if ((events & PromiseEventType.Error) != 0) {
42 try {
43 handler();
44 // Analysis disable once EmptyGeneralCatchClause
45 } catch {
46 }
47 }
48 return this;
49 }
50
51 public IPromise<T> Cast<T>() {
52 return (IPromise<T>)this;
53 }
54
55 public void Join() {
56 throw new TargetInvocationException(Error);
57 }
58
59 public void Join(int timeout) {
60 throw new TargetInvocationException(Error);
61 }
62
63 public virtual Type PromiseType {
64 get {
65 return typeof(void);
66 }
67 }
68
69 public bool IsResolved {
70 get {
71 return true;
72 }
73 }
74
75 public bool IsCancelled {
76 get {
77 return false;
78 }
79 }
80
81 public Exception Error {
82 get {
83 return m_error;
84 }
85 }
86
87 #endregion
88
89 #region ICancellable implementation
90
91 public void Cancel() {
92 }
93
94 public void Cancel(Exception reason) {
95 }
96
97 #endregion
98 }
99 }
100