示例#1
0
 // Use this for initialization
 void Start()
 {
     btns      = transform.GetComponentsInChildren <Button>();
     responses = new ResponseUnit[btns.Length];
     for (int i = 0; i < responses.Length; ++i)
     {
         responses[i] = new ResponseUnit(this, btns[i]);
         btns[i].onClick.AddListener(responses[i].OnClickBtn);
     }
 }
示例#2
0
        private async Task <ResponseCollection <TResult> > ResponsesAsync <TRequest, TResult>(TRequest request,
                                                                                              bool isNoneRequest)
        {
            var context = _context.Value;

            var responseUnits = new List <ResponseUnit <TResult> >();

            if (!isNoneRequest)
            {
                var valRsp = await ValidateAsync <TRequest, TResult>(context, request);

                if (valRsp != null)
                {
                    responseUnits.Add(new ResponseUnit <TResult>
                    {
                        IsMainResponse = true,
                        Response       = Bolt.RequestBus.Response.Failed <TResult>(valRsp.Errors)
                    });

                    return(new ResponseCollection <TResult> {
                        Responses = responseUnits
                    });
                }
                ;
            }

            var applicableHandlers = _sp.GetServices <IResponseHandlerAsync <TRequest, TResult> >()
                                     .Where(x => x.IsApplicable(context, request)).ToArray();

            var firstBatchHandlers = applicableHandlers.Where(x
                                                              => x.ExecutionHint == ExecutionHint.Main || x.ExecutionHint == ExecutionHint.Independent);

            var firstBatchHandlerTasks = new List <Task <Response <TResult> > >();

            var mainHandlerIndex = -1;
            var index            = 0;

            foreach (var handler in firstBatchHandlers)
            {
                if (mainHandlerIndex == -1 && handler.ExecutionHint == ExecutionHint.Main)
                {
                    mainHandlerIndex = index;
                }

                firstBatchHandlerTasks.Add(ExecuteResponseHandler(context, handler, request));

                index++;
            }

            await Task.WhenAll(firstBatchHandlerTasks);

            index = 0;
            foreach (var batchHandler in firstBatchHandlerTasks)
            {
                var handlerRsp = batchHandler.Result;

                if (index == mainHandlerIndex)
                {
                    var rspUnit = new ResponseUnit <TResult>
                    {
                        IsMainResponse = true,
                        Response       = handlerRsp ?? Bolt.RequestBus.Response.Failed <TResult>()
                    };

                    responseUnits.Add(rspUnit);

                    if (!rspUnit.Response.IsSucceed)
                    {
                        return(new ResponseCollection <TResult> {
                            Responses = responseUnits
                        });
                    }
                }
                else
                {
                    if (handlerRsp != null)
                    {
                        responseUnits.Add(new ResponseUnit <TResult>
                        {
                            IsMainResponse = false,
                            Response       = handlerRsp
                        });
                    }
                }

                index++;
            }

            var otherHandlers = applicableHandlers
                                .Where(x => x.ExecutionHint == ExecutionHint.None);

            var otherHandlerTasks = new List <Task <Response <TResult> > >();

            foreach (var otherHandler in otherHandlers)
            {
                otherHandlerTasks.Add(ExecuteResponseHandler(context, otherHandler, request));
            }

            await Task.WhenAll(otherHandlerTasks);

            foreach (var task in otherHandlerTasks)
            {
                if (task.Result != null)
                {
                    responseUnits.Add(new ResponseUnit <TResult>()
                    {
                        Response       = task.Result,
                        IsMainResponse = false
                    });
                }
            }

            var rspCollection = new ResponseCollection <TResult>
            {
                Responses = responseUnits
            };

            var filters = _sp.GetServices <IResponseFilterAsync <TRequest, TResult> >();

            foreach (var filter in filters)
            {
                if (!filter.IsApplicable(context, request))
                {
                    continue;
                }

                await filter.Filter(context, request, rspCollection);
            }

            return(rspCollection);
        }