annotate Implab/SuccessPromiseT.cs @ 151:ec91a6dfa5b3 v2

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