···11package main
2233+import (
44+ "regexp"
55+ "strings"
66+)
77+38// Bus provides a basic pubsub modelo via callbacks.
49type Bus struct {
510 // subscribers
611 topics map[string]*Topic
1212+ topID int
713}
814915func (b *Bus) Register(name string) *Topic {
1616+ if t, ok := b.topics[name]; ok {
1717+ return t
1818+ }
1019 b.topics[name] = &Topic{
1120 name: name,
1221 }
···27362837// Subscribe subscribes the func to the given topic and returns a non-zero ID if it was added.
2938func (b *Bus) Subscribe(topic string, cb func(...any)) (id int) {
3030- t, ok := b.topics[topic]
3131-3232- // Eh... just auto-register if it doesn't exist.
3333- if !ok {
3434- t = b.Register(topic)
3939+ // Check if topic is registered and if not, return 0 (failure)
4040+ if _, ok := b.topics[topic]; !ok {
4141+ return 0
3542 }
36433737- t.topID++
3838- id = t.topID
3939- t.subscriptions = append(t.subscriptions, Subscription{
4040- id: id,
4141- callback: cb,
4242- })
4444+ // Yeah, yeah, this is inefficient. We're just escaping "." and converting "*" to match any/all non-'.' rune.
4545+ pattern := "^" + strings.ReplaceAll(strings.ReplaceAll(topic, ".", "\\."), "*", "([^.]*)") + "$"
4646+4747+ // Crummy regex to subbie.
4848+ var foundMatch bool
4949+ for k, t := range b.topics {
5050+ if match, _ := regexp.Match(pattern, []byte(k)); match {
5151+ if !foundMatch {
5252+ b.topID++
5353+ foundMatch = true
5454+ }
5555+ t.subscriptions = append(t.subscriptions, Subscription{
5656+ id: b.topID,
5757+ callback: cb,
5858+ })
5959+ }
6060+ }
43614444- return id
6262+ return b.topID
4563}
46644765// Unsubscribes removes a subscription for a topic using the given id. Returns true on success, false if the topic or id was not found.
+2
resizable.go
···7575 }
76767777 // Hook into global bus for convenient program-wide handling.
7878+ // eh... register as well.
7979+ bus.Register(UI_CANCEL)
7880 r.subscription = bus.Subscribe(UI_CANCEL, func(v ...any) {
7981 if r.heldGripper != nil {
8082 r.heldGripper.isHeld = false