future

package
v0.39.0 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2024 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 Chan added in v0.37.0

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

Chan represents a future that will complete with a single value of type T. This struct is non-blocking and allows for parallel callbacks to be registered. The result of the future can be accessed via the Get() method or by receiving from the channel returned by the Receive() method. The future can be completed with a value of type T using the Complete() method. The future can be chained with other futures using the ThenAccept() and ThenApply() methods. The future is thread-safe and can be used concurrently. The ThenAccept(), Get() and Receive() methods can be called multiple times to receive the same result. Multiple calls to the Complete() method will only complete the future once.

func Completed added in v0.37.0

func Completed[T any](result T) *Chan[T]

Completed returns a new completed future with the given result.

func NewChan added in v0.37.0

func NewChan[T any]() *Chan[T]

NewChan returns a new Chan that will complete with a single value of type T.

func ThenApply added in v0.37.0

func ThenApply[O1, O2 any](f *Chan[O1], callback func(O1) O2) *Chan[O2]

ThenApply returns a new future that will complete with the result of the callback applied to the result of this future. This method is non-blocking and returns immediately.

func (*Chan[T]) Complete added in v0.37.0

func (f *Chan[T]) Complete(result T) *Chan[T]

Complete sets the result of this future once and propagates it to all current and future listeners. Multiple calls to this method will not have any effect. This method is non-blocking and returns immediately.

func (*Chan[T]) Get added in v0.37.0

func (f *Chan[T]) Get() T

Get returns the result of this future or blocks indefinitely until the result is available. Multiple calls to this method will all block until the result is available.

func (*Chan[T]) Receive added in v0.37.0

func (f *Chan[T]) Receive() <-chan T

Receive returns a channel that will receive the result of this future. The channel will be closed once after the result was sent. Multiple calls to this method will return new channels that will receive the same result.

func (*Chan[T]) ThenAccept added in v0.37.0

func (f *Chan[T]) ThenAccept(callback func(T)) *Chan[T]

ThenAccept registers a parallel callback to be called when the future is completed. This method is non-blocking and returns immediately.

type Future

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

Future is a struct that holds a value of type T, a slice of callbacks to be called when the value is set, a boolean to track if the callbacks have been called, and a mutex for thread safety.

func New

func New[T any]() *Future[T]

New returns a new Future.

func ThenCompose added in v0.39.0

func ThenCompose[T any, U any](f *Future[T], callback func(T) *Future[U]) *Future[U]

ThenCompose registers a callback to be called when the Future is completed and returns a new Future.

func (*Future[T]) Complete

func (f *Future[T]) Complete(value T) *Future[T]

Complete sets the value and calls the registered callbacks if they haven't been called yet.

func (*Future[T]) ThenAccept

func (f *Future[T]) ThenAccept(callback func(T)) *Future[T]

ThenAccept registers a callback to be called when the Future is completed.

Jump to

Keyboard shortcuts

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