From d5c9b6d82585dacf82426ebff0c541b2e243257f Mon Sep 17 00:00:00 2001 From: James Mills <1290234+prologic@users.noreply.github.com> Date: Sun, 12 Mar 2023 15:17:57 +1000 Subject: [PATCH] Fix client.Read() context usage --- cmd/salty-chat/read.go | 36 +++++++++++++++++------------------- internal/web/app.wasm | 4 ++-- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/cmd/salty-chat/read.go b/cmd/salty-chat/read.go index b094e97..8567027 100644 --- a/cmd/salty-chat/read.go +++ b/cmd/salty-chat/read.go @@ -1,6 +1,7 @@ package main import ( + "context" "fmt" "os" "os/signal" @@ -9,7 +10,6 @@ import ( "github.com/mattn/go-isatty" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" - "golang.org/x/net/context" "go.salty.im/saltyim" ) @@ -76,32 +76,16 @@ func init() { } func read(follow bool, extraenvs, prehook, posthook string, args ...string) { - ctx, cancel := context.WithCancel(context.Background()) + ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) defer cancel() - sigs := make(chan os.Signal, 1) - signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) - - go func() { - <-sigs - cancel() - }() - opts := []saltyim.ReadOption{ saltyim.WithExtraEnvs(extraenvs), saltyim.WithPreHook(prehook), saltyim.WithPostHook(posthook), } - if follow { - for msg := range cli.Subscribe(ctx, opts...) { - if isatty.IsTerminal(os.Stdout.Fd()) { - fmt.Println(saltyim.FormatMessage(msg.Text)) - } else { - fmt.Println(msg.Text) - } - } - } else { + if !follow { msg, err := cli.Read(opts...) if err != nil { if err == saltyim.ErrNoMessages { @@ -115,5 +99,19 @@ func read(follow bool, extraenvs, prehook, posthook string, args ...string) { } else { fmt.Println(msg.Text) } + return + } + + msgs := cli.Subscribe(ctx, opts...) + select { + case msg := <-msgs: + if isatty.IsTerminal(os.Stdout.Fd()) { + fmt.Println(saltyim.FormatMessage(msg.Text)) + } else { + fmt.Println(msg.Text) + } + case <-ctx.Done(): + close(msgs) + return } } diff --git a/internal/web/app.wasm b/internal/web/app.wasm index 418acfa..5c01490 100755 --- a/internal/web/app.wasm +++ b/internal/web/app.wasm @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:a41cea81148f0a7b01c94011223272c9bcbd86ea086f4338d7b6cca2258a2e38 -size 29761883 +oid sha256:11a097d990f4dfee2f727c9aac96ba1bc46bac19e1803da3114dcc786b20f0c6 +size 30206460