Beispiel #1
0
        public void Then(
            Delegate fulfilledHandler, Delegate errorHandler = null, Delegate progressHandler = null)
        {
            var req = new XMLHttpRequest();

            req.OnReadyStateChange = () => {
                if (req.ReadyState != AjaxReadyState.Done)
                {
                    return;
                }

                if (req.Status >= 200 && req.Status < 400)
                {
                    Logger.Debug(GetType(), "upload success");
                    fulfilledHandler?.Call(null, ResultHolder <XMLHttpRequest> .CreateSuccess(req));
                    return;
                }

                Logger.Debug(GetType(), "upload error");
                fulfilledHandler?.Call(
                    null, ResultHolder <XMLHttpRequest> .CreateFailure(req.ResponseText, null, req));
            };
            req.Open(_method, _url, true);

            req.SetRequestHeader("Cache-Control", "no-cache");
            req.SetRequestHeader("Pragma", "no-cache");

            var tzOffset = Script.Eval <string>("new Date().getTimezoneOffset() + \"\"");

            req.SetRequestHeader(Philadelphia.Common.Model.Magics.TimeZoneOffsetFieldName, tzOffset);

            try {
                var tzCode = Script.Eval <object>("Intl.DateTimeFormat().resolvedOptions().timeZone");
                if (Script.IsDefined(tzCode))
                {
                    req.SetRequestHeader(Philadelphia.Common.Model.Magics.TimeZoneCodeFieldName, tzCode.ToString());
                }
            } catch (Exception) {
                //most likely it is unsupported
                Logger.Error(GetType(), "could not determine timeZone");
            }

            if (CsrfToken != null)
            {
                req.SetRequestHeader(Philadelphia.Common.Model.Magics.CsrfTokenFieldName, CsrfToken);
            }

            if (_frmData != null)
            {
                req.Send(_frmData);
            }
            else
            {
                req.Send(_dataToPost);
            }
        }
        private async Task <ResultHolder <DataT[]> > FetchMatching()
        {
            var matchingValuesKey = Guid.NewGuid().ToString();

            _matchingValuesKey = matchingValuesKey;
            var par = _input.Value;

            await Task.Delay(_delayMilisec);

            var mayUse = _matchingValuesKey == matchingValuesKey;

            Logger.Debug(GetType(),
                         "autocomplete awaiting for result of input {0} requestCount={1} mayUse={2}",
                         par, _requestCount, mayUse);

            if (!mayUse)
            {
                return(ResultHolder <DataT[]> .CreateFailure("autocomplete[1] skipping due to new input"));
            }

            DataT[] result;

            try {
                result = await _matchingValuesProvider(MaxVisibleItems, par);
            } catch (Exception ex) {
                return(ResultHolder <DataT[]> .CreateFailure("autocomplete result not received", ex));
            }

            mayUse = _matchingValuesKey == matchingValuesKey;

            Logger.Debug(GetType(),
                         "autocomplete result received itemsCount={0} for input={1} currentInputIs={2} mayUse?={3}",
                         result.Length, par, _input.Value, mayUse);

            if (!mayUse)
            {
                return(ResultHolder <DataT[]> .CreateFailure("autocomplete[2] skipping due to new input"));
            }

            return(ResultHolder <DataT[]> .CreateSuccess(result));
        }