Git Product home page Git Product logo

debouncemonitoring's Introduction

LOGO

DebounceMonitoring

CI PLATFORM NuGet

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 to your .NET Standard 2.0 - compatible project

PM> Install-Package DebounceMonitoring

Usage

using DebounceMonitoring;

Debounce instance methods:

internal class ViewModel
{
    public void OnButtonClick()
    {
        if (this.DebounceHere()) return;

        // Handle the click
    }
}

snippet source

Debounce lambdas and local functions:

public Command ClickCommand { get; }

public ViewModel()
{
    ClickCommand = new Command(() =>
    {
        if (this.DebounceHere()) return;

        // Handle the click
    });
}

snippet source

Debounce static methods:

internal class Analytics
{
    public static void TrackEvent()
    {
        if (DebounceMonitor.DebounceHereStatic<Analytics>()) return;

        // Send the event
    }
}

snippet source

Rx Operator

This library also provides the simplest implementation of the debounce operator for Rx.NET (throttle in RxJs).

Example:

button.ClickAsObservable()
    .Debounce()
    .Subscribe(_ => OnButtonClick());

snippet source

Interval

The default debounce interval is 500 ms.
It can be specified as an argument:

this.DebounceHere(intervalMs: 1_000)

IObservable<T>.Debounce(intervalMs: 1_000)

or set globally:

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 or Fody.ModuleInit.

internal static class UnitTestGlobalSetup
{
    [System.Runtime.CompilerServices.ModuleInitializer]
    internal static void SetupDebounceMonitor() => DebounceMonitor.Disabled = true;
}

snippet source

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 file for details.

debouncemonitoring's People

Contributors

sidovsky avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.