debouncer

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 31, 2024 License: Apache-2.0 Imports: 2 Imported by: 5

README

go-debouncer

Simple thread safe debouncer in Go

Install

go get github.com/skydive-project/go-debouncer

Usage

package main

import (
        "fmt"
        "time"
        "github.com/skydive-project/go-debouncer"
)

func myCallback() {
        fmt.Printf("Let's do something\n")
}

func main() {
        // Creates and returns a new debouncer that will postpone
        // the execution of a function myCallback after 3 seconds
        // have elapsed since the last time it was invoked.
        debouncer := debouncer.New(time.Second, myCallback)

        // Start the debouncer
        debouncer.Start()

        // Trigger the execution of the callback
        debouncer.Call()

        // The following calls will not cause the callback to
        // be executed more than once
        for i:=0; i<10; i++ {
                debouncer.Call()
        }

        // Wait a bit to let the debouncer
        time.Sleep(3*time.Second)

        // Stop the debouncer
        debouncer.Stop()
}

License

This software is licensed under the Apache License, Version 2.0 (the "License"); you may not use this software 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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Debouncer

type Debouncer struct {
	// contains filtered or unexported fields
}

Debouncer defines a debouncer object

func New

func New(interval time.Duration, callback func()) *Debouncer

New returns a Debouncer calling the given callback after a certain amount of time.

func (*Debouncer) Call

func (d *Debouncer) Call()

Call calls the callback of the Debouncer object

func (*Debouncer) Start

func (d *Debouncer) Start()

Start the debouncer

func (*Debouncer) Stop

func (d *Debouncer) Stop()

Stop stops the debouncer

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL