callback

package
v0.11.14 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2025 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Callback

type Callback[T any] interface {
	// Complete is invoked when the operation completes successfully with the result 't' of type T.
	Complete(t T)

	// CompleteError is invoked when the operation fails, providing an error 'err' indicating the failure reason.
	CompleteError(err error)
}

Callback defines an interface for handling the completion of an asynchronous operation. It allows the caller to notify the system when an operation has completed successfully or when it has failed with an error. The interface provides two methods for handling both success and error cases.

The generic type T represents the result type of the operation, which can vary depending on the specific use case.

Methods:

  • Complete(t T): This method is called when the asynchronous operation completes successfully. It accepts a result of type T, which is the outcome of the operation.

  • CompleteError(err error): This method is called when the asynchronous operation fails. It accepts an error, which indicates the reason for the failure.

func NewOnce

func NewOnce[T any](onComplete func(t T), onError func(err error)) Callback[T]

NewOnce creates a new instance of Once with the provided success and error callbacks. It ensures that the callbacks are invoked only once, either for success or failure.

type Once

type Once[T any] struct {
	OnComplete      func(t T)       // Callback function called on successful completion
	OnCompleteError func(err error) // Callback function called when an error occurs
	// contains filtered or unexported fields
}

func (*Once[T]) Complete

func (c *Once[T]) Complete(t T)

Complete is called to notify that the operation has completed successfully with the result 't'. It ensures that the 'OnComplete' callback is only called once.

func (*Once[T]) CompleteError

func (c *Once[T]) CompleteError(err error)

CompleteError is called to notify that the operation has failed with an error 'err'. It ensures that the 'OnCompleteError' callback is only called once.

Jump to

Keyboard shortcuts

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