use TaskCompletionSource

This commit is contained in:
TORISOUP
2018-04-21 23:18:02 +09:00
parent 68fcedc3f9
commit 6a459e3735
2 changed files with 21 additions and 70 deletions

View File

@@ -6,6 +6,9 @@ using UniGLTF;
using System.Collections.Generic;
using System.Collections;
using UniTask;
#if (NET_4_6 && UNITY_2018_1_OR_NEWER)
using System.Threading.Tasks;
#endif
namespace VRM
@@ -524,32 +527,26 @@ namespace VRM
}
#if (NET_4_6 && UNITY_2018_1_OR_NEWER)
/// <summary>
/// Gets an awaiter that returns the loaded GameObject.
/// </summary>
public static Schedulable<GameObject> LoadVrmAsync(string path)
public static Task<GameObject> LoadVrmAsync(string path)
{
var context = new VRMImporterContext(path);
context.ParseVrm(File.ReadAllBytes(path));
return LoadVrmAsyncInternal(context);
return LoadVrmAsyncInternal(context).ToTask();
}
/// <summary>
/// Gets an awaiter that returns the loaded GameObject.
/// </summary>
public static Schedulable<GameObject> LoadVrmAsync(Byte[] bytes)
public static Task<GameObject> LoadVrmAsync(Byte[] bytes)
{
var context = new VRMImporterContext();
context.ParseVrm(bytes);
return LoadVrmAsync(context);
}
/// <summary>
/// Gets an awaiter that returns the loaded GameObject.
/// </summary>
public static Schedulable<GameObject> LoadVrmAsync(VRMImporterContext ctx)
public static Task<GameObject> LoadVrmAsync(VRMImporterContext ctx)
{
return LoadVrmAsyncInternal(ctx);
return LoadVrmAsyncInternal(ctx).ToTask();
}
#endif

View File

@@ -2,8 +2,7 @@
using System.Collections;
using System.Collections.Generic;
#if (NET_4_6 && UNITY_2018_1_OR_NEWER)
using System.Threading;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
#endif
namespace UniTask
@@ -186,9 +185,16 @@ namespace UniTask
}
#if (NET_4_6 && UNITY_2018_1_OR_NEWER)
public static SchedulableAwaiter<T> GetAwaiter<T>(this Schedulable<T> schedulable)
public static Task<T> ToTask<T>(this Schedulable<T> schedulable)
{
return new SchedulableAwaiter<T>(schedulable, true);
return ToTask(schedulable, Scheduler.MainThread);
}
public static Task<T> ToTask<T>(this Schedulable<T> schedulable, IScheduler scheduler)
{
var tcs = new TaskCompletionSource<T>();
schedulable.Subscribe(scheduler, r => tcs.SetResult(r), ex => tcs.SetException(ex));
return tcs.Task;
}
#endif
@@ -233,57 +239,5 @@ namespace UniTask
}, scheduler);
}
*/
#if (NET_4_6 && UNITY_2018_1_OR_NEWER)
public class SchedulableAwaiter<T> : INotifyCompletion
{
private readonly Schedulable<T> target = null;
private T result;
private readonly bool continueOnCapturedContext;
public T GetResult()
{
return result;
}
public bool IsCompleted { get; private set; }
public SchedulableAwaiter(Schedulable<T> target)
{
this.target = target;
continueOnCapturedContext = true;
}
public SchedulableAwaiter(Schedulable<T> target, bool continueOnCapturedContext)
{
this.target = target;
this.continueOnCapturedContext = continueOnCapturedContext;
}
public void OnCompleted(Action continuation)
{
var context = SynchronizationContext.Current;
if (continueOnCapturedContext && context != null)
{
target.Subscribe(target.Parent.Schedulder, r =>
{
result = r;
IsCompleted = true;
context.Post(_ => continuation(), null);
}, ex => { throw ex; });
}
else
{
target.Subscribe(target.Parent.Schedulder, r =>
{
result = r;
IsCompleted = true;
continuation();
}, ex => { throw ex; });
}
}
}
#endif
}
}