Skip to content

Commit 7509f87

Browse files
committed
- Added support for IEnumerator<T>
- Added helper static class Awaiters
1 parent d0f04d1 commit 7509f87

File tree

3 files changed

+71
-6
lines changed

3 files changed

+71
-6
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using UnityEngine;
3+
4+
// TODO: Remove the allocs here, use a static memory pool?
5+
public static class Awaiters
6+
{
7+
readonly static WaitForUpdate _waitForUpdate = new WaitForUpdate();
8+
readonly static WaitForFixedUpdate _waitForFixedUpdate = new WaitForFixedUpdate();
9+
readonly static WaitForEndOfFrame _waitForEndOfFrame = new WaitForEndOfFrame();
10+
11+
public static WaitForUpdate NextFrame
12+
{
13+
get { return _waitForUpdate; }
14+
}
15+
16+
public static WaitForFixedUpdate FixedUpdate
17+
{
18+
get { return _waitForFixedUpdate; }
19+
}
20+
21+
public static WaitForEndOfFrame EndOfFrame
22+
{
23+
get { return _waitForEndOfFrame; }
24+
}
25+
26+
public static WaitForSeconds Seconds(float seconds)
27+
{
28+
return new WaitForSeconds(seconds);
29+
}
30+
31+
public static WaitForSecondsRealtime SecondsRealtime(float seconds)
32+
{
33+
return new WaitForSecondsRealtime(seconds);
34+
}
35+
36+
public static WaitUntil Until(Func<bool> predicate)
37+
{
38+
return new WaitUntil(predicate);
39+
}
40+
41+
public static WaitWhile While(Func<bool> predicate)
42+
{
43+
return new WaitWhile(predicate);
44+
}
45+
}

UnityProject/Assets/Plugins/AsyncAwaitUtil/Source/Awaiters.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnityProject/Assets/Plugins/AsyncAwaitUtil/Source/IEnumeratorAwaitExtensions.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,19 @@ public static SimpleCoroutineAwaiter<AssetBundle> GetAwaiter(this AssetBundleCre
8787
return awaiter;
8888
}
8989

90+
public static SimpleCoroutineAwaiter<T> GetAwaiter<T>(this IEnumerator<T> coroutine)
91+
{
92+
var awaiter = new SimpleCoroutineAwaiter<T>();
93+
RunOnUnityScheduler(() => AsyncCoroutineRunner.Instance.StartCoroutine(
94+
new CoroutineWrapper<T>(coroutine, awaiter).Run()));
95+
return awaiter;
96+
}
97+
9098
public static SimpleCoroutineAwaiter<object> GetAwaiter(this IEnumerator coroutine)
9199
{
92100
var awaiter = new SimpleCoroutineAwaiter<object>();
93101
RunOnUnityScheduler(() => AsyncCoroutineRunner.Instance.StartCoroutine(
94-
new CoroutineWrapper(coroutine, awaiter).Run()));
102+
new CoroutineWrapper<object>(coroutine, awaiter).Run()));
95103
return awaiter;
96104
}
97105

@@ -225,13 +233,13 @@ void INotifyCompletion.OnCompleted(Action continuation)
225233
}
226234
}
227235

228-
class CoroutineWrapper
236+
class CoroutineWrapper<T>
229237
{
230-
readonly SimpleCoroutineAwaiter<object> _awaiter;
238+
readonly SimpleCoroutineAwaiter<T> _awaiter;
231239
readonly Stack<IEnumerator> _processStack;
232240

233241
public CoroutineWrapper(
234-
IEnumerator coroutine, SimpleCoroutineAwaiter<object> awaiter)
242+
IEnumerator coroutine, SimpleCoroutineAwaiter<T> awaiter)
235243
{
236244
_processStack = new Stack<IEnumerator>();
237245
_processStack.Push(coroutine);
@@ -252,7 +260,7 @@ public IEnumerator Run()
252260
}
253261
catch (Exception e)
254262
{
255-
_awaiter.Complete(null, e);
263+
_awaiter.Complete(default(T), e);
256264
yield break;
257265
}
258266

@@ -262,7 +270,7 @@ public IEnumerator Run()
262270

263271
if (_processStack.Count == 0)
264272
{
265-
_awaiter.Complete(topWorker.Current, null);
273+
_awaiter.Complete((T)topWorker.Current, null);
266274
yield break;
267275
}
268276
}

0 commit comments

Comments
 (0)