fn

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: May 2, 2025 License: Apache-2.0 Imports: 4 Imported by: 8

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Future

type Future[T any] func() T

Future is a basic function for providing a fun-style function type for a function object that will produce an object of the specified type.

func AsFuture

func AsFuture[T any](in T) Future[T]

AsFuture wraps a value and returns a future object that, when called, will return the provided value.

func MakeFuture

func MakeFuture[T any](f func() T) Future[T]

MakeFuture constructs a Future[T] object from a function object.

func Translate

func Translate[T any, O any](in Future[T], tfn func(T) O) Future[O]

Translate converts a future from one type to another.

func (Future[T]) If

func (f Future[T]) If(cond bool) Future[T]

If produces a future that only runs when the condition value is true. If the condition is false, the future will return the zero value of T.

func (Future[T]) Ignore

func (f Future[T]) Ignore() func()

Ignore produces a function that will call the Future but discards the output.

func (Future[T]) Join

func (f Future[T]) Join(merge func(T, T) T, ops ...Future[T]) Future[T]

Join iteratively merges a collection of future operations.

If any of the futures are nil, the zero value for T is used. The merge function MUST NOT be nil.

func (Future[T]) Limit

func (f Future[T]) Limit(in int) Future[T]

func (Future[T]) Lock

func (f Future[T]) Lock() Future[T]

Locked returns a wrapped future that ensure that all calls to the future are protected by a mutex.

func (Future[T]) Not

func (f Future[T]) Not(cond bool) Future[T]

Not produces that only runs when the condition value is false. If the condition is true, the future will return the zero value of T.

func (Future[T]) Once

func (f Future[T]) Once() Future[T]

Once returns a future that will only run the underlying future exactly once.

func (Future[T]) PostHook

func (f Future[T]) PostHook(fn func()) Future[T]

PostHook unconditionally runs the provided function after running the future. The hook runs in a defer statement.

func (Future[T]) PreHook

func (f Future[T]) PreHook(fn func()) Future[T]

PreHook unconditionally runs the provided function before running and returning the function.

func (Future[T]) RecoverPanic

func (f Future[T]) RecoverPanic() (T, error)

func (Future[T]) Reduce

func (f Future[T]) Reduce(merge func(T, T) T, next Future[T]) Future[T]

Reduce takes the input future, the next future, and merges the results using the merge function.

func (Future[T]) Resolve

func (f Future[T]) Resolve() T

Resolve executes the future and returns its value.

func (Future[T]) Safe

func (f Future[T]) Safe() func() (T, error)

func (Future[T]) Slice

func (f Future[T]) Slice() func() []T

Slice returns a future-like function that wraps the output of the future as the first element in a slice.

func (Future[T]) TTL

func (f Future[T]) TTL(dur time.Duration) Future[T]

func (Future[T]) When

func (f Future[T]) When(c func() bool) Future[T]

When produces a new future wrapping the input future that executes when the condition function returns true, returning the zero value for T when the condition is false. The condition value is checked every time the future is called.

func (Future[T]) WithLock

func (f Future[T]) WithLock(m *sync.Mutex) Future[T]

WithLock return a future that is protected with the provided mutex.

func (Future[T]) WithLocker

func (f Future[T]) WithLocker(m sync.Locker) Future[T]

WithLocker return a future that is protected with the provided sync.Locker implementation.

type Handler

type Handler[T any] func(T)

Handler describes a function that operates on a single object, but returns no output, and is used primarily for side effects, particularly around handling errors or collecting metrics. The Handler implementation here makes it possible to provide panic-safety for these kinds of functions or easily convert to other related types.

func JoinHandlers

func JoinHandlers[T any](ops []Handler[T]) Handler[T]

JoinHandlers returns a function that combines a collection of handlers.

func NewHandler

func NewHandler[T any](in func(T)) Handler[T]

NewHandler produces an Handler[T] function as a helper.

func (Handler[T]) All

func (of Handler[T]) All(in ...T)

All processes all inputs with the specified handler.

func (Handler[T]) Capture

func (of Handler[T]) Capture(in T) func()

Capture returns a function that handles the specified value, but only when executed later.

func (Handler[T]) Chain

func (of Handler[T]) Chain(chain ...Handler[T]) Handler[T]

Chain calls the base handler, and then calls every handler in the chain.

func (Handler[T]) Filter

func (of Handler[T]) Filter(filter func(T) T) Handler[T]

Filter creates an handler that only executes the root handler Use this to process or transform the input before it is passed to the underlying handler. Use in combination with the Skip function to filter out non-actionable inputs.

func (Handler[T]) Handle

func (of Handler[T]) Handle(in T)

Handler provides a more expository operation to call a handler function.

func (Handler[T]) If

func (of Handler[T]) If(cond bool) Handler[T]

If returns an handler that only executes the root handler if the condition is true.

func (Handler[T]) Join

func (of Handler[T]) Join(next Handler[T]) Handler[T]

Join creates an handler function that runs both the root handler and the "next" handler.

func (Handler[T]) Lock

func (of Handler[T]) Lock() Handler[T]

Lock returns an handler that is protected by a mutex. All execution's of the handler are isolated.

func (Handler[T]) Once

func (of Handler[T]) Once() Handler[T]

Once produces an handler function that runs exactly once, and successive executions of the handler are noops.

func (Handler[T]) PreHook

func (of Handler[T]) PreHook(prev Handler[T]) Handler[T]

PreHook provides the inverse operation to Join, running "prev" handler before the base handler.

Returns itself if prev is nil.

func (Handler[T]) RecoverPanic

func (of Handler[T]) RecoverPanic(in T) error

RecoverPanic runs the handler function with a panic handler and converts a possible panic to an error.

func (Handler[T]) Safe

func (of Handler[T]) Safe() func(T) error

Safe returns a function that will execute the underlying Handler function. Any panics in the underlying handler are converted to errors.

func (Handler[T]) Skip

func (of Handler[T]) Skip(hook func(T) bool) Handler[T]

Skip runs a check before passing the object to the obsever, when the condition function--which can inspect the input object--returns true, the underlying Handler is called, otherwise, the observation is a noop.

func (Handler[T]) When

func (of Handler[T]) When(cond func() bool) Handler[T]

When returns an handler function that only executes the handler function if the condition function returns true. The condition function is run every time the handler function runs.

func (Handler[T]) WithLock

func (of Handler[T]) WithLock(mtx *sync.Mutex) Handler[T]

WithLock protects the action of the handler with the provied mutex.

func (Handler[T]) WithLocker

func (of Handler[T]) WithLocker(mtx sync.Locker) Handler[T]

WithLock protects the action of the handler with the provied mutex.

func (Handler[T]) WithRecover

func (of Handler[T]) WithRecover(oe Handler[error]) Handler[T]

WithRecover handles any panic encountered during the handler's execution and converts it to an error.

Jump to

Keyboard shortcuts

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