Last active
July 8, 2020 15:41
-
-
Save mburbea/001bae5f59faca2fea5bf69441a9957c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class Utils | |
{ | |
private static Func<T> Delay<T>(Func<Task, T> continuation, int ms, CancellationTokenSource? src = null) | |
where T : Task | |
{ | |
Interlocked.Exchange(ref src, new CancellationTokenSource())?.Cancel(); | |
return () => continuation(Task.Delay(ms, src!.Token)); | |
} | |
public static Func<Task<T>> Debounce<T>(Func<T> method, int ms = 250) | |
=> Delay(t => t.ContinueWith(_ => method, TaskContinuationOptions.OnlyOnRanToCompletion), ms); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class Utils | |
{ | |
public static Func<Task<T>> Debounce<T>(Func<T> method, int ms = 250, CancellationTokenSource? src = null) => delegate | |
{ | |
Interlocked.Exchange(ref src, new CancellationTokenSource())?.Cancel(); | |
return Task.Delay(ms, src!.Token).ContinueWith(_ => method(), TaskContinuationOptions.OnlyOnRanToCompletion); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
One last go of shit...