promise

package
v0.0.0-...-0b2320d Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2024 License: Apache-2.0 Imports: 3 Imported by: 6

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoData is an error returned when a request completes without available data.
	ErrNoData = errors.New("promise: No Data")
)

Functions

This section is empty.

Types

type Generator

type Generator[T any] func(context.Context) (T, error)

Generator is the Promise's generator type.

type Map

type Map[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Map is a map from some key to a promise that does something associated with this key.

First call to Get initiates a new promise. All subsequent calls return exact same promise (even if it has finished).

func (*Map[K, V]) Get

func (pm *Map[K, V]) Get(ctx context.Context, key K, gen Generator[V]) *Promise[V]

Get either returns an existing promise for the given key or creates and immediately launches a new promise.

type Promise

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

Promise is a promise structure with goroutine-safe methods that is responsible for owning a single piece of data. Promises have multiple readers and a single writer.

Readers will retrieve the Promise's data via Get(). If the data has not been populated, the reader will block pending the data. Once the data has been delivered, all readers will unblock and receive a reference to the Promise's data.

func New

func New[T any](ctx context.Context, gen Generator[T]) *Promise[T]

New instantiates a new, empty Promise instance. The Promise's value will be the value returned by the supplied generator function.

The generator will be invoked immediately in its own goroutine.

func NewDeferred

func NewDeferred[T any](gen Generator[T]) *Promise[T]

NewDeferred instantiates a new, empty Promise instance. The Promise's value will be the value returned by the supplied generator function.

Unlike New, the generator function will not be immediately executed. Instead, it will be run when the first call to Get is made, and will use one of the Get callers' goroutines. goroutine a Get caller.

func (*Promise[T]) Get

func (p *Promise[T]) Get(ctx context.Context) (T, error)

Get returns the promise's value. If the value isn't set, Get will block until the value is available, following the Context's timeout parameters.

If the value is available, it will be returned with its error status. If the context times out or is cancelled, the appropriate context error will be returned.

Jump to

Keyboard shortcuts

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