mirror of
https://github.com/ProtonMail/proton-bridge.git
synced 2025-12-23 02:26:42 +00:00
feat: implement confirmer
This commit is contained in:
44
pkg/confirmer/confirmer.go
Normal file
44
pkg/confirmer/confirmer.go
Normal file
@ -0,0 +1,44 @@
|
||||
package confirmer
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Confirmer struct {
|
||||
requests map[string]*Request
|
||||
locker sync.Locker
|
||||
}
|
||||
|
||||
func New() *Confirmer {
|
||||
return &Confirmer{
|
||||
requests: make(map[string]*Request),
|
||||
locker: &sync.Mutex{},
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Confirmer) NewRequest(timeout time.Duration) *Request {
|
||||
c.locker.Lock()
|
||||
defer c.locker.Unlock()
|
||||
|
||||
req := newRequest(timeout)
|
||||
|
||||
c.requests[req.ID()] = req
|
||||
|
||||
return req
|
||||
}
|
||||
|
||||
func (c *Confirmer) SetResponse(uuid string, value bool) error {
|
||||
c.locker.Lock()
|
||||
defer c.locker.Unlock()
|
||||
|
||||
req, ok := c.requests[uuid]
|
||||
if !ok {
|
||||
return errors.New("no such request")
|
||||
}
|
||||
|
||||
req.value <- value
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user