Revert the revert of G's mesh-sdk merge (#191): G's sdk stands again over reader release 3
Main reverted G's half merge (#189, reverted by8afc3ebin #191) and then took the reader release's S1-S5, which copied G's code. G's form holds where they differ (process_bus.go, seats.go, state.go and their tests); the release's kept names stay so main's callers keep building: SeatViewPrefix beside SeatReadPrefix, and QueueInvoke's empty strings on not-ok (with its test TestQueueInvoke). The tree differs from G's headf30dee54only by those and the release's two tests (objects cap option, seat states and stream kinds). Builds: design 47 G (the milestone's sdk part, as atf30dee54) Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
This commit is contained in:
+41
-5
@@ -499,6 +499,17 @@ func live() (*channel, error) {
|
||||
// answer in time is kept, as the runtime may have bound it.) Call it from a goroutine
|
||||
// started before Serve, or from a tool, once Serve runs.
|
||||
func Subscribe(pattern string, handle func(Envelope) error) error {
|
||||
return subscribe(pattern, handle, false)
|
||||
}
|
||||
|
||||
// SubscribeNew is Subscribe for a handler that is given only the messages published after the subscription is made:
|
||||
// the runtime makes an ordered consumer on the stream that carries the pattern, starting at the next message
|
||||
// (`mesh/subscribe` with `deliver: new`; design 47 G §559).
|
||||
func SubscribeNew(pattern string, handle func(Envelope) error) error {
|
||||
return subscribe(pattern, handle, true)
|
||||
}
|
||||
|
||||
func subscribe(pattern string, handle func(Envelope) error, deliverNew bool) error {
|
||||
c, err := live()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -508,7 +519,11 @@ func Subscribe(pattern string, handle func(Envelope) error) error {
|
||||
n := c.subscribed
|
||||
c.listeners = append(c.listeners, listener{n: n, pattern: pattern, handle: handle})
|
||||
c.mu.Unlock()
|
||||
_, err = c.ask("mesh/subscribe", map[string]any{"pattern": pattern}, 30*time.Second)
|
||||
params := map[string]any{"pattern": pattern}
|
||||
if deliverNew {
|
||||
params["deliver"] = "new"
|
||||
}
|
||||
_, err = c.ask("mesh/subscribe", params, 30*time.Second)
|
||||
// Removed only when the runtime answered that it refused: one that did not answer in time may
|
||||
// have bound the consumer, and a handler removed then would have its events taken unhandled.
|
||||
var no *refused
|
||||
@@ -567,7 +582,8 @@ func Ask(key string, body any) (json.RawMessage, error) {
|
||||
}
|
||||
|
||||
// TopicMatches says whether an event's type matches a pattern: `*` is one dot-separated segment, `#`
|
||||
// (or `**`) any number, as the TypeScript SDK matches.
|
||||
// (or `**`) any number, as the TypeScript SDK matches; the bus's own `>` as the last token matches one or more
|
||||
// further tokens (design 47 G A-101), so an adopted follower's `<seat>.<event>.>` hears every event on the seat's event.
|
||||
func TopicMatches(pattern, key string) bool {
|
||||
return matchFrom(strings.Split(pattern, "."), 0, strings.Split(key, "."), 0)
|
||||
}
|
||||
@@ -576,6 +592,9 @@ func matchFrom(p []string, pi int, k []string, ki int) bool {
|
||||
if pi == len(p) {
|
||||
return ki == len(k)
|
||||
}
|
||||
if p[pi] == ">" && pi == len(p)-1 {
|
||||
return ki < len(k)
|
||||
}
|
||||
if p[pi] == "#" || p[pi] == "**" {
|
||||
for skip := ki; skip <= len(k); skip++ {
|
||||
if matchFrom(p, pi+1, k, skip) {
|
||||
@@ -599,6 +618,9 @@ type EmitOptions struct {
|
||||
// is kept to one, never generated once an id is given (novox/hq ADR 0436 clause 4). Empty is a random
|
||||
// one, as on main.
|
||||
MessageID string
|
||||
// Ack asks the runtime to answer only once the bus acknowledged the event (`mesh/publish` with `ack: true`): Emit
|
||||
// then returns an error where the runtime answered `{error}` (design 47 G §559).
|
||||
Ack bool
|
||||
}
|
||||
|
||||
// Emit asks the runtime to publish an event as this bundle's module, and returns once the bus has
|
||||
@@ -616,11 +638,12 @@ func Emit(eventType string, body any, opts ...EmitOptions) error {
|
||||
if node == "" {
|
||||
node = "unknown"
|
||||
}
|
||||
id := ""
|
||||
id, ack := "", false
|
||||
for _, o := range opts {
|
||||
if o.MessageID != "" {
|
||||
id = o.MessageID
|
||||
}
|
||||
ack = ack || o.Ack
|
||||
}
|
||||
if id == "" {
|
||||
id = eventID()
|
||||
@@ -637,8 +660,21 @@ func Emit(eventType string, body any, opts ...EmitOptions) error {
|
||||
headers["x-schema"] = o.Schema
|
||||
}
|
||||
}
|
||||
_, err := c.ask("mesh/publish", map[string]any{"key": eventType, "node": node, "body": body, "headers": headers}, 30*time.Second)
|
||||
return err
|
||||
params := map[string]any{"key": eventType, "node": node, "body": body, "headers": headers}
|
||||
if ack {
|
||||
params["ack"] = true
|
||||
}
|
||||
answer, err := c.ask("mesh/publish", params, 30*time.Second)
|
||||
if err != nil || !ack {
|
||||
return err
|
||||
}
|
||||
var said struct {
|
||||
Error any `json:"error"`
|
||||
}
|
||||
if json.Unmarshal(answer, &said) == nil && said.Error != nil {
|
||||
return fmt.Errorf("the bus did not acknowledge %s: %v", eventType, said.Error)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// eventID is a random UUID, version 4, as the TypeScript SDK's randomUUID.
|
||||
|
||||
Reference in New Issue
Block a user