annotate Implab/AbstractPromise.cs @ 244:eee3e49dd1ff v3

working on promises
author cin
date Thu, 25 Jan 2018 19:09:16 +0300
parents b1e0ffdf3451
children fb70574741a1
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;
243
b1e0ffdf3451 working on promises
cin
parents: 242
diff changeset
2 using System.Diagnostics;
b1e0ffdf3451 working on promises
cin
parents: 242
diff changeset
3 using System.Reflection;
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
4 using Implab.Parallels;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
5
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
6 namespace Implab {
244
eee3e49dd1ff working on promises
cin
parents: 243
diff changeset
7 public class AbstractPromise : AbstractEvent<IResolvable>, IPromise {
243
b1e0ffdf3451 working on promises
cin
parents: 242
diff changeset
8
244
eee3e49dd1ff working on promises
cin
parents: 243
diff changeset
9 class ResolvableSignal : IResolvable {
eee3e49dd1ff working on promises
cin
parents: 243
diff changeset
10 public Signal Signal { get; private set; }
eee3e49dd1ff working on promises
cin
parents: 243
diff changeset
11 public ResolvableSignal() {
eee3e49dd1ff working on promises
cin
parents: 243
diff changeset
12 Signal = new Signal();
144
8c0b95069066 DRAFT: refactoring
cin
parents: 143
diff changeset
13 }
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
14
244
eee3e49dd1ff working on promises
cin
parents: 243
diff changeset
15
eee3e49dd1ff working on promises
cin
parents: 243
diff changeset
16 public void Reject(Exception error) {
eee3e49dd1ff working on promises
cin
parents: 243
diff changeset
17 Signal.Set();
144
8c0b95069066 DRAFT: refactoring
cin
parents: 143
diff changeset
18 }
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
19
244
eee3e49dd1ff working on promises
cin
parents: 243
diff changeset
20 public void Resolve() {
eee3e49dd1ff working on promises
cin
parents: 243
diff changeset
21 Signal.Set();
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
22 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
23 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
24
243
b1e0ffdf3451 working on promises
cin
parents: 242
diff changeset
25 PromiseState m_state;
b1e0ffdf3451 working on promises
cin
parents: 242
diff changeset
26
b1e0ffdf3451 working on promises
cin
parents: 242
diff changeset
27 Exception m_error;
b1e0ffdf3451 working on promises
cin
parents: 242
diff changeset
28
b1e0ffdf3451 working on promises
cin
parents: 242
diff changeset
29 public bool IsRejected {
b1e0ffdf3451 working on promises
cin
parents: 242
diff changeset
30 get {
b1e0ffdf3451 working on promises
cin
parents: 242
diff changeset
31 return m_state == PromiseState.Rejected;
b1e0ffdf3451 working on promises
cin
parents: 242
diff changeset
32 }
b1e0ffdf3451 working on promises
cin
parents: 242
diff changeset
33 }
b1e0ffdf3451 working on promises
cin
parents: 242
diff changeset
34
244
eee3e49dd1ff working on promises
cin
parents: 243
diff changeset
35 public bool IsFulfilled {
243
b1e0ffdf3451 working on promises
cin
parents: 242
diff changeset
36 get {
244
eee3e49dd1ff working on promises
cin
parents: 243
diff changeset
37 return m_state == PromiseState.Fulfilled;
243
b1e0ffdf3451 working on promises
cin
parents: 242
diff changeset
38 }
b1e0ffdf3451 working on promises
cin
parents: 242
diff changeset
39 }
b1e0ffdf3451 working on promises
cin
parents: 242
diff changeset
40
b1e0ffdf3451 working on promises
cin
parents: 242
diff changeset
41 public Exception RejectReason {
b1e0ffdf3451 working on promises
cin
parents: 242
diff changeset
42 get {
b1e0ffdf3451 working on promises
cin
parents: 242
diff changeset
43 return m_error;
b1e0ffdf3451 working on promises
cin
parents: 242
diff changeset
44 }
b1e0ffdf3451 working on promises
cin
parents: 242
diff changeset
45 }
b1e0ffdf3451 working on promises
cin
parents: 242
diff changeset
46
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
47
244
eee3e49dd1ff working on promises
cin
parents: 243
diff changeset
48 internal void Resolve() {
eee3e49dd1ff working on promises
cin
parents: 243
diff changeset
49 if (BeginTransit())
eee3e49dd1ff working on promises
cin
parents: 243
diff changeset
50 CompleteResolve();
eee3e49dd1ff working on promises
cin
parents: 243
diff changeset
51 }
eee3e49dd1ff working on promises
cin
parents: 243
diff changeset
52
eee3e49dd1ff working on promises
cin
parents: 243
diff changeset
53 internal void Reject(Exception reason) {
eee3e49dd1ff working on promises
cin
parents: 243
diff changeset
54 if (BeginTransit()) {
eee3e49dd1ff working on promises
cin
parents: 243
diff changeset
55 m_error = reason;
eee3e49dd1ff working on promises
cin
parents: 243
diff changeset
56 m_state = PromiseState.Rejected;
eee3e49dd1ff working on promises
cin
parents: 243
diff changeset
57 CompleteTransit();
eee3e49dd1ff working on promises
cin
parents: 243
diff changeset
58 }
eee3e49dd1ff working on promises
cin
parents: 243
diff changeset
59 }
eee3e49dd1ff working on promises
cin
parents: 243
diff changeset
60
eee3e49dd1ff working on promises
cin
parents: 243
diff changeset
61
144
8c0b95069066 DRAFT: refactoring
cin
parents: 143
diff changeset
62 #region implemented abstract members of AbstractPromise
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
63
244
eee3e49dd1ff working on promises
cin
parents: 243
diff changeset
64 protected override void SignalHandler(IResolvable handler) {
243
b1e0ffdf3451 working on promises
cin
parents: 242
diff changeset
65 switch (m_state) {
244
eee3e49dd1ff working on promises
cin
parents: 243
diff changeset
66 case PromiseState.Fulfilled:
eee3e49dd1ff working on promises
cin
parents: 243
diff changeset
67 handler.Resolve();
156
97fbbf816844 Promises: SignalXXX methods merged into SignalHandler method.
cin
parents: 145
diff changeset
68 break;
243
b1e0ffdf3451 working on promises
cin
parents: 242
diff changeset
69 case PromiseState.Rejected:
244
eee3e49dd1ff working on promises
cin
parents: 243
diff changeset
70 handler.Reject(RejectReason);
156
97fbbf816844 Promises: SignalXXX methods merged into SignalHandler method.
cin
parents: 145
diff changeset
71 break;
97fbbf816844 Promises: SignalXXX methods merged into SignalHandler method.
cin
parents: 145
diff changeset
72 default:
243
b1e0ffdf3451 working on promises
cin
parents: 242
diff changeset
73 throw new InvalidOperationException(String.Format("Invalid promise signal: {0}", m_state));
156
97fbbf816844 Promises: SignalXXX methods merged into SignalHandler method.
cin
parents: 145
diff changeset
74 }
119
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
242
cbe10ac0731e Working on promises
cin
parents: 197
diff changeset
77 protected override Signal GetFulfillSignal() {
244
eee3e49dd1ff working on promises
cin
parents: 243
diff changeset
78 var next = new ResolvableSignal();
eee3e49dd1ff working on promises
cin
parents: 243
diff changeset
79 Then(next);
eee3e49dd1ff working on promises
cin
parents: 243
diff changeset
80 return next.Signal;
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
81 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
82
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
83 #endregion
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
84
243
b1e0ffdf3451 working on promises
cin
parents: 242
diff changeset
85 protected void CompleteResolve() {
244
eee3e49dd1ff working on promises
cin
parents: 243
diff changeset
86 m_state = PromiseState.Fulfilled;
243
b1e0ffdf3451 working on promises
cin
parents: 242
diff changeset
87 CompleteTransit();
b1e0ffdf3451 working on promises
cin
parents: 242
diff changeset
88 }
b1e0ffdf3451 working on promises
cin
parents: 242
diff changeset
89
242
cbe10ac0731e Working on promises
cin
parents: 197
diff changeset
90 public Type ResultType {
144
8c0b95069066 DRAFT: refactoring
cin
parents: 143
diff changeset
91 get {
8c0b95069066 DRAFT: refactoring
cin
parents: 143
diff changeset
92 return typeof(void);
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
93 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
94 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
95
243
b1e0ffdf3451 working on promises
cin
parents: 242
diff changeset
96
b1e0ffdf3451 working on promises
cin
parents: 242
diff changeset
97 protected void Rethrow() {
b1e0ffdf3451 working on promises
cin
parents: 242
diff changeset
98 Debug.Assert(m_error != null);
b1e0ffdf3451 working on promises
cin
parents: 242
diff changeset
99 if (m_error is OperationCanceledException)
b1e0ffdf3451 working on promises
cin
parents: 242
diff changeset
100 throw new OperationCanceledException("Operation cancelled", m_error);
b1e0ffdf3451 working on promises
cin
parents: 242
diff changeset
101 else
b1e0ffdf3451 working on promises
cin
parents: 242
diff changeset
102 throw new TargetInvocationException(m_error);
b1e0ffdf3451 working on promises
cin
parents: 242
diff changeset
103 }
b1e0ffdf3451 working on promises
cin
parents: 242
diff changeset
104
244
eee3e49dd1ff working on promises
cin
parents: 243
diff changeset
105 public void Then(IResolvable next) {
eee3e49dd1ff working on promises
cin
parents: 243
diff changeset
106 AddHandler(next);
144
8c0b95069066 DRAFT: refactoring
cin
parents: 143
diff changeset
107 }
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
108
144
8c0b95069066 DRAFT: refactoring
cin
parents: 143
diff changeset
109 public IPromise<T> Cast<T>() {
8c0b95069066 DRAFT: refactoring
cin
parents: 143
diff changeset
110 throw new InvalidCastException();
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
111 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
112
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
113 public void Join() {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
114 WaitResult(-1);
243
b1e0ffdf3451 working on promises
cin
parents: 242
diff changeset
115 if (IsRejected)
b1e0ffdf3451 working on promises
cin
parents: 242
diff changeset
116 Rethrow();
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
117 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
118
144
8c0b95069066 DRAFT: refactoring
cin
parents: 143
diff changeset
119 public void Join(int timeout) {
8c0b95069066 DRAFT: refactoring
cin
parents: 143
diff changeset
120 WaitResult(timeout);
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
121 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
122 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
123 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
124