refresh neither leaks wraps, allows 2 of the same at once, nor closes raws
This commit is contained in:
@@ -4,13 +4,14 @@ import (
|
||||
"context"
|
||||
"log"
|
||||
"mayhem-party/src/device/input/button"
|
||||
"mayhem-party/src/device/input/raw"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
var (
|
||||
ChanSigUsr1 = func() chan os.Signal {
|
||||
chSigUsr1 = func() chan os.Signal {
|
||||
c := make(chan os.Signal, 1)
|
||||
signal.Notify(c, syscall.SIGUSR1)
|
||||
return c
|
||||
@@ -22,12 +23,12 @@ type Refresh struct {
|
||||
input Wrap
|
||||
}
|
||||
|
||||
func NewRefresh(newWrap func() Wrap) *Refresh {
|
||||
return NewRefreshWith(newWrap, ChanSigUsr1)
|
||||
func NewRefresh(ctx context.Context, newWrap func() Wrap) *Refresh {
|
||||
return NewRefreshWith(ctx, newWrap, chSigUsr1)
|
||||
}
|
||||
|
||||
func NewRefreshWith(newWrap func() Wrap, ch <-chan os.Signal) *Refresh {
|
||||
ctx, can := context.WithCancel(context.Background())
|
||||
func NewRefreshWith(ctx context.Context, newWrap func() Wrap, ch <-chan os.Signal) *Refresh {
|
||||
ctx, can := context.WithCancel(ctx)
|
||||
result := &Refresh{
|
||||
can: can,
|
||||
input: newWrap(),
|
||||
@@ -40,16 +41,19 @@ func NewRefreshWith(newWrap func() Wrap, ch <-chan os.Signal) *Refresh {
|
||||
return
|
||||
case sig := <-ch:
|
||||
log.Println("refreshing for", sig)
|
||||
newInput := newWrap()
|
||||
oldInput := result.input
|
||||
result.input = newInput
|
||||
oldInput.Close()
|
||||
result.input.CloseWrap()
|
||||
result.input = newWrap()
|
||||
}
|
||||
}
|
||||
}()
|
||||
return result
|
||||
}
|
||||
|
||||
func (r *Refresh) CloseWrap() raw.Raw {
|
||||
r.can()
|
||||
return r.input.CloseWrap()
|
||||
}
|
||||
|
||||
func (r *Refresh) Read() []button.Button {
|
||||
return r.input.Read()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user