Exemplo n.º 1
0
        /// <summary>
        ///   创建一个在目标 <see cref="T:UnityEngine.TaskExtension.UnityTask" /> 成功执行时异步执行的延续任务。
        /// </summary>
        /// <param name="task">目标<see cref="T:UnityEngine.TaskExtension.UnityTask" /></param>
        /// <param name="continuation">
        ///   在 <see cref="T:UnityEngine.TaskExtension.UnityTask" /> 完成时要运行的操作。
        ///    在运行时,委托将作为一个参数传递给完成的任务。
        /// </param>
        /// <returns>
        ///   一个新的延续 <see cref="T:UnityEngine.TaskExtension.UnityTask" />。
        /// </returns>
        /// <exception cref="T:System.ArgumentNullException">
        ///   <paramref name="continuation" /> 参数为 <see langword="null" />。
        /// </exception>
        public static UnityTask OnSuccess(this UnityTask task, Action <UnityTask> continuation)
        {
            UnityTaskCompletionSource <object> uTcs = new UnityTaskCompletionSource <object>();

            task.ContinueWith(t =>
            {
                if (t.IsFaulted)
                {
                    uTcs.TrySetException(t.Exception);
                }
                else if (t.IsCanceled)
                {
                    uTcs.TrySetCanceled();
                }
                else
                {
                    try
                    {
                        continuation(t);
                        uTcs.TrySetResult(null);
                    }
                    catch (Exception e)
                    {
                        Debug.LogError(e);
                        uTcs.TrySetException(e);
                    }
                }
            });
            return(uTcs.Task);
        }
Exemplo n.º 2
0
        /// <summary>
        ///   创建一个在目标 <see cref="T:UnityEngine.TaskExtension.UnityTask`1" /> 成功执行时异步执行的延续任务。
        /// </summary>
        /// <typeparam name="TResult">该任务的结果的类型。</typeparam>
        /// <param name="task">目标<see cref="T:UnityEngine.TaskExtension.UnityTask`1" /></param>
        /// <param name="continuation">
        ///   在 <see cref="T:UnityEngine.TaskExtension.UnityTask`1" /> 完成时要运行的操作。
        ///    在运行时,委托将作为一个参数传递给完成的任务。
        /// </param>
        /// <returns>
        ///   一个新的延续 <see cref="T:UnityEngine.TaskExtension.UnityTask`1" />。
        /// </returns>
        /// <exception cref="T:System.ArgumentNullException">
        ///   <paramref name="continuation" /> 参数为 <see langword="null" />。
        /// </exception>
        public static UnityTask <TResult> OnSuccess <TResult>(this UnityTask task, Func <UnityTask, TResult> continuation)
        {
            UnityTaskCompletionSource <TResult> uTcs = new UnityTaskCompletionSource <TResult>();

            task.ContinueWith(t =>
            {
                if (t.IsFaulted)
                {
                    uTcs.TrySetException(t.Exception);
                }
                else if (t.IsCanceled)
                {
                    uTcs.TrySetCanceled();
                }
                else
                {
                    try
                    {
                        TResult result = continuation(t);
                        uTcs.TrySetResult(result);
                    }
                    catch (Exception e)
                    {
                        Debug.LogError(e);
                        uTcs.TrySetException(e);
                    }
                }
            });
            return(uTcs.Task);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Unwraps a nested task, producing a task that is complete when both the outer and inner tasks are complete. This is primarily useful for chaining asynchronous operations together.
        /// </summary>
        /// <param name="p_task">The task to unwrap.</param>
        /// <returns>A new task that is complete when both the outer and inner tasks are complete.</returns>
        public static UnityTask Unwrap(this UnityTask <UnityTask> p_task)
        {
            var tcs = new UnityTaskCompletionSource <object>();

            p_task.ContinueWith(t =>
            {
                if (t.IsFaulted)
                {
                    tcs.TrySetException(t.Exception);
                }
                else if (t.IsCanceled)
                {
                    tcs.TrySetCanceled();
                }
                else
                {
                    p_task.Result.ContinueWith(inner =>
                    {
                        if (inner.IsFaulted)
                        {
                            tcs.TrySetException(inner.Exception);
                        }
                        else if (inner.IsCanceled)
                        {
                            tcs.TrySetCanceled();
                        }
                        else
                        {
                            tcs.TrySetResult(null);
                        }
                    });
                }
            });
            return(tcs.Task);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Unwraps a nested task, producing a task that is complete when both the outer
        /// and inner tasks are complete and that has the inner task's result.
        /// This is primarily useful for chaining asynchronous operations together.
        /// </summary>
        /// <param name="task">The task to unwrap.</param>
        /// <returns>A new task that is complete when both the outer and inner tasks
        /// are complete and that has the inner task's result.</returns>
        public static UnityTask <T> Unwrap <T>(this UnityTask <UnityTask <T> > task)
        {
            var tcs = new UnityTaskCompletionSource <T>();

            task.ContinueWith((UnityTask <UnityTask <T> > t) =>
            {
                if (t.IsFaulted)
                {
                    tcs.TrySetException(t.Exception);
                }
                else if (t.IsCanceled)
                {
                    tcs.TrySetCanceled();
                }
                else
                {
                    t.Result.ContinueWith(delegate(UnityTask <T> inner)
                    {
                        if (inner.IsFaulted)
                        {
                            tcs.TrySetException(inner.Exception);
                        }
                        else if (inner.IsCanceled)
                        {
                            tcs.TrySetCanceled();
                        }
                        else
                        {
                            tcs.TrySetResult(inner.Result);
                        }
                    });
                }
            });
            return(tcs.Task);
        }
Exemplo n.º 5
0
        /// <summary>
        ///   创建一个在目标 <see cref="T:UnityEngine.TaskExtension.UnityTask`1" /> 成功执行时异步执行的<see cref="T:System.Threading.Tasks.Task`1" />延续任务。
        /// </summary>
        /// <param name="task">目标<see cref="T:UnityEngine.TaskExtension.UnityTask`1" /></param>
        /// <param name="continuation">
        ///   在 <see cref="T:UnityEngine.TaskExtension.UnityTask`1" /> 完成时要运行的操作。
        ///    在运行时,委托将作为一个参数传递给完成的任务。
        /// </param>
        /// <param name="cancellationToken">将指派给新的延续任务的 <see cref="P:System.Threading.Tasks.TaskFactory.CancellationToken" />。</param>
        /// <returns>
        ///   一个新的延续 <see cref="T:System.Threading.Tasks.Task" />。
        /// </returns>
        /// <exception cref="T:System.ArgumentNullException">
        ///   <paramref name="continuation" /> 参数为 <see langword="null" />。
        /// </exception>
        public static Task <TResult> ContinueToBackground <TIn, TResult>(this UnityTask <TIn> task,
                                                                         Func <UnityTask <TIn>, TResult> continuation, CancellationToken cancellationToken)
        {
            TaskCompletionSource <TResult> tcs = new TaskCompletionSource <TResult>();

            task.ContinueWith(p =>
            {
                if (p.IsFaulted || p.IsCanceled)
                {
                    tcs.TrySetException(p.Exception);
                }
                else
                {
                    Task.Run(() => continuation(p), cancellationToken).ContinueWith(t =>
                    {
                        if (t.IsFaulted || t.IsCanceled)
                        {
                            tcs.TrySetException(t.Exception ?? t.DefaultException());
                        }
                        else
                        {
                            tcs.TrySetResult(t.Result);
                        }
                    }, cancellationToken);
                }
            });
            return(tcs.Task);
        }
Exemplo n.º 6
0
        /// <summary>
        ///   创建一个在目标 <see cref="T:UnityEngine.TaskExtension.UnityTask" /> 成功执行时异步执行的<see cref="T:System.Threading.Tasks.Task" />延续任务。
        /// </summary>
        /// <param name="task">目标<see cref="T:UnityEngine.TaskExtension.UnityTask" /></param>
        /// <param name="continuation">
        ///   在 <see cref="T:UnityEngine.TaskExtension.UnityTask" /> 完成时要运行的操作。
        ///    在运行时,委托将作为一个参数传递给完成的任务。
        /// </param>
        /// <param name="cancellationToken">将指派给新的延续任务的 <see cref="P:System.Threading.Tasks.TaskFactory.CancellationToken" />。</param>
        /// <returns>
        ///   一个新的延续 <see cref="T:System.Threading.Tasks.Task" />。
        /// </returns>
        /// <exception cref="T:System.ArgumentNullException">
        ///   <paramref name="continuation" /> 参数为 <see langword="null" />。
        /// </exception>
        public static Task ContinueToBackground(this UnityTask task, Action <UnityTask> continuation,
                                                CancellationToken cancellationToken)
        {
            TaskCompletionSource <int> tcs = new TaskCompletionSource <int>();

            task.ContinueWith(t =>
            {
                if (t.IsFaulted || t.IsCanceled)
                {
                    tcs.TrySetException(t.Exception);
                }
                else
                {
                    Task.Run(() => { continuation(t); }, cancellationToken)
                    .ContinueWith(tt =>
                    {
                        if (tt.IsFaulted || tt.IsCanceled)
                        {
                            tcs.TrySetException(tt.Exception ?? tt.DefaultException());
                        }
                        else
                        {
                            tcs.TrySetResult(0);
                        }
                    }, cancellationToken);
                }
            });
            return(tcs.Task);
        }
Exemplo n.º 7
0
        /// <summary>
        /// OnSuccess
        /// </summary>
        /// <param name="p_task"></param>
        /// <param name="p_continuation"></param>
        /// <returns></returns>
        public static UnityTask OnSuccess(this UnityTask p_task, Action <UnityTask> p_continuation)
        {
            UnityTaskCompletionSource <object> utcs = new UnityTaskCompletionSource <object>();

            p_task.ContinueWith(t =>
            {
                if (!t.IsFaulted && !t.IsCanceled)
                {
                    p_continuation(t);
                    utcs.TrySetResult(null);
                }
                else
                {
                    utcs.TrySetException(t.Exception);
                }
            });
            return(utcs.Task);
        }
Exemplo n.º 8
0
        /// <summary>
        /// OnSuccess
        /// </summary>
        /// <param name="p_task"></param>
        /// <param name="p_continuation"></param>
        /// <returns></returns>
        public static UnityTask <TResult> OnSuccess <TResult>(this UnityTask p_task, Func <UnityTask, TResult> p_continuation)
        {
            UnityTaskCompletionSource <TResult> utcs = new UnityTaskCompletionSource <TResult>();

            p_task.ContinueWith(t =>
            {
                if (!t.IsFaulted && !t.IsCanceled)
                {
                    TResult result = p_continuation(t);
                    utcs.TrySetResult(result);
                }
                else
                {
                    utcs.TrySetException(t.Exception);
                }
            });

            return(utcs.Task);
        }
Exemplo n.º 9
0
        /// <summary>
        ///   创建一个在目标 <see cref="T:UnityEngine.TaskExtension.UnityTask`1" /> 成功执行时异步执行的<see cref="T:System.Threading.Tasks.Task" />延续任务。
        /// </summary>
        /// <param name="task">目标<see cref="T:UnityEngine.TaskExtension.UnityTask`1" /></param>
        /// <param name="continuation">
        ///   在 <see cref="T:UnityEngine.TaskExtension.UnityTask`1" /> 完成时要运行的操作。
        ///    在运行时,委托将作为一个参数传递给完成的任务。
        /// </param>
        /// <param name="cancellationToken">将指派给新的延续任务的 <see cref="P:System.Threading.Tasks.TaskFactory.CancellationToken" />。</param>
        /// <returns>
        ///   一个新的延续 <see cref="T:System.Threading.Tasks.Task" />。
        /// </returns>
        /// <exception cref="T:System.ArgumentNullException">
        ///   <paramref name="continuation" /> 参数为 <see langword="null" />。
        /// </exception>
        public static Task ContinueToBackground <T>(this UnityTask <T> task, Action <UnityTask <T> > continuation,
                                                    CancellationToken cancellationToken)
        {
            TaskCompletionSource <object> tcs = new TaskCompletionSource <object>();

            task.ContinueWith(p =>
            {
                if (p.IsFaulted)
                {
                    Debug.LogError(p.Exception);
                    tcs.TrySetException(p.Exception);
                }
                else if (p.IsCanceled)
                {
                    tcs.TrySetCanceled();
                }
                else
                {
                    Task.Run(() => continuation(p), cancellationToken).ContinueWith(t =>
                    {
                        if (t.IsFaulted)
                        {
                            Debug.LogError(t.Exception);
                            tcs.TrySetException(t.Exception ?? t.DefaultException());
                        }
                        else if (t.IsCanceled)
                        {
                            tcs.TrySetCanceled();
                        }
                        else
                        {
                            tcs.TrySetResult(0);
                        }
                    }, cancellationToken);
                }
            });
            return(tcs.Task);
        }
Exemplo n.º 10
0
        /// <summary>
        ///   创建一个在目标 <see cref="T:UnityEngine.TaskExtension.UnityTask" /> 成功执行时异步执行的<see cref="T:System.Threading.Tasks.Task`1" />延续任务。
        /// </summary>
        /// <param name="task">目标<see cref="T:UnityEngine.TaskExtension.UnityTask" /></param>
        /// <param name="continuation">
        ///   在 <see cref="T:UnityEngine.TaskExtension.UnityTask" /> 完成时要运行的操作。
        ///    在运行时,委托将作为一个参数传递给完成的任务。
        /// </param>
        /// <param name="cancellationToken">将指派给新的延续任务的 <see cref="P:System.Threading.Tasks.TaskFactory.CancellationToken" />。</param>
        /// <returns>
        ///   一个新的延续 <see cref="T:System.Threading.Tasks.Task" />。
        /// </returns>
        /// <exception cref="T:System.ArgumentNullException">
        ///   <paramref name="continuation" /> 参数为 <see langword="null" />。
        /// </exception>
        public static Task <TResult> ContinueToBackground <TResult>(this UnityTask task,
                                                                    Func <UnityTask, TResult> continuation, CancellationToken cancellationToken)
        {
            TaskCompletionSource <TResult> tcs = new TaskCompletionSource <TResult>();

            task.ContinueWith(t =>
            {
                if (t.IsFaulted)
                {
                    Debug.LogError(t.Exception);
                    tcs.TrySetException(t.Exception);
                }
                else if (t.IsCanceled)
                {
                    tcs.TrySetCanceled();
                }
                else
                {
                    Task.Run(() => continuation(t), cancellationToken).ContinueWith(a =>
                    {
                        if (a.IsFaulted)
                        {
                            Debug.LogError(a.Exception);
                            tcs.TrySetException(a.Exception ?? a.DefaultException());
                        }
                        else if (a.IsCanceled)
                        {
                            tcs.TrySetCanceled();
                        }
                        else
                        {
                            tcs.TrySetResult(a.Result);
                        }
                    }, cancellationToken);
                }
            });
            return(tcs.Task);
        }
Exemplo n.º 11
0
        /// <summary>
        /// switch to backgroud processor, thread pool
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="p_task"></param>
        /// <param name="p_continuation"></param>
        /// <returns></returns>
        public static Task <TResult> ContinueToBackground <T, TResult>(this UnityTask <T> p_task, Func <UnityTask <T>, TResult> p_continuation, CancellationToken p_cancellationToken)
        {
            TaskCompletionSource <TResult> tcs = new TaskCompletionSource <TResult>();
            var cancellation = p_cancellationToken.Register(() => tcs.TrySetCanceled());

            p_task.ContinueWith(t =>
            {
                TaskScheduler.FromCurrentSynchronizationContext().Post(() =>
                {
                    try
                    {
                        tcs.SetResult(p_continuation(t));
                        cancellation.Dispose();
                    }
                    catch (Exception e)
                    {
                        tcs.SetException(e);
                        cancellation.Dispose();
                    }
                });
            });
            return(tcs.Task);
        }
Exemplo n.º 12
0
        /// <summary>
        ///   创建一个代理 <see cref="T:UnityEngine.TaskExtension.UnityTask`1" /> 表示异步操作的Task
        /// </summary>
        /// <param name="task">
        ///   进行解包的任务。
        /// </param>
        /// <typeparam name="T">该任务的结果的类型。</typeparam>
        /// <returns>
        ///   一个 <see cref="T:UnityEngine.TaskExtension.UnityTask`1" /> 表示所提供的异步操作Task
        /// </returns>
        /// <exception cref="T:System.ArgumentNullException">
        ///   如果引发的异常 <paramref name="task" /> 参数为 null。
        /// </exception>
        public static UnityTask <T> Unwrap <T>(this UnityTask <UnityTask <T> > task)
        {
            var uTcs = new UnityTaskCompletionSource <T>();

            task.ContinueWith(t =>
            {
                if (t.IsFaulted)
                {
                    Debug.LogError(t.Exception);
                    uTcs.TrySetException(t.Exception);
                }
                else if (t.IsCanceled)
                {
                    uTcs.TrySetCanceled();
                }
                else
                {
                    t.Result.ContinueWith(inner =>
                    {
                        if (inner.IsFaulted)
                        {
                            Debug.LogError(inner.Exception);
                            uTcs.TrySetException(inner.Exception);
                        }
                        else if (inner.IsCanceled)
                        {
                            uTcs.TrySetCanceled();
                        }
                        else
                        {
                            uTcs.TrySetResult(inner.Result);
                        }
                    });
                }
            });
            return(uTcs.Task);
        }