annotate Implab/SuccessPromise.cs @ 196:40d7fed4a09e

fixed promise chaining behavior, the error handler doesn't handle result or cancellation handlers exceptions these exceptions are propagated to the next handlers.
author cin
date Mon, 29 Aug 2016 23:15:51 +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 : IPromise {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
5 #region IPromise implementation
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 IPromise On(Action success, Action<Exception> error, Action<Exception> cancel) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
8 if (success != null) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
9 try {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
10 success();
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
11 } catch(Exception err) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
12 if (error != 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 error(err);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
15 // Analysis disable once EmptyGeneralCatchClause
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
16 } catch {
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 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
20 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
21 return this;
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 public IPromise On(Action success, Action<Exception> error) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
25 if (success != null) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
26 try {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
27 success();
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
28 } catch(Exception err) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
29 if (error != 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 error(err);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
32 // Analysis disable once EmptyGeneralCatchClause
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
33 } catch {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
34 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
35 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
36 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
37 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
38 return this;
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 public IPromise On(Action success) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
42 if (success != null) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
43 try {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
44 success();
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
45 // Analysis disable once EmptyGeneralCatchClause
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
46 } catch {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
47 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
48 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
49 return this;
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 On(Action handler, PromiseEventType events) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
53 if (handler != null && events.HasFlag(PromiseEventType.Success)) {
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 handler();
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
56 // Analysis disable once EmptyGeneralCatchClause
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
57 } catch {
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> Cast<T>() {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
64 throw new InvalidCastException();
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
65 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
66
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
67 public void Join() {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
68 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
69
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
70 public void Join(int timeout) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
71 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
72
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
73 public Type PromiseType {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
74 get {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
75 return typeof(void);
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
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
79 public bool IsResolved {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
80 get {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
81 return true;
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
82 }
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 bool IsCancelled {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
86 get {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
87 return false;
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
88 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
89 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
90
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
91 public Exception Error {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
92 get {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
93 return null;
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
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
97 #endregion
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
98
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
99 #region ICancellable implementation
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
100
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
101 public void Cancel() {
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 public void Cancel(Exception reason) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
105 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
106
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
107 #endregion
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
108
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
109 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
110 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
111