Then() public method

Add callbacks that will fire on certain task states. If the task has already completed or failed, the related callbacks will fire immediately. Returns the this task so that calls can be chained together.
public Then ( Action onFulfilled = null, Action onFailure = null, Action onProgress = null, System.Action onEnd = null ) : UnityTask
onFulfilled Action Callback that fires when the task succeeds
onFailure Action Callback that fires when the task fails
onProgress Action Callback that fires when the progress of the task changes
onEnd System.Action Callback that fires when the task succeeds or fails
return UnityTask
Exemplo n.º 1
0
    // Use this for initialization
    void Start()
    {
        var testEngine = new NUnitBasicTestEngine ("UnityTools");

        var testTask = new UnityTask( (task) => {
            testEngine.RunTests(new TestRunnerCallback(LogString, task));
        });

        testTask.Then(
            onFulfilled: (o) => LogString("All tests succeeded. UnityTools works on this device!"),
            onFailure: (ex) => LogString("Some tests failed. UnityTools does not work on this device.")
        );
    }
Exemplo n.º 2
0
        /// <summary>
        /// Combines multiple tasks of the same type into one callback and runs them sequentially. On failure the failure callback is called immediately
        /// and the result of the other tasks is discarded.
        /// </summary>
        /// <param name="tasks">A series of tasks to execute in sequential orders</param>
        private static UnityTask AllSequential(IDispatcher dispatcher, params Func <UnityTask>[] tasks)
        {
            if (tasks == null || tasks.Length == 0)
            {
                return(new UnityTask(dispatcher).Resolve());
            }

            UnityTask     combinedTask      = new UnityTask(dispatcher);
            List <Action> sequentialActions = new List <Action>();

            int activeTaskCount = tasks.Length;

            object[] taskResults = new object[activeTaskCount];

            for (int i = 0; i < tasks.Length; i++)
            {
                int taskCount = i;

                sequentialActions.Add(() =>
                {
                    UnityTask task = tasks[taskCount]();

                    task.Then(
                        onFulfilled: (object result) =>
                    {
                        taskResults[taskCount] = result;

                        if (taskCount == sequentialActions.Count - 1)
                        {
                            // Last task
                            combinedTask.Resolve(taskResults);
                        }
                        else
                        {
                            sequentialActions[taskCount + 1]();
                        }
                    },
                        onFailure: (Exception e) =>
                    {
                        combinedTask.Reject(e);
                    }
                        );
                });
            }

            sequentialActions[0]();

            return(combinedTask);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Combines multiple tasks of the same type into one callback and runs them simultaneously. On failure the failure callback is called immediately
        /// and the result of the other tasks is discarded.
        /// </summary>
        /// <param name="tasks">A series of tasks to execute in parallel</param>
        private static UnityTask All(IDispatcher dispatcher, params UnityTask[] tasks)
        {
            if (tasks == null || tasks.Length == 0)
            {
                return(new UnityTask(dispatcher).Resolve());
            }

            UnityTask combinedTask = new UnityTask(dispatcher);

            int activeTaskCount = tasks.Length;

            object[] taskResults     = new object[activeTaskCount];
            bool     tasksSucceeding = true;

            for (int i = 0; i < tasks.Length; i++)
            {
                UnityTask task      = tasks[i];
                int       taskCount = i;

                task.Then(
                    onFulfilled: (object result) =>
                {
                    if (!tasksSucceeding)
                    {
                        return;
                    }

                    taskResults[taskCount] = result;

                    activeTaskCount--;
                    if (activeTaskCount == 0)
                    {
                        combinedTask.Resolve(taskResults);
                    }
                },
                    onFailure: (Exception ex) =>
                {
                    // Only reject the task the first time
                    if (tasksSucceeding)
                    {
                        combinedTask.Reject(ex);     // Bail if one of the task errors out
                    }
                    tasksSucceeding = false;
                }
                    );
            }

            return(combinedTask);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Combines multiple tasks of the same type into one callback and runs them simultaneously. On failure the failure callback is called immediately
        /// and the result of the other tasks is discarded.
        /// </summary>
        /// <param name="tasks">A series of tasks to execute in paralell</param>
        public static UnityTask <T[]> All(params UnityTask <T>[] tasks)
        {
            // Cast the tasks down to the non generic version for code reuse
            UnityTask[] nonGenericTasks = tasks.Cast <UnityTask>().ToArray();

            // Call non generic version of All
            UnityTask       combinedTask = UnityTask.All(nonGenericTasks);
            UnityTask <T[]> outputTask   = new UnityTask <T[]>();

            // Funnel the non generic task into a generic one
            combinedTask.Then(
                onFulfilled: o => outputTask.Resolve((T)o),
                onFailure: ex => outputTask.Resolve(ex)
                );

            return(outputTask);
        }
Exemplo n.º 5
0
        public void TaskDoesResolve()
        {
            object value = null;
            object targetValue = new object();

            UnityTask t = new UnityTask();
            t.Then((o) => value = o);

            t.Resolve(targetValue);

            Assert.That(value == targetValue);
        }
Exemplo n.º 6
0
        public void TaskDoesNotify()
        {
            float value = 0f;
            float targetValue = 1f;

            UnityTask t = new UnityTask();
            t.Then(null, null, (p) => value = p);

            t.Notify(1f);

            Assert.That(Utils.Math.NearlyEqual(value, targetValue));
        }
Exemplo n.º 7
0
        public void TaskDoesFail()
        {
            Exception value = null;
            Exception targetValue = new Exception();

            UnityTask t = new UnityTask();
            t.Then(null, (ex) => value = ex);

            t.Reject(targetValue);

            Assert.That(value == targetValue);
        }
Exemplo n.º 8
0
        public void TaskDoesEndOnResolveAndReject()
        {
            bool value = false;
            bool targetValue = true;

            UnityTask t = new UnityTask();
            t.Then(onEnd: () => value = targetValue);

            t.Resolve(null);

            Assert.AreEqual(value, targetValue);

            value = false;

            t = new UnityTask();
            t.Then(onEnd: () => value = targetValue);

            t.Reject(null);

            Assert.AreEqual(value, targetValue);

            value = false;

            t = new UnityTask();
            t.Then(onEnd: () => value = targetValue);

            t.Notify(0f);

            Assert.AreNotEqual(value, targetValue);
        }
Exemplo n.º 9
0
        public void TaskCanOnlyResolveOnce()
        {
            UnityTask t = new UnityTask();
            t.Then((o) => Console.WriteLine("Task Fulfilled"));

            t.Resolve();
            t.Resolve();
        }
Exemplo n.º 10
0
        public void TaskCanOnlyRejectOnce()
        {
            UnityTask t = new UnityTask();
            t.Then(null, (o) => Console.WriteLine("Task Failed"));

            t.Reject(new Exception());
            t.Reject(new Exception());
        }