/// <summary> /// Creates an interceptor policy responded at a specific times. /// </summary> /// <param name="action">The action to register to execute.</param> /// <param name="count">The invoking count.</param> /// <param name="timeout">The time span between each invoking.</param> /// <returns>The action with the specific interceptor policy integration.</returns> /// <remarks> /// A handler to process at last only when request to call in the specific times range. /// A sample scenario is double click. /// </remarks> public static Func <T, Task> Times(Func <T, Task> action, int count, TimeSpan timeout) => Action(action, InterceptorPolicy.Times(count, timeout));
/// <summary> /// Creates an interceptor policy responded at a specific times. /// </summary> /// <param name="action">The action to register to execute.</param> /// <param name="min">The minmum invoking count.</param> /// <param name="max">The maxmum invoking count.</param> /// <param name="timeout">The time span between each invoking.</param> /// <returns>The action with the specific interceptor policy integration.</returns> /// <remarks> /// A handler to process at last only when request to call in the specific times range. /// A sample scenario is double click. /// </remarks> public static Func <T, Task> Times(Func <T, Task> action, int min, int?max, TimeSpan timeout) => Action(action, InterceptorPolicy.Times(min, max, timeout));
/// <summary> /// Creates an interceptor policy responded at a specific times. /// </summary> /// <param name="action">The action to register to execute.</param> /// <param name="count">The invoking count.</param> /// <param name="timeout">The time span between each invoking.</param> /// <returns>The action with the specific interceptor policy integration.</returns> /// <remarks> /// A handler to process at last only when request to call in the specific times range. /// A sample scenario is double click. /// </remarks> public static Action <T> Times(Action <T> action, int count, TimeSpan timeout) => Action(action, InterceptorPolicy.Times(count, timeout));
/// <summary> /// Creates an interceptor policy responded at a specific times. /// </summary> /// <param name="action">The action to register to execute.</param> /// <param name="min">The minmum invoking count.</param> /// <param name="max">The maxmum invoking count.</param> /// <param name="timeout">The time span between each invoking.</param> /// <returns>The action with the specific interceptor policy integration.</returns> /// <remarks> /// A handler to process at last only when request to call in the specific times range. /// A sample scenario is double click. /// </remarks> public static Action <T> Times(Action <T> action, int min, int?max, TimeSpan timeout) => Action(action, InterceptorPolicy.Times(min, max, timeout));
public async Task TestTimesHitsAsync() { var taskTokens = new List <Task>(); var result = string.Empty; var task = new Interceptor <string>( v => result = v, InterceptorPolicy.Times(2, 3, TimeSpan.FromMilliseconds(100)) ); Assert.IsFalse(task.IsWorking); _ = task.InvokeAsync("abc"); await Task.Delay(20); Assert.AreEqual(string.Empty, result); _ = task.InvokeAsync("defg"); Assert.IsTrue(task.IsWorking); await Task.Delay(20); Assert.AreEqual(string.Empty, result); _ = task.InvokeAsync("hijk"); await Task.Delay(20); Assert.AreEqual(string.Empty, result); _ = task.InvokeAsync("lmn"); await Task.Delay(20); Assert.AreEqual(string.Empty, result); await Task.Delay(90); Assert.AreEqual(string.Empty, result); _ = task.InvokeAsync("opq"); await Task.Delay(20); Assert.AreEqual(string.Empty, result); _ = task.InvokeAsync("rst"); await Task.Delay(20); Assert.AreEqual(string.Empty, result); await Task.Delay(90); if (string.IsNullOrEmpty(result)) { await Task.Delay(20); } Assert.AreEqual("rst", result); _ = task.InvokeAsync("uvw"); await Task.Delay(20); Assert.AreEqual("rst", result); _ = task.InvokeAsync("xyz"); await Task.Delay(140); if (result == "rst") { await Task.Delay(20); } Assert.AreEqual("xyz", result); await task.WaitAsync(); Assert.IsFalse(task.IsWorking); }
private async Task TestKeyFeaturesAsync() { Console.WriteLine("Interceptor testing."); Console.WriteLine("Debounce"); var task = new Interceptor <string>( v => value = v, InterceptorPolicy.Debounce(TimeSpan.FromMilliseconds(200)) ); _ = task.InvokeAsync("b"); await Task.Delay(100); _ = task.InvokeAsync("c"); await Task.Delay(100); _ = task.InvokeAsync("d"); await Task.Delay(100); _ = task.InvokeAsync("e"); Check("a"); await Task.Delay(300); Check("e"); await task.WaitAsync(); Check("e"); Console.WriteLine("Throttle"); task.ResetDuration(); task.Policy = InterceptorPolicy.Throttle(TimeSpan.FromMilliseconds(800)); _ = task.InvokeAsync("f"); await Task.Delay(100); _ = task.InvokeAsync("g"); await Task.Delay(100); _ = task.InvokeAsync("h"); await Task.Delay(100); _ = task.InvokeAsync("i"); await Task.Delay(100); Check("f"); await task.WaitAsync(); Check("f"); Console.WriteLine("Multiple"); task.ResetDuration(); task.Policy = InterceptorPolicy.Mutliple(2, 4, TimeSpan.FromMilliseconds(200)); _ = task.InvokeAsync("j"); Check("f"); await Task.Delay(100); _ = task.InvokeAsync("k"); Check("k"); await Task.Delay(100); _ = task.InvokeAsync("l"); await Task.Delay(100); _ = task.InvokeAsync("m"); await Task.Delay(100); Check("m"); _ = task.InvokeAsync("n"); await Task.Delay(100); Check("m"); await task.WaitAsync(); Check("m"); Console.WriteLine("Times 1"); task.ResetDuration(); task.Policy = InterceptorPolicy.Times(2, 3, TimeSpan.FromMilliseconds(200)); _ = task.InvokeAsync("o"); Check("m"); await Task.Delay(100); _ = task.InvokeAsync("p"); Check("m"); await Task.Delay(100); _ = task.InvokeAsync("q"); await Task.Delay(100); _ = task.InvokeAsync("r"); await Task.Delay(100); Check("m"); _ = task.InvokeAsync("s"); await Task.Delay(100); Check("m"); await task.WaitAsync(); Check("m"); Console.WriteLine("Times 2"); task.ResetDuration(); task.Policy = InterceptorPolicy.Times(2, 3, TimeSpan.FromMilliseconds(200)); _ = task.InvokeAsync("t"); Check("m"); await Task.Delay(100); _ = task.InvokeAsync("u"); Check("m"); await Task.Delay(100); _ = task.InvokeAsync("v"); await Task.Delay(100); Check("m"); await task.WaitAsync(); Check("v"); }