Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sidovsky/debouncemonitoring
📑 Add debounce logic for any method in a single line.
https://github.com/sidovsky/debouncemonitoring
click debounce maui mvvm rx throttle touch ui uwp winforms wpf xamarin
Last synced: about 2 months ago
JSON representation
📑 Add debounce logic for any method in a single line.
- Host: GitHub
- URL: https://github.com/sidovsky/debouncemonitoring
- Owner: SIDOVSKY
- License: mit
- Created: 2021-03-06T05:35:16.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2021-07-05T17:54:01.000Z (over 3 years ago)
- Last Synced: 2024-11-15T21:30:44.828Z (2 months ago)
- Topics: click, debounce, maui, mvvm, rx, throttle, touch, ui, uwp, winforms, wpf, xamarin
- Language: C#
- Homepage:
- Size: 163 KB
- Stars: 48
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
![LOGO](Assets/icon.png)
# DebounceMonitoring
[![CI](https://github.com/SIDOVSKY/DebounceMonitoring/actions/workflows/ci.yml/badge.svg)](https://github.com/SIDOVSKY/DebounceMonitoring/actions/workflows/ci.yml)
[![PLATFORM](https://img.shields.io/badge/platform-.NET%20Standard%202.0-lightgrey)](#DebounceMonitoring)
[![NuGet](https://img.shields.io/nuget/v/DebounceMonitoring?logo=nuget)](https://www.nuget.org/packages/DebounceMonitoring/)Extensions to filter out repeated function calls caused by false or accidental clicks or touches.
* One-line integration
* Inlined, no method wrapping
* Shareable between multiple platforms
* Automated testing friendly## Installing
Add [NuGet package](https://www.nuget.org/packages/DebounceMonitoring) to your [.NET Standard 2.0 - compatible](https://github.com/dotnet/standard/blob/master/docs/versions/netstandard2.0.md#platform-support) project
```
PM> Install-Package DebounceMonitoring
```## Usage
```csharp
using DebounceMonitoring;
```#### Debounce **instance** methods:
```csharp
internal class ViewModel
{
public void OnButtonClick()
{
if (this.DebounceHere()) return;// Handle the click
}
}
```
[snippet source](/DebounceMonitoring.Tests/Snippets/Sample.cs#L9-L16)#### Debounce **lambdas** and **local functions**:
```csharp
public Command ClickCommand { get; }public ViewModel()
{
ClickCommand = new Command(() =>
{
if (this.DebounceHere()) return;// Handle the click
});
}
```
[snippet source](/DebounceMonitoring.Tests/Snippets/Sample.cs#L18-L28)#### Debounce **static** methods:
```csharp
internal class Analytics
{
public static void TrackEvent()
{
if (DebounceMonitor.DebounceHereStatic()) return;// Send the event
}
}
```
[snippet source](/DebounceMonitoring.Tests/Snippets/Sample.cs#L52-L61)### Rx Operator
This library also provides the simplest implementation of the debounce operator for [Rx.NET](https://github.com/dotnet/reactive) ([`throttle`](https://rxmarbles.com/#throttle) in RxJs).
Example:
```csharp
button.ClickAsObservable()
.Debounce()
.Subscribe(_ => OnButtonClick());
```
[snippet source](/DebounceMonitoring.Tests/Snippets/Sample.cs#L32-L34)### Interval
The default debounce interval is 500 ms.
It can be specified as an argument:```csharp
this.DebounceHere(intervalMs: 1_000)IObservable.Debounce(intervalMs: 1_000)
```
or set globally:```csharp
DebounceMonitor.DefaultInterval = TimeSpan.FromSeconds(5);
```### Disable (for automated testing)
The `DebounceMonitor` can be disabled in your base `TestFixture.Setup` or globally in `ModuleInitializer` with [ModuleInitializerAttribute](https://docs.microsoft.com/dotnet/api/system.runtime.compilerservices.moduleinitializerattribute) or [Fody.ModuleInit](https://github.com/Fody/ModuleInit).
```csharp
internal static class UnitTestGlobalSetup
{
[System.Runtime.CompilerServices.ModuleInitializer]
internal static void SetupDebounceMonitor() => DebounceMonitor.Disabled = true;
}
```
[snippet source](/DebounceMonitoring.Tests/Snippets/Sample.cs#L63-L67)## How does it work?
When `this.DebounceHere` is called, the call time is mapped to its location (method name + line number) and target (`this` in this case).
On the next call, the time is compared to the stored one. If the `interval` has not yet passed, then the call is meant to be debounced.
The debounce target (reference) is held weakly, so no memory leaks are caused.
## License
This project is licensed under the MIT license - see the [LICENSE](LICENSE) file for details.