repos / pico

pico services - prose.sh, pastes.sh, imgs.sh, feeds.sh, pgs.sh
git clone https://github.com/picosh/pico.git

commit
a7664b8
parent
d155314
author
Eric Bower
date
2024-09-24 01:50:37 +0000 UTC
feat(pubsub): pub new flag, `-t {duration}` timeout

The default is no timeout.
1 files changed,  +20, -1
M pubsub/cli.go
+20, -1
 1@@ -8,6 +8,7 @@ import (
 2 	"log/slog"
 3 	"strings"
 4 	"text/tabwriter"
 5+	"time"
 6 
 7 	"github.com/charmbracelet/ssh"
 8 	"github.com/charmbracelet/wish"
 9@@ -181,6 +182,7 @@ func WishMiddleware(handler *CliHandler) wish.Middleware {
10 				pubCmd := flagSet("pub", sesh)
11 				empty := pubCmd.Bool("e", false, "Send an empty message to subs")
12 				public := pubCmd.Bool("p", false, "Anyone can sub to this channel")
13+				timeout := pubCmd.Duration("t", -1, "Timeout as a Go duration before cancelling the pub event. Valid time units are 'ns', 'us' (or 'µs'), 'ms', 's', 'm', 'h'. Default is no timeout.")
14 				if !flagCheck(pubCmd, repoName, cmdArgs) {
15 					return
16 				}
17@@ -215,8 +217,14 @@ func WishMiddleware(handler *CliHandler) wish.Middleware {
18 					})
19 				}
20 
21+				tt := *timeout
22+				str := "no subs found ... waiting"
23+				if tt > 0 {
24+					str += " " + tt.String()
25+				}
26+
27 				if count == 0 {
28-					wish.Println(sesh, "no subs found ... waiting")
29+					wish.Println(sesh, str)
30 				}
31 
32 				go func() {
33@@ -224,6 +232,17 @@ func WishMiddleware(handler *CliHandler) wish.Middleware {
34 					pub.Cleanup()
35 				}()
36 
37+				go func() {
38+					// never cancel pub event
39+					if tt == -1 {
40+						return
41+					}
42+
43+					<-time.After(tt)
44+					pub.Cleanup()
45+					sesh.Close()
46+				}()
47+
48 				err = pubsub.PubSub.Pub(name, pub)
49 				wish.Println(sesh, "msg sent!")
50 				if err != nil {