views.FindingsAskedSubject, views.FindingsAskedEvent and FINDINGS_ASKED_EVENT are gone; the stdio writer subscribes the seat event the given subject names. Builds: design 47 G A-17, A-13, §461 Claude-Session: https://claude.ai/code/session_01JG73KEq4zoDfM9WtYpcQgZ
76 lines
2.9 KiB
Go
76 lines
2.9 KiB
Go
package views
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
"sync/atomic"
|
|
|
|
stdio "git.novox.be/novox/mesh-sdk/go"
|
|
)
|
|
|
|
// NewStdioWriter makes the finding writer of a stdio module (design 47 G A-16, §461, §485, §503): it fills `Publish` from
|
|
// the module's own event publish (stdio.Emit, answered once the bus acknowledged it) and `Listen` from its subscription
|
|
// (stdio.SubscribeNew of the seat event the config's Asked subject names, only the asks published after it is made), so no module writes that adapter. A `Publish` or `Listen`
|
|
// the config already holds is kept. The runtime publishes and subscribes by the event's name on the subject the module's
|
|
// membership names for it, so each of the writer's four subjects (FindingSubjects) is told by its event.
|
|
func NewStdioWriter[F, C, S, A any](cfg WriterConfig[F, C, S, A]) (*Writer[F, C, S, A], error) {
|
|
return newStdioWriter(cfg, stdio.Emit, stdio.SubscribeNew)
|
|
}
|
|
|
|
func newStdioWriter[F, C, S, A any](cfg WriterConfig[F, C, S, A],
|
|
emit func(string, any, ...stdio.EmitOptions) error,
|
|
subscribe func(string, func(stdio.Envelope) error) error) (*Writer[F, C, S, A], error) {
|
|
if cfg.Publish == nil {
|
|
cfg.Publish = func(_ context.Context, subject string, data []byte, id string) error {
|
|
event, err := eventOfSubject(subject)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return emit(event, json.RawMessage(data), stdio.EmitOptions{MessageID: id, Ack: true})
|
|
}
|
|
}
|
|
if cfg.Listen == nil {
|
|
cfg.Listen = func(_ context.Context, subject string, handle func([]byte)) (func(), error) {
|
|
event, err := seatEventOfSubject(subject)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var stopped atomic.Bool
|
|
// the runtime keeps a subscription until the process ends: a stopped listener hands on nothing
|
|
if err := subscribe(event, func(env stdio.Envelope) error {
|
|
if !stopped.Load() {
|
|
handle(env.Body)
|
|
}
|
|
return nil
|
|
}); err != nil {
|
|
return nil, fmt.Errorf("subscribing to %s: %w", event, err)
|
|
}
|
|
return func() { stopped.Store(true) }, nil
|
|
}
|
|
}
|
|
return NewWriter(cfg)
|
|
}
|
|
|
|
// eventOfSubject is the event of a subject of FindingSubjects: `mesh.stream.<module>.event.<event>.<node>`.
|
|
func eventOfSubject(subject string) (string, error) {
|
|
p := strings.Split(subject, ".")
|
|
for i, s := range p {
|
|
if s == "event" && i+2 == len(p)-1 && i > 0 {
|
|
return p[i+1], nil
|
|
}
|
|
}
|
|
return "", fmt.Errorf("the subject %q is not of the form mesh.stream.<module>.event.<event>.<node>", subject)
|
|
}
|
|
|
|
// seatEventOfSubject is `<seat>.<event>` of a seat event's subject `mesh.seat.<seat>.event.<event>`, the name a runtime
|
|
// subscribes a seat's event by.
|
|
func seatEventOfSubject(subject string) (string, error) {
|
|
p := strings.Split(subject, ".")
|
|
if len(p) == 5 && p[0] == "mesh" && p[1] == "seat" && p[3] == "event" {
|
|
return p[2] + "." + p[4], nil
|
|
}
|
|
return "", fmt.Errorf("the subject %q is not of the form mesh.seat.<seat>.event.<event>", subject)
|
|
}
|