comparison Implab/Safe.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 a0ff6a0e9c44
children 8200ab154c8a
comparison
equal deleted inserted replaced
202:2651cb9a4250 203:4d9830a9bbb8
65 if (d != null) 65 if (d != null)
66 d.Dispose(); 66 d.Dispose();
67 } 67 }
68 68
69 [DebuggerStepThrough] 69 [DebuggerStepThrough]
70 public static IPromise<T> WrapPromise<T>(Func<T> action) { 70 public static IPromise<T> Run<T>(Func<T> action) {
71 ArgumentNotNull(action, "action");
72
73 var p = new Promise<T>();
74 try {
75 p.Resolve(action());
76 } catch (Exception err) {
77 p.Reject(err);
78 }
79
80 return p;
81 }
82
83 [DebuggerStepThrough]
84 public static IPromise WrapPromise(Action action) {
85 ArgumentNotNull(action, "action");
86
87 var p = new Promise();
88 try {
89 action();
90 p.Resolve();
91 } catch (Exception err) {
92 p.Reject(err);
93 }
94
95 return p;
96 }
97
98 [DebuggerStepThrough]
99 public static IPromise InvokePromise(Func<IPromise> action) {
100 ArgumentNotNull(action, "action"); 71 ArgumentNotNull(action, "action");
101 72
102 try { 73 try {
103 var p = action(); 74 return Promise<T>.FromResult(action());
104 if (p == null) {
105 var d = new Promise();
106 d.Reject(new Exception("The action returned null"));
107 p = d;
108 }
109 return p;
110 } catch (Exception err) { 75 } catch (Exception err) {
111 var p = new Promise(); 76 return Promise<T>.FromException(err);
112 p.Reject(err);
113 return p;
114 } 77 }
115 } 78 }
116 79
117 [DebuggerStepThrough] 80 [DebuggerStepThrough]
118 public static IPromise<T> InvokePromise<T>(Func<IPromise<T>> action) { 81 public static IPromise Run(Action action) {
82 ArgumentNotNull(action, "action");
83
84 try {
85 action();
86 return Promise.SUCCESS;
87 } catch (Exception err) {
88 return new FailedPromise(err);
89 }
90 }
91
92 [DebuggerStepThrough]
93 public static IPromise Run(Func<IPromise> action) {
94 ArgumentNotNull(action, "action");
95
96 try {
97 return action() ?? new FailedPromise(new Exception("The action returned null"));
98 } catch (Exception err) {
99 return new FailedPromise(err);
100 }
101 }
102
103 [DebuggerStepThrough]
104 public static IPromise<T> Run<T>(Func<IPromise<T>> action) {
119 ArgumentNotNull(action, "action"); 105 ArgumentNotNull(action, "action");
120 106
121 try { 107 try {
122 return action() ?? Promise<T>.FromException(new Exception("The action returned null")); 108 return action() ?? Promise<T>.FromException(new Exception("The action returned null"));
123 } catch (Exception err) { 109 } catch (Exception err) {