gcon

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: May 30, 2023 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrIncomplete is returned when GetNow is invoked and the Func associated with the Promise hasn't completed.
	ErrIncomplete = errors.New("incomplete")
)

Functions

func Wait

func Wait[V any](promises ...*Promise[V]) error

Wait wait for a group of same type promises to complete

func WaitAny

func WaitAny(ws ...Waiter) error

WaitAny takes in zero or more Waiter instances and paused until one returns an error or all of them complete successfully. It returns the first error from a Waiter or nil, if no Waiter returns an error.

Types

type Func

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

Func represents any function that returns a Promise when passed to Run or Then.

func WithCancellation

func WithCancellation[T, V any](f Func[T, V]) Func[T, V]

WithCancellation takes in a Func and returns a Func that implements the passed-in Func's behavior, but adds context cancellation.

type Promise

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

Promise represents a potential or actual result from running a Func.

func Run

func Run[T, V any](ctx context.Context, t T, f Func[T, V]) *Promise[V]

Run produces a Promise for the supplied Func, evaluating the supplied context.Context and data. The Promise is returned immediately, no matter how long it takes for the Func to complete processing.

func Then

func Then[T, V any](ctx context.Context, p *Promise[T], f Func[T, V]) *Promise[V]

Then produces a Promise for the supplied Func, evaluating the supplied context.Context and Promise. The returned Promise is returned immediately, no matter how long it takes for the Func to complete processing. If the supplied Promise returns a non-nil error, the error is propagated to the returned Promise and the passed-in Func is not run.

func (*Promise[V]) Done

func (promise *Promise[V]) Done() bool

Done check if work is done

func (*Promise[V]) Get

func (promise *Promise[V]) Get() (V, error)

Get returns the value and the error (if any) for the Promise. Get waits until the Func associated with this Promise has completed. If the Func has completed, Get returns immediately.

func (*Promise[V]) GetNow

func (promise *Promise[V]) GetNow() (V, error)

GetNow returns the value and the error (if any) for the Promise. If the Func associated with this Promise has not completed, GetNow returns the zero value for the return type and ErrIncomplete.

func (*Promise[V]) Wait

func (promise *Promise[V]) Wait() error

Wait allows a Promise to implement the Waiter interface. It is similar to Get, but only returns the error.

type PromiseSet

type PromiseSet[V any] struct {
	Promises []*Promise[V]
}

PromiseSet a struct holding a list of promises

func NewPromiseSet

func NewPromiseSet[V any]() *PromiseSet[V]

NewPromiseSet make a new promise set with an initialized promises slice

func (*PromiseSet[V]) Add

func (promiseSet *PromiseSet[V]) Add(promises ...*Promise[V])

Add add one or more promises to promise set

func (*PromiseSet[V]) Wait

func (promiseSet *PromiseSet[V]) Wait() error

Wait wait for all promises in promiseset to complete. Same as the Wait function but tied to a promise set

type Waiter

type Waiter interface {
	Wait() error
}

Waiter defines an interface for the parameters to the Wait function.

Jump to

Keyboard shortcuts

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