diff --git a/pkg/confirmer/confirmer.go b/pkg/confirmer/confirmer.go index 15445d64..718ebffe 100644 --- a/pkg/confirmer/confirmer.go +++ b/pkg/confirmer/confirmer.go @@ -25,6 +25,8 @@ import ( // NOTE: For now, Confirmer only supports bool values but it could easily be made generic. +// Confirmer is used to ask for some value (e.g. a confirmation from a GUI element) +// in a threadsafe manner and retrieve that value later. type Confirmer struct { requests map[string]*Request locker sync.Locker @@ -37,6 +39,7 @@ func New() *Confirmer { } } +// NewRequest creates a new request object that waits up to the given amount of time for the result. func (c *Confirmer) NewRequest(timeout time.Duration) *Request { c.locker.Lock() defer c.locker.Unlock() @@ -48,11 +51,12 @@ func (c *Confirmer) NewRequest(timeout time.Duration) *Request { return req } -func (c *Confirmer) SetResponse(uuid string, value bool) error { +// SetResult sets the result value of the request with the given ID. +func (c *Confirmer) SetResult(id string, value bool) error { c.locker.Lock() defer c.locker.Unlock() - req, ok := c.requests[uuid] + req, ok := c.requests[id] if !ok { return errors.New("no such request") } diff --git a/pkg/confirmer/request.go b/pkg/confirmer/request.go index 199886da..6aa15ace 100644 --- a/pkg/confirmer/request.go +++ b/pkg/confirmer/request.go @@ -24,6 +24,7 @@ import ( "github.com/google/uuid" ) +// Request provides a result when it becomes available. type Request struct { uuid string value chan bool @@ -38,10 +39,12 @@ func newRequest(timeout time.Duration) *Request { } } +// ID returns the request's ID, used to set the request's value. func (r *Request) ID() string { return r.uuid } +// Result returns the result or an error if it is not available within the request timeout. func (r *Request) Result() (bool, error) { select { case res := <-r.value: