Skip to content

mmolosay/debounce

Repository files navigation

Maven metadata URL License Apache 2.0 kotlin

debounce

Debounce your lambdas.

code snippet

Table of Contents


What is debouncing?

In programming, "debouncing" refers to a technique used to prevent multiple triggering of an event or action due to rapid or repeated signals. Specifically, it is a process of filtering out unwanted, extraneous signals or noise from an input signal that can result in multiple events being triggered when only one is desired.

More here: article

Problems to solve

  • Prevent multiple clicks on a button from triggering the same action multiple times.
  • Delay the processing of text field input until the user has finished typing, to improve performance and prevent unwanted intermediate results, like in search queries.
  • Filter out noise and ensure that only valid sensor readings are used in decision-making processes.

Reasons to use

  • Convenient to use.
  • Small source code size.
  • 100% documented.
  • Covered in unit tests.

Installation

Using Gradle Kotlin DSL:

repositories {
    mavenCentral()
}
dependencies {
    implementation("io.github.mmolosay:debounce:VERSION")
}

You can find the most recent version at the top of this file in Maven badge.

What to use

The centerpieces of this library are two functions:

The main difference between them is the moment when debouncing timeout starts.

For debounced() timeout starts right after an execution of the action. It suits for cases without coroutines or other indefinitely long operations.

For DebounceStateIdentity.debounce() it is you who decide when to start a release timeout. It's a right choice for actions which launch coroutines or contain other indefinitely long operations.

Tip

Does your action contain coroutines or other indefinitely long operations?

NO → debounced()

YES → DebounceStateIdentity.debounce()

For examples of use see Examples section down below.

Migration

Some versions contain breaking changes. Check releases page for migration guide.

💡Examples

Examples can be found here.

License

Copyright 2023 Mikhail Malasai

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.