add ctx to v01
parent
51ae1b27b4
commit
3dd0a557d4
|
|
@ -13,7 +13,7 @@ type Parser interface {
|
|||
|
||||
func New(ctx context.Context, src raw.Raw) Parser {
|
||||
if os.Getenv("BUTTON_PARSER_V01") == "true" {
|
||||
return NewV01(src)
|
||||
return NewV01(ctx, src)
|
||||
}
|
||||
return NewPlaintext(src)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package button
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
|
|
@ -15,6 +16,8 @@ var debugging = os.Getenv("DEBUG") == "true"
|
|||
|
||||
type (
|
||||
V01 struct {
|
||||
ctx context.Context
|
||||
can context.CancelFunc
|
||||
src raw.Raw
|
||||
cfg v01Cfg
|
||||
}
|
||||
|
|
@ -36,17 +39,21 @@ type (
|
|||
v01Transformation map[string]string
|
||||
)
|
||||
|
||||
func NewV01(src raw.Raw) V01 {
|
||||
func NewV01(ctx context.Context, src raw.Raw) V01 {
|
||||
var cfg v01Cfg
|
||||
b, _ := ioutil.ReadFile(os.Getenv("BUTTON_PARSER_V01_CONFIG"))
|
||||
yaml.Unmarshal(b, &cfg)
|
||||
ctx, can := context.WithCancel(ctx)
|
||||
return V01{
|
||||
ctx: ctx,
|
||||
can: can,
|
||||
src: src,
|
||||
cfg: cfg,
|
||||
}
|
||||
}
|
||||
|
||||
func (v01 V01) Close() {
|
||||
v01.can()
|
||||
v01.src.Close()
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package button_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"mayhem-party/src/device/input/button"
|
||||
"os"
|
||||
|
|
@ -12,7 +13,7 @@ import (
|
|||
func TestV01(t *testing.T) {
|
||||
src := constSrc(fmt.Sprintf(`{"T":%v,"U":"bel","Y":"abc","N":"cde"}`, time.Now().UnixNano()/int64(time.Millisecond)-50))
|
||||
t.Logf("(%v) %s", len(src), src.Read())
|
||||
v01 := button.NewV01(src)
|
||||
v01 := button.NewV01(context.Background(), src)
|
||||
got := v01.Read()
|
||||
want := []button.Button{
|
||||
{Down: true, Char: 'a'},
|
||||
|
|
@ -48,7 +49,7 @@ func TestV01WithCfg(t *testing.T) {
|
|||
os.Setenv("BUTTON_PARSER_V01_CONFIG", p)
|
||||
|
||||
t.Run("unknown user ignored", func(t *testing.T) {
|
||||
v01 := button.NewV01(constSrc(`{"U":"qt","Y":"w"}`))
|
||||
v01 := button.NewV01(context.Background(), constSrc(`{"U":"qt","Y":"w"}`))
|
||||
got := v01.Read()
|
||||
if len(got) != 0 {
|
||||
t.Error(got)
|
||||
|
|
@ -56,7 +57,7 @@ func TestV01WithCfg(t *testing.T) {
|
|||
})
|
||||
|
||||
t.Run("player2", func(t *testing.T) {
|
||||
v01 := button.NewV01(constSrc(`{"U":"bel","Y":"w","N":"w"}`))
|
||||
v01 := button.NewV01(context.Background(), constSrc(`{"U":"bel","Y":"w","N":"w"}`))
|
||||
got := v01.Read()
|
||||
if len(got) != 2 {
|
||||
t.Error(got)
|
||||
|
|
|
|||
Loading…
Reference in New Issue