docs: add docstrings for confirmer

This commit is contained in:
James Houlahan
2020-07-23 14:38:45 +02:00
parent 36ef9f20ae
commit c988d739a1
2 changed files with 9 additions and 2 deletions

View File

@ -25,6 +25,8 @@ import (
// NOTE: For now, Confirmer only supports bool values but it could easily be made generic. // 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 { type Confirmer struct {
requests map[string]*Request requests map[string]*Request
locker sync.Locker 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 { func (c *Confirmer) NewRequest(timeout time.Duration) *Request {
c.locker.Lock() c.locker.Lock()
defer c.locker.Unlock() defer c.locker.Unlock()
@ -48,11 +51,12 @@ func (c *Confirmer) NewRequest(timeout time.Duration) *Request {
return req 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() c.locker.Lock()
defer c.locker.Unlock() defer c.locker.Unlock()
req, ok := c.requests[uuid] req, ok := c.requests[id]
if !ok { if !ok {
return errors.New("no such request") return errors.New("no such request")
} }

View File

@ -24,6 +24,7 @@ import (
"github.com/google/uuid" "github.com/google/uuid"
) )
// Request provides a result when it becomes available.
type Request struct { type Request struct {
uuid string uuid string
value chan bool 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 { func (r *Request) ID() string {
return r.uuid 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) { func (r *Request) Result() (bool, error) {
select { select {
case res := <-r.value: case res := <-r.value: