annotate Implab/SuccessPromiseT.cs @ 209:a867536c68fc v2

Bound promise to CancellationToken Added new states to ExecutionSate enum. Added Safe.Guard() method to handle cleanup of the result of the promise
author cin
date Wed, 16 Nov 2016 03:06:08 +0300
parents 4d9830a9bbb8
children cbe10ac0731e
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);
203
4d9830a9bbb8 Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents: 145
diff changeset
15 // Analysis disable once EmptyGeneralCatchClause
4d9830a9bbb8 Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents: 145
diff changeset
16 } catch {
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
17 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
18 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
19 return this;
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
20 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
21
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
22 public IPromise<T> On(Action<T> success, Action<Exception> error) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
23 if (success != null) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
24 try {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
25 success(m_value);
203
4d9830a9bbb8 Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents: 145
diff changeset
26 // Analysis disable once EmptyGeneralCatchClause
4d9830a9bbb8 Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents: 145
diff changeset
27 } catch {
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
28 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
29 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
30 return this;
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
31 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
32
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
33 public IPromise<T> On(Action<T> success) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
34 if (success != null) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
35 try {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
36 success(m_value);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
37 // Analysis disable once EmptyGeneralCatchClause
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
38 } catch {
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 return this;
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
42 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
43
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
44 public T Join() {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
45 return m_value;
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
46 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
47
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
48 public T Join(int timeout) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
49 return m_value;
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
50 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
51
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
52 public IPromise<T> On(Action success, Action<Exception> error, Action<Exception> cancel) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
53 if (success != null) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
54 try {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
55 success();
203
4d9830a9bbb8 Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents: 145
diff changeset
56 // Analysis disable once EmptyGeneralCatchClause
4d9830a9bbb8 Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents: 145
diff changeset
57 } catch {
145
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 return this;
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
61 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
62
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
63 public IPromise<T> On(Action success, Action<Exception> error) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
64 if (success != null) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
65 try {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
66 success();
203
4d9830a9bbb8 Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents: 145
diff changeset
67 // Analysis disable once EmptyGeneralCatchClause
4d9830a9bbb8 Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents: 145
diff changeset
68 } catch {
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
69 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
70 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
71 return this;
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
72 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
73
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
74 public IPromise<T> On(Action success) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
75 if (success != null) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
76 try {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
77 success();
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
78 // Analysis disable once EmptyGeneralCatchClause
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
79 } catch {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
80 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
81 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
82 return this;
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
83 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
84
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
85 public IPromise<T> On(Action handler, PromiseEventType events) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
86 if (handler != null && events.HasFlag(PromiseEventType.Success)) {
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 handler();
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 return this;
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
94 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
95
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
96 IPromise IPromise.On(Action success, Action<Exception> error, Action<Exception> cancel) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
97 return On(success, error, cancel);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
98 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
99
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
100 IPromise IPromise.On(Action success, Action<Exception> error) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
101 return On(success, error);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
102 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
103
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
104 IPromise IPromise.On(Action success) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
105 return On(success);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
106 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
107
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
108 IPromise IPromise.On(Action handler, PromiseEventType events) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
109 return On(handler, events);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
110 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
111
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
112 public IPromise<T2> Cast<T2>() {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
113 return new SuccessPromise<T2>((T2)(object)m_value);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
114 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
115
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
116 void IPromise.Join() {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
117 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
118
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
119 void IPromise.Join(int timeout) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
120 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
121
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
122 public Type PromiseType {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
123 get {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
124 return typeof(T);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
125 }
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 public bool IsResolved {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
129 get {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
130 return true;
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
131 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
132 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
133
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
134 public bool IsCancelled {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
135 get {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
136 return false;
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
137 }
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 public Exception Error {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
141 get {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
142 return null;
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
143 }
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 void Cancel() {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
147 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
148
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
149 public void Cancel(Exception reason) {
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 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
153