package stdio import ( "encoding/json" "errors" "fmt" "git.novox.be/novox/mesh-sdk/go/buscalls" "strings" "time" ) // busStateCalls are the method prefixes of the calls a bundle makes on its own bus state through the runtime (novox/hq // ADR 0595 §9, design 25 § 12.5): their shapes and names are package buscalls's, and package busstate the typed way // to make them. var busStateCalls = []string{"mesh/objects.", "mesh/stream.", "mesh/workqueue.", "mesh/ledger."} // BusState asks the runtime one call on this bundle's own bus state, an entry its module asks for in `requires`, named // by its local name in params; the runtime serves it from the module's own bindings only. A method that is none of // those calls is refused here, before the runtime is asked. func BusState(method string, params any) (json.RawMessage, error) { known := false for _, p := range busStateCalls { known = known || strings.HasPrefix(method, p) } if !known { return nil, fmt.Errorf("%s is no call on a bundle's own bus state (%s)", method, strings.Join(busStateCalls, ", ")) } c, err := live() if err != nil { return nil, err } return c.ask(method, params, 60*time.Second) } // WatchLedger hands handle every current state of the instances of this bundle's own ledger that instances names (an // instance, with `*` for one name and `>` for the rest; empty is every instance), and then every change, in the order it // came, and returns once the current states have all been handled. The watch lasts as long as the process. handle runs // beside the runtime (follower.go): its time never counts against the runtime's bound on the current states, and an error // it returns is said on standard error. A watch the runtime refuses is given up, said, and a change the runtime still hands // to it is refused saying so. A change names the instance's expiry while it has ended (design 47 H2 H-A-706 on DMC-13, // H-A-717 DQ-50). func WatchLedger(ledger, instances string, handle func(buscalls.InstanceChange) error) error { if handle == nil { return errors.New("a watch needs a handler") } c, err := live() if err != nil { return err } id := c.newID() f := newFollower("the watch "+id+" of the ledger "+ledger, handle, func(ch buscalls.InstanceChange) string { return ch.Instance }) c.mu.Lock() c.ledgerWatches[id] = f c.mu.Unlock() if _, err := c.askAs(id, buscalls.LedgerWatch, buscalls.LedgerWatchRequest{Ledger: ledger, Instances: instances, Watch: id}, 60*time.Second); err != nil { c.mu.Lock() delete(c.ledgerWatches, id) c.gaveUp(id, fmt.Sprintf("the ledger %s at %s: %v", ledger, time.Now().Format(time.RFC3339), err)) c.mu.Unlock() sayf("the watch %s of the ledger %s is given up: %v", id, ledger, err) return err } // The runtime hands every current state over before it answers the watch: each is in the follower by now. <-f.handled() return nil }