public void TestDebouncer() { var debouncer = new Debouncer(TimeSpan.FromSeconds(1)); var tempValue = 1; var testAction = new Action(() => { tempValue++; }); for (var i = 0; i < 5; i++) { Task.Run(() => { debouncer.AddAction(testAction); }); } Assert.IsTrue(tempValue == 1); }
public void TestDebouncerWithDelay() { var milliseconds = 100; var debouncer = new Debouncer(TimeSpan.FromMilliseconds(milliseconds)); var tempValue = 1; var testAction = new Action(() => { tempValue++; }); for (var i = 0; i < 5; i++) { Task.Delay(milliseconds).Wait(); Task.Run(() => { debouncer.AddAction(testAction); }); } Assert.IsTrue(tempValue == 5); }
public List <Result> Query(Query query) { var key = _settings.GoogleKey; List <Result> results = new List <Result>(); if (string.IsNullOrEmpty(query.Search)) { return(results); } string searchLowered = query.Search.ToLower(); var found = Times.Where(x => x.Country.ToLower().Contains(searchLowered) || x.Name.ToLower().Contains(searchLowered)); foreach (var el in found) { var time = DateTime.Now; time = time.AddSeconds(el.TimeChange); var place = el.Name; var value = $"Local mean time for {place} is {time}"; results.Add(new Result() { Title = value, SubTitle = "Copy to clipboard", Action = _ => { Clipboard.SetText(value); return(true); } }); } if (!string.IsNullOrEmpty(key)) { var getData = new Action(() => { var client = new RestClient("https://maps.googleapis.com"); var name = query.Search; var geoRequest = new RestRequest("maps/api/geocode/json"); geoRequest.AddQueryParameter("address", name); geoRequest.AddQueryParameter("key", key); IRestResponse <GeoOutput> response = client.Execute <GeoOutput>(geoRequest); //if not ok, nothing else to do if (response.StatusCode != HttpStatusCode.OK) { return; } foreach (var el in response.Data.Results) { var address = el.FormattedAddress; var location = el.Geometry.Location; var request = new RestRequest("maps/api/timezone/json?location={lat},{lng}&key={key}", Method.GET); request.AddUrlSegment("lat", location.Lat.ToString(CultureInfo.InvariantCulture)); request.AddUrlSegment("lng", location.Lng.ToString(CultureInfo.InvariantCulture)); request.AddUrlSegment("key", key); request.AddQueryParameter("timestamp", ((long)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds).ToString()); IRestResponse <TimeResult> oneTimeResult = client.Execute <TimeResult>(request); var timeResult = oneTimeResult.Data; if (oneTimeResult.StatusCode == HttpStatusCode.OK && timeResult.Status == "OK") { var time = DateTime.UtcNow.AddSeconds(timeResult.RawOffset + timeResult.DstOffset); var value = $"Local time for {address} is {time}"; var result = new Result() { Title = value, SubTitle = "Copy to clipboard", Action = e => { Clipboard.SetText(value); return(true); } }; results.Add(result); } } }); _debouncer.AddAction(getData); } return(results); }