示例#1
0
        public static async Task ActionFunctionCalledWhenExecuted()
        {
            bool wasCalled = false;
            var  task      = new AsyncActionTask(
                () =>
            {
                wasCalled = true;
                return(Task.FromResult(wasCalled));
            });

            var context = Substitute.For <ITaskContext>();

            await task.ExecuteAsync(context);

            wasCalled.Should().BeTrue();
        }
示例#2
0
        public static async Task ActionFunctionCalledWhenExecuted()
        {
            bool wasCalled = false;
            var task = new AsyncActionTask(
                () => 
                {
                    wasCalled = true;
                    return Task.FromResult(wasCalled);
                });

            var context = Substitute.For<ITaskContext>();

            await task.ExecuteAsync(context);

            wasCalled.Should().BeTrue();
        }
示例#3
0
        public void RequestWithMTokensAsync(Component caller, Action <IPresenterResponse> onResponse, string url, params MToken[] args)
        {
            PresenterActionInfo action;

            if (!_actionMapping.TryGetValue(url, out action))
            {
                Debug.LogErrorFormat("Can not find requested url : {0} ", url);
                onResponse(new PresenterResponse()
                {
                    StatusCode   = 404,
                    ErrorMessage = "Can not find requested url : " + url
                });
                return;
            }

            var parameters = action.Method.GetParameters();

            if (parameters.Length - 1 != args.Length)
            {
                onResponse(new PresenterResponse()
                {
                    StatusCode   = 500,
                    ErrorMessage = string.Format("request parameter count {0} is not equal with action method parameter count {1}",
                                                 parameters.Length, args.Length)
                });
                return;
            }
            var lastParameter = parameters[parameters.Length - 1];

            if (lastParameter.ParameterType != typeof(AsyncReturn) &&
                !lastParameter.ParameterType.IsSubclassOf(typeof(AsyncReturn)))
            {
                onResponse(new PresenterResponse()
                {
                    StatusCode   = 500,
                    ErrorMessage = string.Format("for async action, the last parameter should be AsyncReturn or its sub class. current is {0}", lastParameter.ParameterType),
                });
                return;
            }

            try
            {
                var pList = new List <object>();
                for (var i = 0; i < args.Length; i++)
                {
                    var parameterType = parameters[i].ParameterType;
                    if (parameterType != typeof(MToken) && !parameterType.IsSubclassOf(typeof(MToken)))
                    {
                        var temp = args[i].ToObject(parameterType);
                        pList.Add(temp);
                    }
                    else
                    {
                        pList.Add(args[i]);
                    }
                }

                var task = new AsyncActionTask();
                task.ActionInfo = action;
                task.CallLinker = caller;
                task.CallerName = caller.name;
                task.OnResponse = onResponse;
                _asyncActionTasks.Add(task);

                var asyncReturn = Activator.CreateInstance(lastParameter.ParameterType) as AsyncReturn;
                pList.Add(asyncReturn);

                asyncReturn.CallbackMethod = (returnArgs) =>
                {
                    lock (_asyncActionTasks)
                    {
                        task.Completed = true;
                        task.Data      = returnArgs;
                    }
                };

                action.Method.Invoke(null, pList.ToArray());
            }
            catch (Exception e)
            {
                Debug.LogException(e);
                onResponse(new PresenterResponse()
                {
                    StatusCode   = 500,
                    ErrorMessage = e.Message
                });
            }
        }