Skip to content

Commit b869ac6

Browse files
committed
async extensions
1 parent 8b77051 commit b869ac6

3 files changed

Lines changed: 114 additions & 4 deletions

File tree

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
using System.Threading;
2+
using System.Threading.Tasks;
3+
using UnityEngine;
4+
5+
namespace CatCode.Commands
6+
{
7+
public static class AsyncCommandExtensions
8+
{
9+
public static Awaitable StartedToAwaitable(this ICommand command, CancellationToken token)
10+
{
11+
var tcs = new AwaitableCompletionSource();
12+
if (command.State > CommandState.Idle)
13+
{
14+
tcs.SetResult();
15+
}
16+
else
17+
{
18+
var cts = token.Register(OnCancel);
19+
command.Started += OnStarted;
20+
}
21+
return tcs.Awaitable;
22+
23+
void OnStarted()
24+
{
25+
command.Started -= OnStarted;
26+
tcs.TrySetResult();
27+
}
28+
29+
void OnCancel()
30+
{
31+
command.Started -= OnStarted;
32+
tcs.TrySetCanceled();
33+
}
34+
}
35+
36+
public static Awaitable FinishToAwaitable(this ICommand command, CancellationToken token)
37+
{
38+
var tcs = new AwaitableCompletionSource();
39+
if (command.State == CommandState.Finished)
40+
{
41+
tcs.SetResult();
42+
}
43+
else
44+
{
45+
var ctr = token.Register(OnCancel);
46+
command.Finished += OnFinished;
47+
}
48+
return tcs.Awaitable;
49+
50+
void OnFinished()
51+
{
52+
command.Finished -= OnFinished;
53+
tcs.TrySetResult();
54+
}
55+
void OnCancel()
56+
{
57+
command.Finished -= OnFinished;
58+
tcs.TrySetCanceled();
59+
}
60+
}
61+
62+
63+
public static Task StartToTask(this ICommand command, CancellationToken token)
64+
{
65+
if (command.State > CommandState.Idle)
66+
return Task.CompletedTask;
67+
68+
var tcs = new TaskCompletionSource<bool>();
69+
var ctr = token.Register(OnCancel);
70+
command.Started += OnStarted;
71+
return tcs.Task;
72+
73+
void OnStarted()
74+
{
75+
command.Started -= OnStarted;
76+
tcs.TrySetResult(true);
77+
}
78+
79+
void OnCancel()
80+
{
81+
command.Started -= OnStarted;
82+
tcs.TrySetCanceled();
83+
}
84+
}
85+
86+
public static Task FinishToTask(this ICommand command, CancellationToken token)
87+
{
88+
if (command.State == CommandState.Finished)
89+
return Task.CompletedTask;
90+
91+
var tcs = new TaskCompletionSource<bool>();
92+
var ctr = token.Register(OnCancel);
93+
command.Finished += OnFinished;
94+
return tcs.Task;
95+
96+
void OnFinished()
97+
{
98+
command.Finished -= OnFinished;
99+
tcs.TrySetResult(true);
100+
}
101+
void OnCancel()
102+
{
103+
command.Finished -= OnFinished;
104+
tcs.TrySetCanceled();
105+
}
106+
}
107+
}
108+
}

Assets/CatCode/Commands/AsyncCommandExtensions.cs.meta

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

Assets/CatCode/Commands/CommandState.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
{
33
public enum CommandState
44
{
5-
Idle,
6-
Executing,
7-
Stopped,
8-
Finished
5+
Idle = 0,
6+
Executing = 1,
7+
Stopped = 2,
8+
Finished = 3
99
}
1010
}

0 commit comments

Comments
 (0)