utils

package
v0.1.11 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package utils provides small shared helpers for common value handling.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Default

func Default[T comparable](value, fallback T) T

Default returns fallback when value is the zero value for T.

func DefaultString

func DefaultString(value, fallback string) string

DefaultString returns fallback when value is empty.

Types

type DebouncedHandler

type DebouncedHandler[T any] func(ctx context.Context, value T) error

DebouncedHandler handles the final value that survives a debounce window.

type ErrorHandler

type ErrorHandler func(ctx context.Context, err error)

ErrorHandler observes handler errors without coupling callers to background execution.

type MergeFunc

type MergeFunc[T any] func(existing T, incoming T) T

MergeFunc merges an incoming value into the existing pending value. Return the merged result. If nil, the incoming value replaces the existing one.

type TrailingDebouncer

type TrailingDebouncer[T any] struct {
	// contains filtered or unexported fields
}

TrailingDebouncer keeps the latest call for each key and runs it after the window is quiet.

func NewTrailingDebouncer

func NewTrailingDebouncer[T any](window time.Duration, handler DebouncedHandler[T], onError ErrorHandler, merge MergeFunc[T]) (*TrailingDebouncer[T], error)

NewTrailingDebouncer creates a keyed trailing-edge debouncer. If merge is nil, Call replaces the pending value; otherwise it merges via merge(existing, incoming).

func (*TrailingDebouncer[T]) Call

func (d *TrailingDebouncer[T]) Call(ctx context.Context, key string, value T)

Call resets the key's debounce window and keeps value as the latest pending work. If a merge function is configured, the incoming value is merged into the existing pending value.

Jump to

Keyboard shortcuts

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