comparison Implab/AbstractPromise.cs @ 242:cbe10ac0731e v3

Working on promises
author cin
date Wed, 24 Jan 2018 03:03:21 +0300
parents 86187b01c4e0
children b1e0ffdf3451
comparison
equal deleted inserted replaced
240:fa6cbf4d8841 242:cbe10ac0731e
1 using System; 1 using System;
2 using Implab.Parallels; 2 using Implab.Parallels;
3 3
4 namespace Implab { 4 namespace Implab {
5 public abstract class AbstractPromise : AbstractEvent<AbstractPromise.HandlerDescriptor>, IPromise { 5 public abstract class AbstractPromise : AbstractEvent<AbstractPromise.HandlerDescriptor>, IPromise {
6 public struct HandlerDescriptor { 6 public class HandlerDescriptor {
7 readonly Action m_handler; 7 readonly Action m_handler;
8 readonly Action<Exception> m_error; 8 readonly Action<Exception> m_error;
9 readonly Action<Exception> m_cancel; 9 public HandlerDescriptor(Action success, Action<Exception> error) {
10 readonly PromiseEventType m_mask;
11
12 public HandlerDescriptor(Action success, Action<Exception> error, Action<Exception> cancel) {
13 m_handler = success; 10 m_handler = success;
14 m_error = error; 11 m_error = error;
15 m_cancel = cancel;
16 m_mask = PromiseEventType.Success;
17 }
18
19 public HandlerDescriptor(Action handler, PromiseEventType mask) {
20 m_handler = handler;
21 m_error = null;
22 m_cancel = null;
23 m_mask = mask;
24 } 12 }
25 13
26 public void SignalSuccess() { 14 public void SignalSuccess() {
27 if ((m_mask & PromiseEventType.Success) != 0 && m_handler != null) { 15 if (m_handler != null) {
28 try { 16 try {
29 m_handler(); 17 m_handler();
30 // Analysis disable once EmptyGeneralCatchClause 18 // Analysis disable once EmptyGeneralCatchClause
31 } catch { 19 } catch {
32 } 20 }
35 23
36 public void SignalError(Exception err) { 24 public void SignalError(Exception err) {
37 if (m_error != null) { 25 if (m_error != null) {
38 try { 26 try {
39 m_error(err); 27 m_error(err);
40 // Analysis disable once EmptyGeneralCatchClause
41 } catch {
42 }
43 } else if ((m_mask & PromiseEventType.Error ) != 0 && m_handler != null) {
44 try {
45 m_handler();
46 // Analysis disable once EmptyGeneralCatchClause
47 } catch {
48 }
49 }
50 }
51
52 public void SignalCancel(Exception reason) {
53 if (m_cancel != null) {
54 try {
55 m_cancel(reason);
56 // Analysis disable once EmptyGeneralCatchClause
57 } catch {
58 }
59 } else if ( (m_mask & PromiseEventType.Cancelled) != 0 && m_handler != null) {
60 try {
61 m_handler();
62 // Analysis disable once EmptyGeneralCatchClause 28 // Analysis disable once EmptyGeneralCatchClause
63 } catch { 29 } catch {
64 } 30 }
65 } 31 }
66 } 32 }
73 switch (signal) { 39 switch (signal) {
74 case SUCCEEDED_STATE: 40 case SUCCEEDED_STATE:
75 handler.SignalSuccess(); 41 handler.SignalSuccess();
76 break; 42 break;
77 case REJECTED_STATE: 43 case REJECTED_STATE:
78 handler.SignalError(Error); 44 handler.SignalError(RejectReason);
79 break;
80 case CANCELLED_STATE:
81 handler.SignalCancel(CancellationReason);
82 break; 45 break;
83 default: 46 default:
84 throw new InvalidOperationException(String.Format("Invalid promise signal: {0}", signal)); 47 throw new InvalidOperationException(String.Format("Invalid promise signal: {0}", signal));
85 } 48 }
86 } 49 }
87 50
88 protected override Signal GetResolveSignal() { 51 protected override Signal GetFulfillSignal() {
89 var signal = new Signal(); 52 var signal = new Signal();
90 On(signal.Set, PromiseEventType.All); 53 On(signal.Set, e => signal.Set());
91 return signal; 54 return signal;
92 } 55 }
93 56
94 #endregion 57 #endregion
95 58
96 public Type PromiseType { 59 public Type ResultType {
97 get { 60 get {
98 return typeof(void); 61 return typeof(void);
99 } 62 }
100 } 63 }
101 64
102 public IPromise On(Action success, Action<Exception> error, Action<Exception> cancel) { 65 public void On(Action success, Action<Exception> error) {
103 AddHandler(new HandlerDescriptor(success, error, cancel)); 66 AddHandler(new HandlerDescriptor(success, error));
104 return this;
105 }
106
107 public IPromise On(Action success, Action<Exception> error) {
108 AddHandler(new HandlerDescriptor(success, error, null));
109 return this;
110 }
111
112 public IPromise On(Action success) {
113 AddHandler(new HandlerDescriptor(success, null, null));
114 return this;
115 }
116
117 public IPromise On(Action handler, PromiseEventType events) {
118 AddHandler(new HandlerDescriptor(handler,events));
119 return this;
120 } 67 }
121 68
122 public IPromise<T> Cast<T>() { 69 public IPromise<T> Cast<T>() {
123 throw new InvalidCastException(); 70 throw new InvalidCastException();
124 } 71 }