forked from Real-Serious-Games/C-Sharp-Promise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PromiseTimer.cs
248 lines (204 loc) · 6.98 KB
/
PromiseTimer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
using System;
using System.Collections.Generic;
namespace RSG
{
public class PromiseCancelledException : Exception
{
/// <summary>
/// Just create the exception
/// </summary>
public PromiseCancelledException()
{
}
/// <summary>
/// Create the exception with description
/// </summary>
/// <param name="message">Exception description</param>
public PromiseCancelledException(String message) : base(message)
{
}
}
/// <summary>
/// A class that wraps a pending promise with it's predicate and time data
/// </summary>
internal class PredicateWait
{
/// <summary>
/// Predicate for resolving the promise
/// </summary>
public Func<TimeData, bool> predicate;
/// <summary>
/// The time the promise was started
/// </summary>
public float timeStarted;
/// <summary>
/// The pending promise which is an interface for a promise that can be rejected or resolved.
/// </summary>
public IPendingPromise pendingPromise;
/// <summary>
/// The time data specific to this pending promise. Includes elapsed time and delta time.
/// </summary>
public TimeData timeData;
/// <summary>
/// The frame the promise was started
/// </summary>
public int frameStarted;
}
/// <summary>
/// Time data specific to a particular pending promise.
/// </summary>
public struct TimeData
{
/// <summary>
/// The amount of time that has elapsed since the pending promise started running
/// </summary>
public float elapsedTime;
/// <summary>
/// The amount of time since the last time the pending promise was updated.
/// </summary>
public float deltaTime;
/// <summary>
/// The amount of times that update has been called since the pending promise started running
/// </summary>
public int elapsedUpdates;
}
public interface IPromiseTimer
{
/// <summary>
/// Resolve the returned promise once the time has elapsed
/// </summary>
IPromise WaitFor(float seconds);
/// <summary>
/// Resolve the returned promise once the predicate evaluates to true
/// </summary>
IPromise WaitUntil(Func<TimeData, bool> predicate);
/// <summary>
/// Resolve the returned promise once the predicate evaluates to false
/// </summary>
IPromise WaitWhile(Func<TimeData, bool> predicate);
/// <summary>
/// Update all pending promises. Must be called for the promises to progress and resolve at all.
/// </summary>
void Update(float deltaTime);
/// <summary>
/// Cancel a waiting promise and reject it immediately.
/// </summary>
bool Cancel(IPromise promise);
}
public class PromiseTimer : IPromiseTimer
{
/// <summary>
/// The current running total for time that this PromiseTimer has run for
/// </summary>
private float curTime;
/// <summary>
/// The current running total for the amount of frames the PromiseTimer has run for
/// </summary>
private int curFrame;
/// <summary>
/// Currently pending promises
/// </summary>
private readonly LinkedList<PredicateWait> waiting = new LinkedList<PredicateWait>();
/// <summary>
/// Resolve the returned promise once the time has elapsed
/// </summary>
public IPromise WaitFor(float seconds)
{
return WaitUntil(t => t.elapsedTime >= seconds);
}
/// <summary>
/// Resolve the returned promise once the predicate evaluates to false
/// </summary>
public IPromise WaitWhile(Func<TimeData, bool> predicate)
{
return WaitUntil(t => !predicate(t));
}
/// <summary>
/// Resolve the returned promise once the predicate evalutes to true
/// </summary>
public IPromise WaitUntil(Func<TimeData, bool> predicate)
{
var promise = new Promise();
var wait = new PredicateWait()
{
timeStarted = curTime,
pendingPromise = promise,
timeData = new TimeData(),
predicate = predicate,
frameStarted = curFrame
};
waiting.AddLast(wait);
return promise;
}
public bool Cancel(IPromise promise)
{
var node = FindInWaiting(promise);
if (node == null)
{
return false;
}
node.Value.pendingPromise.Reject(new PromiseCancelledException("Promise was cancelled by user."));
waiting.Remove(node);
return true;
}
LinkedListNode<PredicateWait> FindInWaiting(IPromise promise)
{
for (var node = waiting.First; node != null; node = node.Next)
{
if (node.Value.pendingPromise.Id.Equals(promise.Id))
{
return node;
}
}
return null;
}
/// <summary>
/// Update all pending promises. Must be called for the promises to progress and resolve at all.
/// </summary>
public void Update(float deltaTime)
{
curTime += deltaTime;
curFrame += 1;
var node = waiting.First;
while (node != null)
{
var wait = node.Value;
var newElapsedTime = curTime - wait.timeStarted;
wait.timeData.deltaTime = newElapsedTime - wait.timeData.elapsedTime;
wait.timeData.elapsedTime = newElapsedTime;
var newElapsedUpdates = curFrame - wait.frameStarted;
wait.timeData.elapsedUpdates = newElapsedUpdates;
bool result;
try
{
result = wait.predicate(wait.timeData);
}
catch (Exception ex)
{
wait.pendingPromise.Reject(ex);
node = RemoveNode(node);
continue;
}
if (result)
{
wait.pendingPromise.Resolve();
node = RemoveNode(node);
}
else
{
node = node.Next;
}
}
}
/// <summary>
/// Removes the provided node and returns the next node in the list.
/// </summary>
private LinkedListNode<PredicateWait> RemoveNode(LinkedListNode<PredicateWait> node)
{
var currentNode = node;
node = node.Next;
waiting.Remove(currentNode);
return node;
}
}
}