feat(01-02): dispatch typed events with request context

- App-owned festival bus with Fire, Collect and UntilHandled
- Recover listener panics with owner plugin IDs; isolate buses per app
- towel context accessors and hello command demo of all three modes
This commit is contained in:
Jakub Zych
2026-09-16 13:30:46 +02:00
parent 36c152f4bb
commit a654e96d66
5 changed files with 272 additions and 13 deletions

View File

@@ -5,10 +5,15 @@ import (
"git.golem15.com/golem15/summercms/backpack"
"git.golem15.com/golem15/summercms/bonfire"
"git.golem15.com/golem15/summercms/festival"
"git.golem15.com/golem15/summercms/pact"
"git.golem15.com/golem15/summercms/party"
"git.golem15.com/golem15/summercms/towel"
)
var _ festival.Collectable = (*HelloEvent)(nil)
var _ festival.Handleable = (*HelloEvent)(nil)
// Plugin is the golem15.greeter plugin. It requires golem15.hello.
type Plugin struct {
app *backpack.App
@@ -28,6 +33,19 @@ func (p *Plugin) Boot(app *backpack.App) error {
p.extra = msg.Message()
}
}
if app != nil && app.Events != nil {
app.Events.Listen[*HelloEvent]("golem15.greeter", func(ctx context.Context, e *HelloEvent) error {
if e.data == nil {
e.data = map[string]any{}
}
e.data["source"] = "greeter"
if actor, ok := towel.Actor(ctx); ok {
e.data["actor"] = actor
}
e.handled = true
return nil
})
}
return nil
}
@@ -44,12 +62,50 @@ func (p *Plugin) Commands() []bonfire.Command {
posts = p.app.Config.Int("golem15.hello.posts_per_page")
debug = p.app.Config.Bool("app.debug")
}
out.Printf("name=%s posts_per_page=%d debug=%t extra=%s\n", name, posts, debug, p.extra)
events := "ok"
collected := ""
handled := false
if p.app != nil && p.app.Events != nil {
if err := p.app.Events.Fire(ctx, &HelloEvent{}); err != nil {
events = "err"
}
payload, err := p.app.Events.Collect(ctx, &HelloEvent{})
if err != nil {
events = "err"
}
if src, ok := payload["source"].(string); ok {
collected = src
}
ok, err := p.app.Events.UntilHandled(ctx, &HelloEvent{})
if err != nil {
events = "err"
}
handled = ok
}
out.Printf("name=%s posts_per_page=%d debug=%t extra=%s events=%s collected=%s handled=%t\n", name, posts, debug, p.extra, events, collected, handled)
return nil
},
}}
}
// HelloEvent is a typed greeter event used to demonstrate Fire, Collect
// and UntilHandled on the app bus.
type HelloEvent struct {
data map[string]any
handled bool
}
func (e *HelloEvent) Collected() map[string]any {
if e == nil {
return nil
}
return e.data
}
func (e *HelloEvent) IsHandled() bool {
return e != nil && e.handled
}
var _ pact.HasCommands = (*Plugin)(nil)
func init() {