refresh users global ch
parent
2113252e2d
commit
97cc3ae151
|
|
@ -6,6 +6,15 @@ import (
|
||||||
"mayhem-party/src/device/input/button"
|
"mayhem-party/src/device/input/button"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ChanSigUsr1 = func() chan os.Signal {
|
||||||
|
c := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(c, syscall.SIGUSR1)
|
||||||
|
return c
|
||||||
|
}()
|
||||||
)
|
)
|
||||||
|
|
||||||
type Refresh struct {
|
type Refresh struct {
|
||||||
|
|
@ -13,13 +22,11 @@ type Refresh struct {
|
||||||
input Wrap
|
input Wrap
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRefreshCh(sig os.Signal) <-chan os.Signal {
|
func NewRefresh(newWrap func() Wrap) *Refresh {
|
||||||
c := make(chan os.Signal, 1)
|
return NewRefreshWith(newWrap, ChanSigUsr1)
|
||||||
signal.Notify(c, sig)
|
|
||||||
return c
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRefresh(newWrap func() Wrap, ch <-chan os.Signal) *Refresh {
|
func NewRefreshWith(newWrap func() Wrap, ch <-chan os.Signal) *Refresh {
|
||||||
ctx, can := context.WithCancel(context.Background())
|
ctx, can := context.WithCancel(context.Background())
|
||||||
result := &Refresh{
|
result := &Refresh{
|
||||||
can: can,
|
can: can,
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ func TestRefresh(t *testing.T) {
|
||||||
}
|
}
|
||||||
ch := make(chan os.Signal, 1)
|
ch := make(chan os.Signal, 1)
|
||||||
defer close(ch)
|
defer close(ch)
|
||||||
refresh := NewRefresh(generator, ch)
|
refresh := NewRefreshWith(generator, ch)
|
||||||
defer refresh.Close()
|
defer refresh.Close()
|
||||||
|
|
||||||
assertIts := func(t *testing.T, b byte) {
|
assertIts := func(t *testing.T, b byte) {
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,12 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"mayhem-party/src/device/input/button"
|
"mayhem-party/src/device/input/button"
|
||||||
"os"
|
"os"
|
||||||
"syscall"
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
FlagBuffered = os.Getenv("WRAP_BUFFERED") == "true"
|
||||||
|
FlagRemapFile = os.Getenv("WRAP_REMAP_FILE")
|
||||||
|
FlagRefreshOnSigUsr1 = os.Getenv("WRAP_REFRESH_ON_SIGUSR1") == "true"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Wrap interface {
|
type Wrap interface {
|
||||||
|
|
@ -16,23 +21,22 @@ func New(ctx context.Context, srcFunc func() button.Parser) Wrap {
|
||||||
maker := func() Wrap {
|
maker := func() Wrap {
|
||||||
return srcFunc()
|
return srcFunc()
|
||||||
}
|
}
|
||||||
if os.Getenv("WRAP_BUFFERED") == "true" {
|
if FlagBuffered {
|
||||||
oldMaker := maker
|
oldMaker := maker
|
||||||
maker = func() Wrap {
|
maker = func() Wrap {
|
||||||
return NewBuffered(ctx, oldMaker())
|
return NewBuffered(ctx, oldMaker())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if p := os.Getenv("WRAP_REMAP_FILE"); p != "" {
|
if FlagRemapFile != "" {
|
||||||
oldMaker := maker
|
oldMaker := maker
|
||||||
maker = func() Wrap {
|
maker = func() Wrap {
|
||||||
return NewRemapFromFile(oldMaker(), p)
|
return NewRemapFromFile(oldMaker(), FlagRemapFile)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if os.Getenv("WRAP_REFRESH_ON_SIGUSR1") != "" {
|
if FlagRefreshOnSigUsr1 {
|
||||||
oldMaker := maker
|
oldMaker := maker
|
||||||
c := NewRefreshCh(syscall.SIGUSR1)
|
|
||||||
maker = func() Wrap {
|
maker = func() Wrap {
|
||||||
return NewRefresh(oldMaker, c)
|
return NewRefresh(oldMaker)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return maker()
|
return maker()
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,10 @@
|
||||||
|
package wrap_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"mayhem-party/src/device/input/wrap"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNewRefreshing(t *testing.T) {
|
||||||
|
wrap.FlagBuffered = true
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue