promise

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2024 License: MIT Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	PromiseInit = int64(0)
)

Variables

View Source
var (
	ErrResolved       = errors.New("resolved already")
	ErrTimeoutNoSet   = errors.New("timeout not set")
	ErrTimeout        = errors.New("timeout")
	ErrNotImplemented = errors.New("not implemented")
	ErrReset          = errors.New("promise reset")

	// OnNewPromise offers a default implementation to instancize a new Promise for the PromisePool.
	OnNewPromise = func() Promise {
		return NewWaitGroupPromise()
	}

	// PromisePool offer a default pool to reused the Promises on calling NewPromise.
	// The default implmentation offers no pooling, call InitPool to setup a pool.
	PromisePool sync.Pool[Promise] = &sync.NilPool[Promise]{New: OnNewPromise}
)

Functions

func InitPool

func InitPool(cap int)

InitPool intialize the pool with specified capacity.

func Recycle

func Recycle(p Promise)

Recycle returns the promise to the pool.

Types

type AbstractPromise

type AbstractPromise struct {
	// contains filtered or unexported fields
}

func (*AbstractPromise) Close

func (p *AbstractPromise) Close()

func (*AbstractPromise) Deadline

func (p *AbstractPromise) Deadline() (time.Time, bool)

func (*AbstractPromise) Error

func (p *AbstractPromise) Error() error

func (*AbstractPromise) IsResolved

func (p *AbstractPromise) IsResolved() bool

func (*AbstractPromise) OnCreateTimerLocked

func (p *AbstractPromise) OnCreateTimerLocked(*time.Timer)

func (*AbstractPromise) Options

func (p *AbstractPromise) Options() interface{}

func (*AbstractPromise) Reset

func (p *AbstractPromise) Reset()

func (*AbstractPromise) ResetWithOptions

func (p *AbstractPromise) ResetWithOptions(opts interface{})

func (*AbstractPromise) ResolveRets

func (p *AbstractPromise) ResolveRets(rets ...interface{}) bool

func (*AbstractPromise) ResolvedAt

func (p *AbstractPromise) ResolvedAt() time.Time

func (*AbstractPromise) Result

func (p *AbstractPromise) Result() (interface{}, error)

func (*AbstractPromise) SetProvider

func (p *AbstractPromise) SetProvider(provider PromiseProvider)

func (*AbstractPromise) SetTimeout

func (p *AbstractPromise) SetTimeout(timeout time.Duration)

func (*AbstractPromise) TimeoutC

func (p *AbstractPromise) TimeoutC(timeouts ...time.Duration) (<-chan time.Time, error)

func (*AbstractPromise) Value

func (p *AbstractPromise) Value() interface{}

type ChannelPromise

type ChannelPromise struct {
	AbstractPromise
	// contains filtered or unexported fields
}

func NewChannelPromise

func NewChannelPromise() *ChannelPromise

func NewChannelPromiseWithOptions

func NewChannelPromiseWithOptions(opts interface{}) *ChannelPromise

func ResolvedChannel

func ResolvedChannel(rets ...interface{}) *ChannelPromise

func (*ChannelPromise) Lock

func (p *ChannelPromise) Lock()

func (*ChannelPromise) Reset

func (p *ChannelPromise) Reset()

func (*ChannelPromise) ResetWithOptions

func (p *ChannelPromise) ResetWithOptions(opts interface{})

func (*ChannelPromise) Resolve

func (p *ChannelPromise) Resolve(rets ...interface{}) (Promise, error)

func (*ChannelPromise) Timeout

func (p *ChannelPromise) Timeout(timeouts ...time.Duration) error

func (*ChannelPromise) Unlock

func (p *ChannelPromise) Unlock()

func (*ChannelPromise) Wait

func (p *ChannelPromise) Wait()

PromiseProvider

type Promise

type Promise interface {
	// Reset Reset promise
	Reset()

	// ResetWithOptions Reset promise will options
	ResetWithOptions(interface{})

	// Close Close the promise
	Close()

	// IsResolved If the promise is resolved
	IsResolved() bool

	// Get the time the promise last resolved. time.Time{} if the promise is unresolved.
	ResolvedAt() time.Time

	// Resolve Resolve the promise with value or (value, error)
	Resolve(...interface{}) (Promise, error)

	// Options Get options
	Options() interface{}

	// Value Get resolved value
	Value() interface{}

	// Result Helper function to get (value, error)
	Result() (interface{}, error)

	// Error Get last error on resolving
	Error() error

	// SetTimeout Set how long the promise should timeout.
	SetTimeout(time.Duration)

	// Deadline returns the deadline if timeout is set.
	Deadline() (time.Time, bool)

	// Timeout returns ErrTimeout if timeout, or ErrTimeoutNoSet if the timeout not set.
	Timeout(timeout ...time.Duration) error

	// TimeoutC returns a channel that is closed when the promise timeout.
	TimeoutC(timeout ...time.Duration) (<-chan time.Time, error)
}

func NewPromise

func NewPromise() (p Promise)

NewPromise returns a new promise provided by the pool.

func NewPromiseWithOptions

func NewPromiseWithOptions(opts interface{}) Promise

NewPromise returns a new promise with options provided by the pool.

func Resolved

func Resolved(rets ...interface{}) (p Promise)

Resolved returns a resolved promise.

type PromiseProvider

type PromiseProvider interface {
	Lock()
	Unlock()
	Wait()
	OnCreateTimerLocked(timer *time.Timer)
}

type SyncPromise

type SyncPromise struct {
	AbstractPromise
	// contains filtered or unexported fields
}

func NewSyncPromise

func NewSyncPromise() *SyncPromise

func NewSyncPromiseWithOptions

func NewSyncPromiseWithOptions(opts interface{}) *SyncPromise

func ResolvedSync

func ResolvedSync(rets ...interface{}) *SyncPromise

func (*SyncPromise) Lock

func (p *SyncPromise) Lock()

func (*SyncPromise) OnCreateTimerLocked

func (p *SyncPromise) OnCreateTimerLocked(timer *time.Timer)

func (*SyncPromise) Reset

func (p *SyncPromise) Reset()

func (*SyncPromise) ResetWithOptions

func (p *SyncPromise) ResetWithOptions(opts interface{})

func (*SyncPromise) Resolve

func (p *SyncPromise) Resolve(rets ...interface{}) (Promise, error)

func (*SyncPromise) Timeout

func (p *SyncPromise) Timeout(timeouts ...time.Duration) error

func (*SyncPromise) Unlock

func (p *SyncPromise) Unlock()

func (*SyncPromise) Wait

func (p *SyncPromise) Wait()

PromiseProvider

type WaitGroupPromise

type WaitGroupPromise struct {
	AbstractPromise
	// contains filtered or unexported fields
}

func NewWaitGroupPromise

func NewWaitGroupPromise() *WaitGroupPromise

func NewWaitGroupPromiseWithOptions

func NewWaitGroupPromiseWithOptions(opts interface{}) *WaitGroupPromise

func ResolvedWaitGroup

func ResolvedWaitGroup(rets ...interface{}) *WaitGroupPromise

func (*WaitGroupPromise) Lock

func (p *WaitGroupPromise) Lock()

func (*WaitGroupPromise) Reset

func (p *WaitGroupPromise) Reset()

func (*WaitGroupPromise) ResetWithOptions

func (p *WaitGroupPromise) ResetWithOptions(opts interface{})

func (*WaitGroupPromise) Resolve

func (p *WaitGroupPromise) Resolve(rets ...interface{}) (Promise, error)

func (*WaitGroupPromise) Timeout

func (p *WaitGroupPromise) Timeout(timeouts ...time.Duration) error

func (*WaitGroupPromise) Unlock

func (p *WaitGroupPromise) Unlock()

func (*WaitGroupPromise) Wait

func (p *WaitGroupPromise) Wait()

PromiseProvider

Jump to

Keyboard shortcuts

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