annotate Implab/PromiseExtensions.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 a867536c68fc
children d82909310094
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
72
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
1 using System.Threading;
75
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
2 using System;
109
1b7ebcc52e5a minor fixes
cin
parents: 104
diff changeset
3 using Implab.Diagnostics;
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 110
diff changeset
4 using System.Collections.Generic;
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
5 using System.Linq;
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
6
72
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
7 namespace Implab {
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
8 public static class PromiseExtensions {
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
9
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
10 public static IPromise Then(this IPromise that, Action fulfilled, Action<Exception> rejected) {
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
11 var reaction = new PromiseActionReaction(fulfilled, rejected, Promise.DefaultDispatcher);
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
12 that.Then(reaction);
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
13 return reaction.Promise;
101
279e226dffdd code cleanup
cin
parents: 76
diff changeset
14 }
279e226dffdd code cleanup
cin
parents: 76
diff changeset
15
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
16 public static IPromise Then(this IPromise that, Action fulfilled, Func<Exception, IPromise> rejected) {
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
17 var reaction = new PromiseActionReaction(fulfilled, rejected, Promise.DefaultDispatcher);
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
18 that.Then(reaction);
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
19 return reaction.Promise;
207
558f34b2fb50 added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents: 205
diff changeset
20 }
558f34b2fb50 added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents: 205
diff changeset
21
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
22 public static IPromise Then(this IPromise that, Func<IPromise> fulfilled, Action<Exception> rejected) {
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
23 var reaction = new PromiseActionReaction(fulfilled, rejected, Promise.DefaultDispatcher);
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
24 that.Then(reaction);
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
25 return reaction.Promise;
75
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
26 }
110
1a8426e6e895 added promise timeout helper
cin
parents: 109
diff changeset
27
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
28 public static IPromise Then(this IPromise that, Func<IPromise> fulfilled, Func<Exception, IPromise> rejected) {
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
29 var reaction = new PromiseActionReaction(fulfilled, rejected, Promise.DefaultDispatcher);
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
30 that.Then(reaction);
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
31 return reaction.Promise;
110
1a8426e6e895 added promise timeout helper
cin
parents: 109
diff changeset
32 }
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 110
diff changeset
33
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
34 public static IPromise Then<T>(this IPromise<T> that, Action<T> fulfilled, Action<Exception> rejected) {
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
35 var reaction = new PromiseActionReaction<T>(fulfilled, rejected, Promise.DefaultDispatcher);
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
36 that.Then(reaction);
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
37 return reaction.Promise;
205
8200ab154c8a Added ResetState to RunnableComponent to reset in case of failure
cin
parents: 185
diff changeset
38 }
8200ab154c8a Added ResetState to RunnableComponent to reset in case of failure
cin
parents: 185
diff changeset
39
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
40 public static IPromise Then<T>(this IPromise<T> that, Action<T> fulfilled, Func<Exception, IPromise> rejected) {
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
41 var reaction = new PromiseActionReaction<T>(fulfilled, rejected, Promise.DefaultDispatcher);
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
42 that.Then(reaction);
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
43 return reaction.Promise;
208
7d07503621fe RunnableComponent.Dispose(bool,Exception) changed to standart Dispose(bool)
cin
parents: 207
diff changeset
44 }
7d07503621fe RunnableComponent.Dispose(bool,Exception) changed to standart Dispose(bool)
cin
parents: 207
diff changeset
45
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
46 public static IPromise Then<T>(this IPromise<T> that, Func<T, IPromise> fulfilled, Action<Exception> rejected) {
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
47 var reaction = new PromiseActionReaction<T>(fulfilled, rejected, Promise.DefaultDispatcher);
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
48 that.Then(reaction);
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
49 return reaction.Promise;
124
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
50 }
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
51
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
52 public static IPromise Then<T>(this IPromise<T> that, Func<T, IPromise> fulfilled, Func<Exception, IPromise> rejected) {
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
53 var reaction = new PromiseActionReaction<T>(fulfilled, rejected, Promise.DefaultDispatcher);
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
54 that.Then(reaction);
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
55 return reaction.Promise;
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
56 }
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
57
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
58 public static IPromise<Tout> Then<Tout>(this IPromise that, Func<Tout> fulfilled, Func<Exception, Tout> rejected) {
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
59 var reaction = new PromiseFuncReaction<Tout>(fulfilled, rejected, Promise.DefaultDispatcher);
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
60 that.Then(reaction);
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
61 return reaction.Promise;
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
62 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
63
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
64 public static IPromise<Tout> Then<Tout>(this IPromise that, Func<Tout> fulfilled, Func<Exception, IPromise<Tout>> rejected) {
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
65 var reaction = new PromiseFuncReaction<Tout>(fulfilled, rejected, Promise.DefaultDispatcher);
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
66 that.Then(reaction);
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
67 return reaction.Promise;
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
68 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
69
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
70 public static IPromise<Tout> Then<Tout>(this IPromise that, Func<IPromise<Tout>> fulfilled, Func<Exception, Tout> rejected) {
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
71 var reaction = new PromiseFuncReaction<Tout>(fulfilled, rejected, Promise.DefaultDispatcher);
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
72 that.Then(reaction);
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
73 return reaction.Promise;
207
558f34b2fb50 added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents: 205
diff changeset
74 }
558f34b2fb50 added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents: 205
diff changeset
75
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
76 public static IPromise<Tout> Then<Tout>(this IPromise that, Func<IPromise<Tout>> fulfilled, Func<Exception, IPromise<Tout>> rejected) {
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
77 var reaction = new PromiseFuncReaction<Tout>(fulfilled, rejected, Promise.DefaultDispatcher);
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
78 that.Then(reaction);
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
79 return reaction.Promise;
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
80 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
81
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
82 public static IPromise<Tout> Then<Tin, Tout>(this IPromise<Tin> that, Func<Tin, Tout> fulfilled, Func<Exception, Tout> rejected) {
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
83 var reaction = new PromiseFuncReaction<Tin, Tout>(fulfilled, rejected, Promise.DefaultDispatcher);
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
84 that.Then(reaction);
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
85 return reaction.Promise;
207
558f34b2fb50 added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents: 205
diff changeset
86 }
558f34b2fb50 added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents: 205
diff changeset
87
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
88 public static IPromise<Tout> Then<Tin, Tout>(this IPromise<Tin> that, Func<Tin, Tout> fulfilled, Func<Exception, IPromise<Tout>> rejected) {
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
89 var reaction = new PromiseFuncReaction<Tin, Tout>(fulfilled, rejected, Promise.DefaultDispatcher);
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
90 that.Then(reaction);
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
91 return reaction.Promise;
207
558f34b2fb50 added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents: 205
diff changeset
92 }
558f34b2fb50 added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents: 205
diff changeset
93
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
94 public static IPromise<Tout> Then<Tin, Tout>(this IPromise<Tin> that, Func<Tin, IPromise<Tout>> fulfilled, Func<Exception, Tout> rejected) {
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
95 var reaction = new PromiseFuncReaction<Tin, Tout>(fulfilled, rejected, Promise.DefaultDispatcher);
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
96 that.Then(reaction);
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
97 return reaction.Promise;
207
558f34b2fb50 added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents: 205
diff changeset
98 }
558f34b2fb50 added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents: 205
diff changeset
99
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
100 public static IPromise<Tout> Then<Tin, Tout>(this IPromise<Tin> that, Func<Tin, IPromise<Tout>> fulfilled, Func<Exception, IPromise<Tout>> rejected) {
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
101 var reaction = new PromiseFuncReaction<Tin, Tout>(fulfilled, rejected, Promise.DefaultDispatcher);
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
102 that.Then(reaction);
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
103 return reaction.Promise;
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
104 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
105
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
106 public static IPromise Catch(this IPromise that, Action<Exception> rejected) {
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
107 return Then(that, null, rejected);
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
108 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
109
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
110 public static IPromise Catch(this IPromise that, Func<Exception, IPromise> rejected) {
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
111 return Then(that, null, rejected);
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
112 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
113
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
114 public static IPromise<Tout> Catch<Tout>(this IPromise that, Func<Exception, Tout> rejected) {
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
115 return Then(that, (Func<Tout>)null, rejected);
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
116 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
117
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
118 public static IPromise<Tout> Catch<Tout>(this IPromise that, Func<Exception, IPromise<Tout>> rejected) {
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
119 return Then(that, (Func<Tout>)null, rejected);
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
120 }
75
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
121
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
122 public static IPromise<Tout> Catch<Tin, Tout>(this IPromise<Tin> that, Func<Exception, Tout> rejected) {
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
123 return Then(that, (Func<Tin, Tout>)null, rejected);
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
124 }
208
7d07503621fe RunnableComponent.Dispose(bool,Exception) changed to standart Dispose(bool)
cin
parents: 207
diff changeset
125
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
126 public static IPromise<Tout> Catch<Tin, Tout>(this IPromise<Tin> that, Func<Exception, IPromise<Tout>> rejected) {
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
127 return Then(that, (Func<Tin, Tout>)null, rejected);
5cb4826c2c2a Added awaiters to promises
cin
parents: 209
diff changeset
128 }
72
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
129 }
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
130 }
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
131