##// END OF EJS Templates
working on promises
cin -
r247:fb70574741a1 v3
parent child
Show More

The requested changes are too big and content was truncated. Show full diff

@@ -0,0 +1,161
1 using System;
2 using System.Diagnostics;
3 using System.Reflection;
4 using Implab.Parallels;
5
6 namespace Implab {
7 public class AbstractPromise<T> : AbstractEvent<IResolvable<T>>, IPromise<T> {
8
9 class ResolvableSignal : IResolvable<T> {
10 public Signal Signal { get; private set; }
11 public ResolvableSignal() {
12 Signal = new Signal();
13 }
14
15
16 public void Reject(Exception error) {
17 Signal.Set();
18 }
19
20 public void Resolve(T result) {
21 Signal.Set();
22 }
23 }
24
25 class ResolvableWrapper : IResolvable<T> {
26 readonly IResolvable m_resolvable;
27 public ResolvableWrapper(IResolvable resolvable) {
28
29 }
30
31 public void Reject(Exception reason) {
32 m_resolvable.Reject(reason);
33 }
34
35 public void Resolve(T value) {
36 m_resolvable.Resolve();
37 }
38 }
39
40 PromiseState m_state;
41
42 T m_result;
43
44 Exception m_error;
45
46 public bool IsRejected {
47 get {
48 return m_state == PromiseState.Rejected;
49 }
50 }
51
52 public bool IsFulfilled {
53 get {
54 return m_state == PromiseState.Fulfilled;
55 }
56 }
57
58 public Exception RejectReason {
59 get {
60 return m_error;
61 }
62 }
63
64
65 internal void Resolve(T result) {
66 if (BeginTransit())
67 CompleteResolve();
68 }
69
70 internal void Reject(Exception reason) {
71 if (BeginTransit()) {
72 m_error = reason;
73 m_state = PromiseState.Rejected;
74 CompleteTransit();
75 }
76 }
77
78
79 #region implemented abstract members of AbstractPromise
80
81 protected override void SignalHandler(IResolvable<T> handler) {
82 switch (m_state) {
83 case PromiseState.Fulfilled:
84 handler.Resolve(m_result);
85 break;
86 case PromiseState.Rejected:
87 handler.Reject(RejectReason);
88 break;
89 default:
90 throw new InvalidOperationException(String.Format("Invalid promise signal: {0}", m_state));
91 }
92 }
93
94 protected override Signal GetFulfillSignal() {
95 var next = new ResolvableSignal();
96 Then(next);
97 return next.Signal;
98 }
99
100 #endregion
101
102 protected void CompleteResolve() {
103 m_state = PromiseState.Fulfilled;
104 CompleteTransit();
105 }
106
107 public Type ResultType {
108 get {
109 return typeof(void);
110 }
111 }
112
113
114 protected void Rethrow() {
115 Debug.Assert(m_error != null);
116 if (m_error is OperationCanceledException)
117 throw new OperationCanceledException("Operation cancelled", m_error);
118 else
119 throw new TargetInvocationException(m_error);
120 }
121
122 public void Then(IResolvable<T> next) {
123 AddHandler(next);
124 }
125
126 public void Then(IResolvable next) {
127 AddHandler(new ResolvableWrapper(next));
128 }
129
130 public IPromise<T2> Cast<T2>() {
131 return (IPromise<T2>)this;
132 }
133
134 void IPromise.Join() {
135 WaitResult(-1);
136 if (IsRejected)
137 Rethrow();
138 }
139
140 void IPromise.Join(int timeout) {
141 WaitResult(timeout);
142 if (IsRejected)
143 Rethrow();
144 }
145
146 public T Join() {
147 WaitResult(-1);
148 if (IsRejected)
149 Rethrow();
150 return m_result;
151 }
152
153 public T Join(int timeout) {
154 WaitResult(timeout);
155 if (IsRejected)
156 Rethrow();
157 return m_result;
158 }
159 }
160 }
161
@@ -0,0 +1,88
1 using System;
2 using System.Diagnostics;
3
4 namespace Implab {
5 class PromiseActionReaction : PromiseReaction {
6 readonly Action m_fulfilled;
7
8 readonly Action<Exception> m_rejected;
9
10 readonly Deferred m_next;
11
12 public PromiseActionReaction(Action fulfilled, Action<Exception> rejected, Deferred next, IDispatcher dispatcher) : base(dispatcher) {
13 if (fulfilled != null)
14 m_fulfilled = () => {
15 fulfilled();
16 next.Resolve();
17 };
18
19 if (rejected != null)
20 m_rejected = (x) => {
21 rejected(x);
22 next.Resolve();
23 };
24 m_next = next;
25 }
26
27 public PromiseActionReaction(Func<IPromise> fulfilled, Func<Exception, IPromise> rejected, Deferred next, IDispatcher dispatcher) : base(dispatcher) {
28 if (fulfilled != null)
29 m_fulfilled = () => { next.Resolve(fulfilled()); };
30 if (rejected != null)
31 m_rejected = (e) => { next.Resolve(rejected(e)); };
32 m_next = next;
33 }
34
35 public PromiseActionReaction(Action fulfilled, Func<Exception, IPromise> rejected, Deferred next, IDispatcher dispatcher) : base(dispatcher) {
36 if (fulfilled != null)
37 m_fulfilled = () => {
38 fulfilled();
39 next.Resolve();
40 };
41
42 if (rejected != null)
43 m_rejected = (e) => { next.Resolve(rejected(e)); };
44 m_next = next;
45 }
46
47 public PromiseActionReaction(Func<IPromise> fulfilled, Action<Exception> rejected, Deferred next, IDispatcher dispatcher) : base(dispatcher) {
48 if (fulfilled != null)
49 m_fulfilled = () => { next.Resolve(fulfilled()); };
50
51 if (rejected != null)
52 m_rejected = (x) => {
53 rejected(x);
54 next.Resolve();
55 };
56 m_next = next;
57 }
58
59
60 protected override bool HasFulfilHandler => m_fulfilled != null;
61
62 protected override bool HasRejectHandler => m_rejected != null;
63
64 protected override void DefaultReject(Exception reason) {
65 m_next.Reject(reason);
66 }
67
68 protected override void DefaultResolve() {
69 m_next.Resolve();
70 }
71
72 protected override void RejectImpl(Exception reason) {
73 try {
74 m_rejected(reason);
75 } catch (Exception e){
76 m_next.Reject(e);
77 }
78 }
79
80 protected override void ResolveImpl() {
81 try {
82 m_fulfilled();
83 } catch (Exception e){
84 m_next.Reject(e);
85 }
86 }
87 }
88 } No newline at end of file
@@ -0,0 +1,87
1 using System;
2 using System.Diagnostics;
3
4 namespace Implab {
5 class PromiseActionReaction<T> : PromiseReaction<T> {
6 readonly Action<T> m_fulfilled;
7
8 readonly Action<Exception> m_rejected;
9
10 readonly Deferred m_next;
11
12 public PromiseActionReaction(Action<T> fulfilled, Action<Exception> rejected, Deferred next, IDispatcher dispatcher) : base(dispatcher) {
13 if (fulfilled != null)
14 m_fulfilled = (x) => {
15 fulfilled(x);
16 next.Resolve();
17 };
18
19 if (rejected != null)
20 m_rejected = (x) => {
21 rejected(x);
22 next.Resolve();
23 };
24 m_next = next;
25 }
26
27 public PromiseActionReaction(Func<T, IPromise> fulfilled, Func<Exception, IPromise> rejected, Deferred next, IDispatcher dispatcher) : base(dispatcher) {
28 if (fulfilled != null)
29 m_fulfilled = (x) => { next.Resolve(fulfilled(x)); };
30 if (rejected != null)
31 m_rejected = (e) => { next.Resolve(rejected(e)); };
32 m_next = next;
33 }
34
35 public PromiseActionReaction(Action<T> fulfilled, Func<Exception, IPromise> rejected, Deferred next, IDispatcher dispatcher) : base(dispatcher) {
36 if (fulfilled != null)
37 m_fulfilled = (x) => {
38 fulfilled(x);
39 next.Resolve();
40 };
41
42 if (rejected != null)
43 m_rejected = (e) => { next.Resolve(rejected(e)); };
44 m_next = next;
45 }
46
47 public PromiseActionReaction(Func<T, IPromise> fulfilled, Action<Exception> rejected, Deferred next, IDispatcher dispatcher) : base(dispatcher) {
48 if (fulfilled != null)
49 m_fulfilled = (x) => { next.Resolve(fulfilled(x)); };
50
51 if (rejected != null)
52 m_rejected = (x) => {
53 rejected(x);
54 next.Resolve();
55 };
56 m_next = next;
57 }
58
59 protected override bool HasFulfilHandler => m_fulfilled != null;
60
61 protected override bool HasRejectHandler => m_rejected != null;
62
63 protected override void DefaultReject(Exception reason) {
64 m_next.Reject(reason);
65 }
66
67 protected override void DefaultResolve(T result) {
68 m_next.Resolve();
69 }
70
71 protected override void RejectImpl(Exception reason) {
72 try {
73 m_rejected(reason);
74 } catch (Exception e) {
75 m_next.Reject(e);
76 }
77 }
78
79 protected override void ResolveImpl(T result) {
80 try {
81 m_fulfilled(result);
82 } catch (Exception e) {
83 m_next.Reject(e);
84 }
85 }
86 }
87 } No newline at end of file
@@ -0,0 +1,76
1 using System;
2 using System.Diagnostics;
3
4 namespace Implab {
5 class PromiseFuncReaction<TRet> : PromiseReaction {
6 readonly Action m_fulfilled;
7
8 readonly Action<Exception> m_rejected;
9
10 readonly Deferred<TRet> m_next;
11
12 public PromiseFuncReaction(Func<TRet> fulfilled, Func<Exception, TRet> rejected, Deferred<TRet> next, IDispatcher dispatcher) : base(dispatcher) {
13 if (fulfilled != null)
14 m_fulfilled = () => { next.Resolve(fulfilled()); };
15
16 if (rejected != null)
17 m_rejected = (e) => { next.Resolve(rejected(e)); };
18 m_next = next;
19 }
20
21 public PromiseFuncReaction(Func<IPromise<TRet>> fulfilled, Func<Exception, IPromise<TRet>> rejected, Deferred<TRet> next, IDispatcher dispatcher) : base(dispatcher) {
22 if (fulfilled != null)
23 m_fulfilled = () => { next.Resolve(fulfilled()); };
24 if (rejected != null)
25 m_rejected = (e) => { next.Resolve(rejected(e)); };
26 m_next = next;
27 }
28
29 public PromiseFuncReaction(Func<TRet> fulfilled, Func<Exception, IPromise<TRet>> rejected, Deferred<TRet> next, IDispatcher dispatcher) : base(dispatcher) {
30 if (fulfilled != null)
31 m_fulfilled = () => { next.Resolve(fulfilled()); };
32 if (rejected != null)
33 m_rejected = (e) => { next.Resolve(rejected(e)); };
34
35 m_next = next;
36 }
37
38 public PromiseFuncReaction(Func<IPromise<TRet>> fulfilled, Func<Exception, TRet> rejected, Deferred<TRet> next, IDispatcher dispatcher) : base(dispatcher) {
39 if (fulfilled != null)
40 m_fulfilled = () => { next.Resolve(fulfilled()); };
41 if (rejected != null)
42 m_rejected = (e) => { next.Resolve(rejected(e)); };
43
44 m_next = next;
45 }
46
47
48 protected override bool HasFulfilHandler => m_fulfilled != null;
49
50 protected override bool HasRejectHandler => m_rejected != null;
51
52 protected override void DefaultReject(Exception reason) {
53 m_next.Reject(reason);
54 }
55
56 protected override void DefaultResolve() {
57 throw new NotImplementedException();
58 }
59
60 protected override void RejectImpl(Exception reason) {
61 try {
62 m_rejected(reason);
63 } catch (Exception e){
64 m_next.Reject(e);
65 }
66 }
67
68 protected override void ResolveImpl() {
69 try {
70 m_fulfilled();
71 } catch (Exception e){
72 m_next.Reject(e);
73 }
74 }
75 }
76 } No newline at end of file
@@ -118,6 +118,8 namespace Implab {
118
118
119 public void Join(int timeout) {
119 public void Join(int timeout) {
120 WaitResult(timeout);
120 WaitResult(timeout);
121 if (IsRejected)
122 Rethrow();
121 }
123 }
122 }
124 }
123 }
125 }
@@ -37,5 +37,9 namespace Implab {
37 /// Преобразует результат обещания к заданному типу и возвращает новое обещание.
37 /// Преобразует результат обещания к заданному типу и возвращает новое обещание.
38 /// </summary>
38 /// </summary>
39 IPromise<T> Cast<T>();
39 IPromise<T> Cast<T>();
40
41 void Join();
42
43 void Join(int timeout);
40 }
44 }
41 }
45 }
@@ -3,7 +3,7
3 namespace Implab {
3 namespace Implab {
4 public interface IPromise<out T> : IPromise {
4 public interface IPromise<out T> : IPromise {
5
5
6 void On(Action<T> success, Action<Exception> error);
6 void Then(IResolvable<T> next);
7
7
8 new T Join();
8 new T Join();
9
9
@@ -2,7 +2,7 using System;
2
2
3 namespace Implab {
3 namespace Implab {
4
4
5 public interface IResolvable<T> {
5 public interface IResolvable<in T> {
6 void Resolve(T value);
6 void Resolve(T value);
7
7
8 void Reject(Exception reason);
8 void Reject(Exception reason);
@@ -1,35 +1,45
1 using System;
1 using System;
2
2
3 namespace Implab {
3 namespace Implab {
4 public class PromiseReaction : IResolvable {
4 abstract class PromiseReaction : IResolvable {
5 IDispatcher m_dispatcher;
5 readonly IDispatcher m_dispatcher;
6
7 protected PromiseReaction(IDispatcher dispatcher) {
8 m_dispatcher = dispatcher;
9 }
6
10
7 Action m_onFulfilledJob;
11 protected abstract bool HasFulfilHandler {
12 get;
13 }
8
14
9 Action<Exception> m_onRejectedJob;
15 protected abstract bool HasRejectHandler {
10
16 get;
11 IResolvable m_next;
17 }
12
18
13 public void Reject(Exception error) {
19 public void Reject(Exception error) {
14 if (m_onRejectedJob != null) {
20 if (!HasRejectHandler)
15 if (m_dispatcher != null)
21 DefaultReject(error);
16 m_dispatcher.Enqueue(() => m_onRejectedJob(error));
22 else if (m_dispatcher != null)
23 m_dispatcher.Enqueue(() => RejectImpl(error));
17 else
24 else
18 m_onRejectedJob(error);
25 RejectImpl(error);
19 } else {
20 m_next.Reject(error);
21 }
22 }
26 }
23
27
24 public void Resolve() {
28 public void Resolve() {
25 if (m_onRejectedJob != null) {
29 if (!HasFulfilHandler)
26 if (m_dispatcher != null)
30 DefaultResolve();
27 m_dispatcher.Enqueue( m_onFulfilledJob);
31 else if (m_dispatcher != null)
32 m_dispatcher.Enqueue(ResolveImpl);
28 else
33 else
29 m_onFulfilledJob();
34 ResolveImpl();
30 } else {
31 m_next.Resolve();
32 }
35 }
33 }
36
37 protected abstract void ResolveImpl();
38
39 protected abstract void RejectImpl(Exception reason);
40
41 protected abstract void DefaultResolve();
42
43 protected abstract void DefaultReject(Exception reason);
34 }
44 }
35 } No newline at end of file
45 }
@@ -1,25 +1,45
1 using System;
1 using System;
2
2
3 namespace Implab {
3 namespace Implab {
4 public class PromiseReaction<T> : IResolvable<T> {
4 abstract class PromiseReaction<T> : IResolvable<T> {
5 IDispatcher m_dispatcher;
5 readonly IDispatcher m_dispatcher;
6
7 protected PromiseReaction(IDispatcher dispatcher) {
8 m_dispatcher = dispatcher;
9 }
6
10
7 Action<T> m_onFulfilledJob;
11 protected abstract bool HasFulfilHandler {
12 get;
13 }
8
14
9 Action<Exception> m_onRejectedJob;
15 protected abstract bool HasRejectHandler {
16 get;
17 }
10
18
11 public void Reject(Exception error) {
19 public void Reject(Exception error) {
12 if (m_dispatcher != null)
20 if (!HasRejectHandler)
13 m_dispatcher.Enqueue(() => m_onRejectedJob(error));
21 DefaultReject(error);
22 else if (m_dispatcher != null)
23 m_dispatcher.Enqueue(() => RejectImpl(error));
14 else
24 else
15 m_onRejectedJob(error);
25 RejectImpl(error);
16 }
26 }
17
27
18 public void Resolve(T result) {
28 public void Resolve(T result) {
19 if (m_dispatcher != null)
29 if (!HasFulfilHandler)
20 m_dispatcher.Enqueue(() => m_onFulfilledJob(result));
30 DefaultResolve(result);
31 else if (m_dispatcher != null)
32 m_dispatcher.Enqueue(() => ResolveImpl(result));
21 else
33 else
22 m_onFulfilledJob(result);
34 ResolveImpl(result);
23 }
35 }
36
37 protected abstract void ResolveImpl(T result);
38
39 protected abstract void RejectImpl(Exception reason);
40
41 protected abstract void DefaultResolve(T result);
42
43 protected abstract void DefaultReject(Exception reason);
24 }
44 }
25 } No newline at end of file
45 }
@@ -1,4 +1,4
1 Copyright 2012 Sergey Smirnov
1 Copyright 2012-2018 Sergey Smirnov
2
2
3 Redistribution and use in source and binary forms, with or without
3 Redistribution and use in source and binary forms, with or without
4 modification, are permitted provided that the following conditions are met:
4 modification, are permitted provided that the following conditions are met:
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed, binary diff hidden
NO CONTENT: file was removed, binary diff hidden
1 NO CONTENT: file was removed, binary diff hidden
NO CONTENT: file was removed, binary diff hidden
1 NO CONTENT: file was removed
NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed, binary diff hidden
NO CONTENT: file was removed, binary diff hidden
1 NO CONTENT: file was removed, binary diff hidden
NO CONTENT: file was removed, binary diff hidden
1 NO CONTENT: file was removed
NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed, binary diff hidden
NO CONTENT: file was removed, binary diff hidden
1 NO CONTENT: file was removed
NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed, binary diff hidden
NO CONTENT: file was removed, binary diff hidden
1 NO CONTENT: file was removed
NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed, binary diff hidden
NO CONTENT: file was removed, binary diff hidden
1 NO CONTENT: file was removed
NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed, binary diff hidden
NO CONTENT: file was removed, binary diff hidden
1 NO CONTENT: file was removed
NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed, binary diff hidden
NO CONTENT: file was removed, binary diff hidden
1 NO CONTENT: file was removed, binary diff hidden
NO CONTENT: file was removed, binary diff hidden
1 NO CONTENT: file was removed
NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
General Comments 3
Under Review
author

Auto status change to "Under Review"

Approved
author

ok, latest stable version should be in default

You need to be logged in to leave comments. Login now