UniTaskMotionExtensions.cs
Runtime/Integrations/UniTask/UniTaskMotionExtensions.cs
1 using System;
2 using System.Threading;
3 using Cysharp.Threading.Tasks;
4
5 namespace MeatMotion
6 {
7 public static class UniTaskMotionExtensions
8 {
9 public static UniTask ToUniTask(this MotionHandle handle, CancellationToken cancellationToken = default)
10 => ToUniTask(handle, CancelBehavior.Cancel, true, cancellationToken);
11
12 public static UniTask ToUniTask(this MotionHandle handle, CancelBehavior cancelBehavior, CancellationToken cancellationToken = default)
13 => ToUniTask(handle, cancelBehavior, true, cancellationToken);
14
15 public static UniTask ToUniTask(this MotionHandle handle, CancelBehavior cancelBehavior, bool cancelAwaitOnMotionCanceled, CancellationToken cancellationToken = default)
16 {
17 var node = handle.Node;
18 if (node?.Active != true) return UniTask.CompletedTask;
19 return Wait.Create(node, handle, cancelBehavior, cancelAwaitOnMotionCanceled, cancellationToken);
20 }
21
22 #if UNITY_6000_7_OR_NEWER
23 [Unity.Scripting.LifecycleManagement.NoAutoStaticsCleanup] // A pool of finished waits.
24 #endif
25 sealed class Wait : MotionObserver, IUniTaskSource
26 {
27 [ThreadStatic] static Wait pool;
28 Wait next;
29 UniTaskCompletionSourceCore<AsyncUnit> core;
30 bool linked;
31 readonly Action<bool, CancellationToken> finish;
32
33 Wait() => finish = FinishWait;
34
35 internal static UniTask Create(MotionNode node, MotionHandle handle, CancelBehavior behavior, bool linked, CancellationToken token)
36 {
37 var source = pool ?? new Wait();
38 pool = source.next;
39 source.next = null;
40 source.linked = linked;
41 var task = new UniTask(source, source.core.Version);
42 if (token.CanBeCanceled) new MotionWait(source.finish).Start(handle, behavior, linked, token);
43 else source.Attach(node);
44 return task;
45 }
46
47 protected override void Signal(bool completed) => FinishWait(!completed && linked, default);
48
49 void FinishWait(bool canceled, CancellationToken token)
50 {
51 if (canceled) core.TrySetCanceled(token);
52 else core.TrySetResult(AsyncUnit.Default);
53 }
54
55 public UniTaskStatus GetStatus(short token) => core.GetStatus(token);
56 public UniTaskStatus UnsafeGetStatus() => core.UnsafeGetStatus();
57 public void OnCompleted(Action<object> continuation, object state, short token) => core.OnCompleted(continuation, state, token);
58
59 public void GetResult(short token)
60 {
61 if (core.GetStatus(token) == UniTaskStatus.Pending) throw new InvalidOperationException("Motion is still active.");
62 try { core.GetResult(token); }
63 finally
64 {
65 core.Reset();
66 next = pool;
67 pool = this;
68 }
69 }
70 }
71
72 public static MotionHandle BindToAsyncReactiveProperty(this FloatMotionBuilder builder, AsyncReactiveProperty<float> reactiveProperty)
73 => builder.AsMotionBuilder().BindToAsyncReactiveProperty(reactiveProperty);
74
75 public static MotionHandle BindToAsyncReactiveProperty<TValue, TOptions, TAdapter>(this MotionBuilder<TValue, TOptions, TAdapter> builder, AsyncReactiveProperty<TValue> reactiveProperty)
76 where TValue : unmanaged where TOptions : unmanaged, IMotionOptions where TAdapter : unmanaged, IMotionAdapter<TValue, TOptions>
77 => builder.Bind(reactiveProperty ?? throw new ArgumentNullException(nameof(reactiveProperty)), static (v, property) => property.Value = v);
78 }
79 }