145
|
1 using System;
|
|
2
|
|
3 namespace Implab {
|
|
4 public class SuccessPromise<T> : IPromise<T> {
|
|
5 readonly T m_value;
|
|
6
|
|
7 public SuccessPromise(T value){
|
|
8 m_value = value;
|
|
9 }
|
|
10
|
|
11 public IPromise<T> On(Action<T> success, Action<Exception> error, Action<Exception> cancel) {
|
|
12 if (success != null) {
|
|
13 try {
|
|
14 success(m_value);
|
|
15 } catch(Exception err) {
|
|
16 if (error != null) {
|
|
17 try {
|
|
18 error(err);
|
|
19 // Analysis disable once EmptyGeneralCatchClause
|
|
20 } catch {
|
|
21 }
|
|
22 }
|
|
23 }
|
|
24 }
|
|
25 return this;
|
|
26 }
|
|
27
|
|
28 public IPromise<T> On(Action<T> success, Action<Exception> error) {
|
|
29 if (success != null) {
|
|
30 try {
|
|
31 success(m_value);
|
|
32 } catch(Exception err) {
|
|
33 if (error != null) {
|
|
34 try {
|
|
35 error(err);
|
|
36 // Analysis disable once EmptyGeneralCatchClause
|
|
37 } catch {
|
|
38 }
|
|
39 }
|
|
40 }
|
|
41 }
|
|
42 return this;
|
|
43 }
|
|
44
|
|
45 public IPromise<T> On(Action<T> success) {
|
|
46 if (success != null) {
|
|
47 try {
|
|
48 success(m_value);
|
|
49 // Analysis disable once EmptyGeneralCatchClause
|
|
50 } catch {
|
|
51 }
|
|
52 }
|
|
53 return this;
|
|
54 }
|
|
55
|
|
56 public T Join() {
|
|
57 return m_value;
|
|
58 }
|
|
59
|
|
60 public T Join(int timeout) {
|
|
61 return m_value;
|
|
62 }
|
|
63
|
|
64 public IPromise<T> On(Action success, Action<Exception> error, Action<Exception> cancel) {
|
|
65 if (success != null) {
|
|
66 try {
|
|
67 success();
|
|
68 } catch(Exception err) {
|
|
69 if (error != null) {
|
|
70 try {
|
|
71 error(err);
|
|
72 // Analysis disable once EmptyGeneralCatchClause
|
|
73 } catch {
|
|
74 }
|
|
75 }
|
|
76 }
|
|
77 }
|
|
78 return this;
|
|
79 }
|
|
80
|
|
81 public IPromise<T> On(Action success, Action<Exception> error) {
|
|
82 if (success != null) {
|
|
83 try {
|
|
84 success();
|
|
85 } catch(Exception err) {
|
|
86 if (error != null) {
|
|
87 try {
|
|
88 error(err);
|
|
89 // Analysis disable once EmptyGeneralCatchClause
|
|
90 } catch {
|
|
91 }
|
|
92 }
|
|
93 }
|
|
94 }
|
|
95 return this;
|
|
96 }
|
|
97
|
|
98 public IPromise<T> On(Action success) {
|
|
99 if (success != null) {
|
|
100 try {
|
|
101 success();
|
|
102 // Analysis disable once EmptyGeneralCatchClause
|
|
103 } catch {
|
|
104 }
|
|
105 }
|
|
106 return this;
|
|
107 }
|
|
108
|
|
109 public IPromise<T> On(Action handler, PromiseEventType events) {
|
|
110 if (handler != null && events.HasFlag(PromiseEventType.Success)) {
|
|
111 try {
|
|
112 handler();
|
|
113 // Analysis disable once EmptyGeneralCatchClause
|
|
114 } catch {
|
|
115 }
|
|
116 }
|
|
117 return this;
|
|
118 }
|
|
119
|
|
120 IPromise IPromise.On(Action success, Action<Exception> error, Action<Exception> cancel) {
|
|
121 return On(success, error, cancel);
|
|
122 }
|
|
123
|
|
124 IPromise IPromise.On(Action success, Action<Exception> error) {
|
|
125 return On(success, error);
|
|
126 }
|
|
127
|
|
128 IPromise IPromise.On(Action success) {
|
|
129 return On(success);
|
|
130 }
|
|
131
|
|
132 IPromise IPromise.On(Action handler, PromiseEventType events) {
|
|
133 return On(handler, events);
|
|
134 }
|
|
135
|
|
136 public IPromise<T2> Cast<T2>() {
|
|
137 return new SuccessPromise<T2>((T2)(object)m_value);
|
|
138 }
|
|
139
|
|
140 void IPromise.Join() {
|
|
141 }
|
|
142
|
|
143 void IPromise.Join(int timeout) {
|
|
144 }
|
|
145
|
|
146 public Type PromiseType {
|
|
147 get {
|
|
148 return typeof(T);
|
|
149 }
|
|
150 }
|
|
151
|
|
152 public bool IsResolved {
|
|
153 get {
|
|
154 return true;
|
|
155 }
|
|
156 }
|
|
157
|
|
158 public bool IsCancelled {
|
|
159 get {
|
|
160 return false;
|
|
161 }
|
|
162 }
|
|
163
|
|
164 public Exception Error {
|
|
165 get {
|
|
166 return null;
|
|
167 }
|
|
168 }
|
|
169
|
|
170 public void Cancel() {
|
|
171 }
|
|
172
|
|
173 public void Cancel(Exception reason) {
|
|
174 }
|
|
175 }
|
|
176 }
|
|
177
|