annotate Implab/RejectedPromise`1.cs @ 248:5cb4826c2c2a v3

Added awaiters to promises Added static methods to Promise Resolve, Reject, All. Updated promise helpers
author cin
date Tue, 30 Jan 2018 01:37:17 +0300
parents
children d82909310094
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
248
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
1 using System;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
2
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
3 namespace Implab
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
4 {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
5 public struct RejectedPromise<T> : IPromise<T> {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
6 readonly Exception m_reason;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
7
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
8 public Type ResultType => typeof(void);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
9
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
10 public bool IsResolved => true;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
11
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
12 public bool IsRejected => true;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
13
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
14 public bool IsFulfilled => false;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
15
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
16 public Exception RejectReason => m_reason;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
17
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
18 public RejectedPromise(Exception reason) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
19 m_reason = reason;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
20 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
21
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
22 public IPromise<T2> Cast<T2>() {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
23 return (IPromise<T2>)(IPromise<T>)this;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
24 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
25
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
26 void IPromise.Join() {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
27 m_reason.ThrowInvocationException();
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
28 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
29
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
30 void IPromise.Join(int timeout) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
31 m_reason.ThrowInvocationException();
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
32 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
33
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
34 public T Join() {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
35 m_reason.ThrowInvocationException();
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
36 throw new Exception(); // unreachable code
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
37 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
38
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
39 public T Join(int timeout) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
40 m_reason.ThrowInvocationException();
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
41 throw new Exception(); // unreachable code
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
42 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
43
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
44 public void Then(IResolvable next) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
45 next.Reject(m_reason);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
46 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
47
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
48 public void Then(IResolvable<T> next) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
49 next.Reject(m_reason);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
50 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
51 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
52 }