···11+package main
22+33+// Bus provides a basic pubsub modelo via callbacks.
44+type Bus struct {
55+ // subscribers
66+ topics map[string]*Topic
77+}
88+99+func (b *Bus) Register(name string) *Topic {
1010+ b.topics[name] = &Topic{
1111+ name: name,
1212+ }
1313+ return b.topics[name]
1414+}
1515+1616+func (b *Bus) Unregister(name string) {
1717+ delete(b.topics, name)
1818+}
1919+2020+func (b *Bus) Publish(topic string, parts ...any) {
2121+ if t, ok := b.topics[topic]; ok {
2222+ for _, s := range t.subscriptions {
2323+ s.callback(parts)
2424+ }
2525+ }
2626+}
2727+2828+// Subscribe subscribes the func to the given topic and returns a non-zero ID if it was added.
2929+func (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)
3535+ }
3636+3737+ t.topID++
3838+ id = t.topID
3939+ t.subscriptions = append(t.subscriptions, Subscription{
4040+ id: id,
4141+ callback: cb,
4242+ })
4343+4444+ return id
4545+}
4646+4747+// Unsubscribes removes a subscription for a topic using the given id. Returns true on success, false if the topic or id was not found.
4848+func (b *Bus) Unsubscribe(topic string, id int) bool {
4949+ if t, ok := b.topics[topic]; ok {
5050+ for index, s := range t.subscriptions {
5151+ if s.id == id {
5252+ t.subscriptions = append(t.subscriptions[:index], t.subscriptions[index+1:]...)
5353+ return true
5454+ }
5555+ }
5656+ }
5757+ return false
5858+}
5959+6060+type Subscription struct {
6161+ id int
6262+ callback func(...any)
6363+}
6464+6565+type Topic struct {
6666+ name string
6767+ subscriptions []Subscription
6868+ topID int
6969+}
7070+7171+var bus Bus
7272+7373+func init() {
7474+ bus.topics = make(map[string]*Topic)
7575+}
···11+package main
22+33+// EmphemeralConfig reprsents values that are not crucially defined by the user, such as current panel sizings and positions.
44+type EphemeralConfig struct {
55+ panelSizes map[string]int
66+}