Ejemplo n.º 1
0
        public static async Task <object> RequestAsync(AjaxOptions opts, Ajax ajax = null)
        {
            if (opts.Urls != null)
            {
                if (opts.IsMulticast)
                {
                    if (opts.IsMulticastWaitable)
                    {
                        return(await MulticastWaitResultAsync(opts));
                    }
                    else
                    {
                        MulticastUseAsync(opts, ajax);
                        return(null);
                    }
                }
                else
                {
                    Dictionary <string, Exception> exs = new Dictionary <string, Exception>();
                    foreach (var url in opts.Urls)
                    {
                        opts.url = url;
                        var  visit     = MakeVisitFromOptions(opts, ajax);
                        bool isSuccess = false;
                        var  rs        = await InternalRequestAsync(visit, (ret, v) => {
                            isSuccess = true;
                            if (opts.success != null)
                            {
                                opts.success(ret, v);
                            }
                        }, (ret, v) => {
                            exs.Add(url, ret);
                            return(true);
                        });

                        if (isSuccess)
                        {
                            return(rs);
                        }
                    }
                    var ex = new MultiUrlException(opts, exs);
                    if (opts.error != null)
                    {
                        if (!opts.error(ex, new Visit(opts, ajax)))
                        {
                            throw ex;
                        }
                        return(null);
                    }
                    else
                    {
                        throw ex;
                    }
                }
            }
            else
            {
                return(await InternalRequestAsync(MakeVisitFromOptions(opts, ajax), opts.success, opts.error));
            }
        }
Ejemplo n.º 2
0
 public static object Request(AjaxOptions opts, Ajax ajax = null)
 {
     if (opts.Urls != null)
     {
         if (opts.IsMulticast)
         {
             return(MulticastWaitResult(opts));
         }
         else
         {
             Dictionary <string, Exception> exs = new Dictionary <string, Exception>();
             bool reqSuccess = false;
             foreach (var url in opts.Urls)
             {
                 opts.url = url;
                 var visit  = MakeVisitFromOptions(opts, ajax);
                 var retObj = InternalRequest(visit, (re, v) =>
                 {
                     reqSuccess = true;
                     if (opts.success != null)
                     {
                         opts.success(re, v);
                     }
                 }, (re, v) =>
                 {
                     exs.Add(url, re);
                     return(true);
                 });
                 if (reqSuccess)
                 {
                     return(retObj);
                 }
             }
             var ex = new MultiUrlException(opts, exs);
             if (opts.error != null)
             {
                 if (opts.error(ex, new Visit(opts, ajax)))
                 {
                     throw ex;
                 }
                 return(null);
             }
             else
             {
                 throw ex;
             }
         }
     }
     else
     {
         return(InternalRequest(MakeVisitFromOptions(opts, ajax), opts.success, opts.error));
     }
 }