annotate Implab/AbstractPromise.cs @ 242:cbe10ac0731e v3

Working on promises
author cin
date Wed, 24 Jan 2018 03:03:21 +0300
parents 86187b01c4e0
children b1e0ffdf3451
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
1 using System;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
2 using Implab.Parallels;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
3
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
4 namespace Implab {
144
8c0b95069066 DRAFT: refactoring
cin
parents: 143
diff changeset
5 public abstract class AbstractPromise : AbstractEvent<AbstractPromise.HandlerDescriptor>, IPromise {
242
cbe10ac0731e Working on promises
cin
parents: 197
diff changeset
6 public class HandlerDescriptor {
144
8c0b95069066 DRAFT: refactoring
cin
parents: 143
diff changeset
7 readonly Action m_handler;
8c0b95069066 DRAFT: refactoring
cin
parents: 143
diff changeset
8 readonly Action<Exception> m_error;
242
cbe10ac0731e Working on promises
cin
parents: 197
diff changeset
9 public HandlerDescriptor(Action success, Action<Exception> error) {
144
8c0b95069066 DRAFT: refactoring
cin
parents: 143
diff changeset
10 m_handler = success;
8c0b95069066 DRAFT: refactoring
cin
parents: 143
diff changeset
11 m_error = error;
8c0b95069066 DRAFT: refactoring
cin
parents: 143
diff changeset
12 }
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
13
144
8c0b95069066 DRAFT: refactoring
cin
parents: 143
diff changeset
14 public void SignalSuccess() {
242
cbe10ac0731e Working on promises
cin
parents: 197
diff changeset
15 if (m_handler != null) {
144
8c0b95069066 DRAFT: refactoring
cin
parents: 143
diff changeset
16 try {
8c0b95069066 DRAFT: refactoring
cin
parents: 143
diff changeset
17 m_handler();
197
86187b01c4e0 fixed: the error handler should not handle handlers errors
cin
parents: 186
diff changeset
18 // Analysis disable once EmptyGeneralCatchClause
86187b01c4e0 fixed: the error handler should not handle handlers errors
cin
parents: 186
diff changeset
19 } catch {
144
8c0b95069066 DRAFT: refactoring
cin
parents: 143
diff changeset
20 }
8c0b95069066 DRAFT: refactoring
cin
parents: 143
diff changeset
21 }
8c0b95069066 DRAFT: refactoring
cin
parents: 143
diff changeset
22 }
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
23
144
8c0b95069066 DRAFT: refactoring
cin
parents: 143
diff changeset
24 public void SignalError(Exception err) {
8c0b95069066 DRAFT: refactoring
cin
parents: 143
diff changeset
25 if (m_error != null) {
8c0b95069066 DRAFT: refactoring
cin
parents: 143
diff changeset
26 try {
8c0b95069066 DRAFT: refactoring
cin
parents: 143
diff changeset
27 m_error(err);
8c0b95069066 DRAFT: refactoring
cin
parents: 143
diff changeset
28 // Analysis disable once EmptyGeneralCatchClause
8c0b95069066 DRAFT: refactoring
cin
parents: 143
diff changeset
29 } catch {
8c0b95069066 DRAFT: refactoring
cin
parents: 143
diff changeset
30 }
8c0b95069066 DRAFT: refactoring
cin
parents: 143
diff changeset
31 }
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
32 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
33 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
34
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
35
144
8c0b95069066 DRAFT: refactoring
cin
parents: 143
diff changeset
36 #region implemented abstract members of AbstractPromise
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
37
156
97fbbf816844 Promises: SignalXXX methods merged into SignalHandler method.
cin
parents: 145
diff changeset
38 protected override void SignalHandler(HandlerDescriptor handler, int signal) {
97fbbf816844 Promises: SignalXXX methods merged into SignalHandler method.
cin
parents: 145
diff changeset
39 switch (signal) {
97fbbf816844 Promises: SignalXXX methods merged into SignalHandler method.
cin
parents: 145
diff changeset
40 case SUCCEEDED_STATE:
97fbbf816844 Promises: SignalXXX methods merged into SignalHandler method.
cin
parents: 145
diff changeset
41 handler.SignalSuccess();
97fbbf816844 Promises: SignalXXX methods merged into SignalHandler method.
cin
parents: 145
diff changeset
42 break;
97fbbf816844 Promises: SignalXXX methods merged into SignalHandler method.
cin
parents: 145
diff changeset
43 case REJECTED_STATE:
242
cbe10ac0731e Working on promises
cin
parents: 197
diff changeset
44 handler.SignalError(RejectReason);
156
97fbbf816844 Promises: SignalXXX methods merged into SignalHandler method.
cin
parents: 145
diff changeset
45 break;
97fbbf816844 Promises: SignalXXX methods merged into SignalHandler method.
cin
parents: 145
diff changeset
46 default:
97fbbf816844 Promises: SignalXXX methods merged into SignalHandler method.
cin
parents: 145
diff changeset
47 throw new InvalidOperationException(String.Format("Invalid promise signal: {0}", signal));
97fbbf816844 Promises: SignalXXX methods merged into SignalHandler method.
cin
parents: 145
diff changeset
48 }
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
49 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
50
242
cbe10ac0731e Working on promises
cin
parents: 197
diff changeset
51 protected override Signal GetFulfillSignal() {
144
8c0b95069066 DRAFT: refactoring
cin
parents: 143
diff changeset
52 var signal = new Signal();
242
cbe10ac0731e Working on promises
cin
parents: 197
diff changeset
53 On(signal.Set, e => signal.Set());
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 144
diff changeset
54 return signal;
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
55 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
56
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
57 #endregion
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
58
242
cbe10ac0731e Working on promises
cin
parents: 197
diff changeset
59 public Type ResultType {
144
8c0b95069066 DRAFT: refactoring
cin
parents: 143
diff changeset
60 get {
8c0b95069066 DRAFT: refactoring
cin
parents: 143
diff changeset
61 return typeof(void);
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
62 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
63 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
64
242
cbe10ac0731e Working on promises
cin
parents: 197
diff changeset
65 public void On(Action success, Action<Exception> error) {
cbe10ac0731e Working on promises
cin
parents: 197
diff changeset
66 AddHandler(new HandlerDescriptor(success, error));
144
8c0b95069066 DRAFT: refactoring
cin
parents: 143
diff changeset
67 }
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
68
144
8c0b95069066 DRAFT: refactoring
cin
parents: 143
diff changeset
69 public IPromise<T> Cast<T>() {
8c0b95069066 DRAFT: refactoring
cin
parents: 143
diff changeset
70 throw new InvalidCastException();
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
71 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
72
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
73 public void Join() {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
74 WaitResult(-1);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
75 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
76
144
8c0b95069066 DRAFT: refactoring
cin
parents: 143
diff changeset
77 public void Join(int timeout) {
8c0b95069066 DRAFT: refactoring
cin
parents: 143
diff changeset
78 WaitResult(timeout);
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
79 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
80
144
8c0b95069066 DRAFT: refactoring
cin
parents: 143
diff changeset
81 protected void SetResult() {
186
75103928da09 working on cancelation and error handling
cin
parents: 156
diff changeset
82 if(BeginSetResult())
75103928da09 working on cancelation and error handling
cin
parents: 156
diff changeset
83 EndSetResult();
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 130
diff changeset
84 }
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
85 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
86 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
87