G findings: mesh-config's and mesh-vault's own followers go through a views.Reader holding the writer, adopted
Builds: design 47 G A-28, A-26, A-22, §687 Claude-Session: https://claude.ai/code/session_01JG73KEq4zoDfM9WtYpcQgZ
This commit is contained in:
@@ -21,7 +21,6 @@ import (
|
||||
"time"
|
||||
|
||||
stdio "git.novox.be/novox/mesh-sdk/go"
|
||||
|
||||
"git.novox.be/novox/mesh-sdk/go/views"
|
||||
)
|
||||
|
||||
@@ -49,19 +48,20 @@ var followed = []struct{ Read, Owner, State, Tool string }{
|
||||
{WantsRead, assignerModule, "wants", AssignerWantsTool},
|
||||
}
|
||||
|
||||
// ownerViews are the pictures, by the read name.
|
||||
// ownerViews are the pictures, by the read name, followed through one views.Reader holding the module's writer (design 47 G
|
||||
// A-28): each follower is adopted, so the reader raises and clears its findings and hears its owner's views event.
|
||||
type ownerViews struct {
|
||||
by map[string]*views.Follower
|
||||
// findings keeps the findings a first view unanswered writes.
|
||||
findings views.FindingWriter
|
||||
by map[string]*views.Follower
|
||||
reader *views.Reader
|
||||
}
|
||||
|
||||
// newOwnerViews makes a follower for each state; changed is told when any picture changes.
|
||||
func newOwnerViews(ask func(key string, body any) (json.RawMessage, error), changed func(), findings views.FindingWriter) *ownerViews {
|
||||
o := &ownerViews{by: map[string]*views.Follower{}, findings: findings}
|
||||
// newOwnerViews makes a follower for each state and adopts it; changed is told when any picture changes. findings is the
|
||||
// module's writer, which the reader writes the first views' findings through.
|
||||
func newOwnerViews(ask func(key string, body any) (json.RawMessage, error), changed func(), findings views.WireWriter) *ownerViews {
|
||||
o := &ownerViews{by: map[string]*views.Follower{}, reader: &views.Reader{Module: Module, Findings: findings, Logf: logf}}
|
||||
for _, f := range followed {
|
||||
f := f
|
||||
fol := &views.Follower{Owner: f.Owner, Tool: f.Owner + "." + f.Tool, State: f.State, Logf: logf,
|
||||
fol := &views.Follower{Owner: f.Owner, Tool: f.Owner + "." + f.Tool, State: f.State,
|
||||
Ask: func(_ context.Context, after string) (views.Answer, error) {
|
||||
body := map[string]any{"values": true}
|
||||
if after != "" {
|
||||
@@ -78,54 +78,27 @@ func newOwnerViews(ask func(key string, body any) (json.RawMessage, error), chan
|
||||
return views.DecodeAnswer(answer)
|
||||
},
|
||||
Changed: func(string, json.RawMessage, bool, time.Time) { changed() }}
|
||||
fol.Raise = func(s views.Status) {
|
||||
_ = findings.Put(views.UnansweredFinding(Module, s, time.Now()))
|
||||
hear := func(ctx context.Context, body []byte) error {
|
||||
ev, err := views.DecodeEvent(body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if gap, why := fol.Apply(ev); gap {
|
||||
logf("[mesh-config] a gap in %s's %s (%s): its first view is asked once more", f.Owner, f.State, why)
|
||||
go func() { _ = fol.Start(ctx) }()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
fol.Clear = func(s views.Status) {
|
||||
_ = findings.Delete(views.UnansweredFinding(Module, s, time.Now()))
|
||||
if err := o.reader.Adopt(fol, f.Owner, views.EventName(f.State), hear); err != nil {
|
||||
logf("[mesh-config] %v", err)
|
||||
}
|
||||
o.by[f.Read] = fol
|
||||
}
|
||||
return o
|
||||
}
|
||||
|
||||
// Start asks every first view until answered (each on its goroutine) and subscribes to the owners' views events.
|
||||
func (o *ownerViews) Start(ctx context.Context) {
|
||||
for _, fol := range o.by {
|
||||
go func(f *views.Follower) {
|
||||
if err := f.Start(ctx); err != nil {
|
||||
logf("[mesh-config] the first view of %s's %s ended unanswered: %v", f.Owner, f.State, err)
|
||||
}
|
||||
}(fol)
|
||||
}
|
||||
for _, f := range followed {
|
||||
f := f
|
||||
pattern := f.Owner + "." + views.EventName(f.State)
|
||||
go func() {
|
||||
<-stdio.Served()
|
||||
for wait := 2 * time.Second; ; wait = min(wait*2, time.Minute) {
|
||||
err := stdio.Subscribe(pattern, func(e stdio.Envelope) error {
|
||||
ev, err := views.DecodeEvent(e.Body)
|
||||
if err != nil {
|
||||
logf("[mesh-config] %s is not a views event: %v", pattern, err)
|
||||
return nil
|
||||
}
|
||||
fol := o.by[f.Read]
|
||||
if gap, why := fol.Apply(ev); gap {
|
||||
logf("[mesh-config] a gap in %s's %s (%s): its first view is asked once more", f.Owner, f.State, why)
|
||||
go func() { _ = fol.Start(ctx) }()
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
logf("[mesh-config] does not hear %s yet (%v); asking again in %s", pattern, err, wait)
|
||||
time.Sleep(wait)
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
// Start subscribes the owners' views events and asks every first view until answered, each on its goroutine.
|
||||
func (o *ownerViews) Start(ctx context.Context) { o.reader.Start(ctx) }
|
||||
|
||||
// read is the picture of a read name, and whether the name is one followed.
|
||||
func (o *ownerViews) read(state string) (*views.Follower, bool) {
|
||||
|
||||
@@ -36,6 +36,9 @@ func TestAFollowedStateIsReadFromItsOwnersFirstViewAndEvents(t *testing.T) {
|
||||
if _, followed, err := o.get(WantsRead, "want.g14.dunst"); !followed || err == nil || !strings.Contains(err.Error(), "not answered yet") {
|
||||
t.Fatalf("before the first view: followed %v, %v", followed, err)
|
||||
}
|
||||
if !o.reader.Follows("mesh-delivery-assigner.wants") {
|
||||
t.Error("the wants view is not one of the reader's (A-28: adopted)")
|
||||
}
|
||||
if _, followed, _ := o.get(BuildsRead, "x"); followed {
|
||||
t.Error("the builder's builds is followed, and it has no owner tool")
|
||||
}
|
||||
@@ -78,5 +81,6 @@ func TestAFollowedStateIsReadFromItsOwnersFirstViewAndEvents(t *testing.T) {
|
||||
// nopFindings takes a finding and keeps none.
|
||||
type nopFindings struct{}
|
||||
|
||||
func (nopFindings) Put(views.Finding) error { return nil }
|
||||
func (nopFindings) Delete(views.Finding) error { return nil }
|
||||
func (nopFindings) WriteWire(context.Context, map[string]any) error { return nil }
|
||||
func (nopFindings) Clear(_ context.Context, _, _, _, _ string) error { return nil }
|
||||
func (nopFindings) ModuleNode() (string, string) { return Module, "" }
|
||||
|
||||
Reference in New Issue
Block a user