Documentation ¶
Index ¶
- func WaitRefCountContainer[T comparable](ctx context.Context, target *ccontainer.CContainer[T], ...) (T, error)
- type Ref
- type RefCount
- func (r *RefCount[T]) Access(ctx context.Context, useCtx bool, cb func(ctx context.Context, val T) error) error
- func (r *RefCount[T]) AddRef(cb func(resolved bool, val T, err error)) *Ref[T]
- func (r *RefCount[T]) AddRefPromise() (promise.PromiseLike[T], *Ref[T])
- func (r *RefCount[T]) ClearContext()
- func (r *RefCount[T]) Resolve(ctx context.Context) (T, func(), error)
- func (r *RefCount[T]) ResolveWithReleased(ctx context.Context, released func()) (T, func(), error)
- func (r *RefCount[T]) SetContext(ctx context.Context) bool
- func (r *RefCount[T]) SetContextIfCanceled(ctx context.Context) bool
- func (r *RefCount[T]) Wait(ctx context.Context) (T, *Ref[T], error)
- func (r *RefCount[T]) WaitWithReleased(ctx context.Context, released func()) (promise.PromiseLike[T], *Ref[T])
- type RefLike
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WaitRefCountContainer ¶
func WaitRefCountContainer[T comparable]( ctx context.Context, target *ccontainer.CContainer[T], targetErr *ccontainer.CContainer[*error], ) (T, error)
WaitRefCountContainer waits for a RefCount container handling errors. targetErr can be nil
Types ¶
type Ref ¶
type Ref[T comparable] struct { // contains filtered or unexported fields }
Ref is a reference to a RefCount.
type RefCount ¶
type RefCount[T comparable] struct { // contains filtered or unexported fields }
RefCount is a refcount driven object container. Wraps a ccontainer with a ref count mechanism. When there are no references, the container contents are released.
func NewRefCount ¶
func NewRefCount[T comparable]( ctx context.Context, keepUnref bool, target *ccontainer.CContainer[T], targetErr *ccontainer.CContainer[*error], resolver func(ctx context.Context, released func()) (T, func(), error), ) *RefCount[T]
NewRefCount builds a new RefCount.
ctx, target and targetErr can be empty
keepUnref sets if the value should be kept if there are zero references. resolver is the resolver function returns the value and a release function call the released callback if the value is no longer valid.
func (*RefCount[T]) Access ¶
func (r *RefCount[T]) Access(ctx context.Context, useCtx bool, cb func(ctx context.Context, val T) error) error
Access adds a reference, waits for a value, and calls the callback. Releases the reference once the callback has returned. The context will be canceled if the value is removed / changed. Return context.Canceled if the context is canceled. The callback may be restarted if the context is canceled and a new value is resolved. If useCtx=true and the current context is canceled, updates r to use ctx instead.
func (*RefCount[T]) AddRef ¶
AddRef adds a reference to the RefCount container. cb is an optional callback to call when the value changes. the callback will be called with an empty value when the value becomes empty.
func (*RefCount[T]) AddRefPromise ¶ added in v1.2.0
func (r *RefCount[T]) AddRefPromise() (promise.PromiseLike[T], *Ref[T])
AddRefPromise adds a reference and returns a promise with the value.
func (*RefCount[T]) ClearContext ¶ added in v1.1.2
func (r *RefCount[T]) ClearContext()
ClearContext clears the context and shuts down all routines.
func (*RefCount[T]) Resolve ¶ added in v1.9.0
Resolve adds a reference and waits for a value. Returns the value, release function, and any error. If err != nil, value and reference will be nil.
func (*RefCount[T]) ResolveWithReleased ¶ added in v1.9.0
ResolveWithReleased adds a reference, waits for a value, returns the value and a release function. Calls the released callback (if set) when the value or reference is released. Note: it's very unlikely, but still possible, that released will be called before the promise resolves. Note: released will always be called from a new goroutine. Note: this matches the signature of the refcount resolver function.
func (*RefCount[T]) SetContext ¶
SetContext updates the context to use for the RefCount container resolution. If ctx=nil the RefCount will wait until ctx != nil to start. This also restarts resolution, if there are any refs. Returns if the context was updated.
func (*RefCount[T]) SetContextIfCanceled ¶ added in v1.3.2
SetContextIfCanceled updates the context to use for the RefCount container resolution. If the current r.ctx is not nil and not canceled, does nothing. If the passed ctx is nil or canceled, does nothing. This also restarts resolution if there are any refs. Returns if the context was updated.
func (*RefCount[T]) Wait ¶
Wait adds a reference and waits for a value. Returns the value, reference, and any error. If err != nil, value and reference will be nil.
func (*RefCount[T]) WaitWithReleased ¶ added in v1.0.4
func (r *RefCount[T]) WaitWithReleased(ctx context.Context, released func()) (promise.PromiseLike[T], *Ref[T])
WaitWithReleased adds a reference, waits for a value, returns the value and a release function. Calls the released callback (if set) when the value or reference is released. Note: it's very unlikely, but still possible, that released will be called before the promise resolves. Note: released will always be called from a new goroutine.