The view's process follows the trunk's catalog half (first view of the seat mesh-catalog's verb revision, then each catalog-revision of branch main) and hands each changed entry to its model in the form of the value it always kept; its tools read the newest trunk revision through the verb. The manifest invokes seat:mesh-catalog.revision and consumes mesh-catalog.catalog-revision instead of the indexer's modules read tool and views family. Builds: design 47 G §196, test 61 Claude-Session: https://claude.ai/code/session_01JG73KEq4zoDfM9WtYpcQgZ
84 lines
2.3 KiB
Go
84 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"git.novox.be/novox/mesh-delivery/contracts/go/clients/meshcatalogclient"
|
|
"sort"
|
|
"sync"
|
|
"time"
|
|
|
|
"mesh-delivery-view/internal/view"
|
|
)
|
|
|
|
// catalogWithin is how long a call waits for the seat mesh-catalog's first view before it says there is none.
|
|
var catalogWithin = 30 * time.Second
|
|
|
|
// catalogRead is the modules the view's tools answer, read from the catalog half through the seat mesh-catalog's verb
|
|
// `revision` (design 47, Two graphs): the trunk's newest revision, asked once per call and in pages, each entry in the
|
|
// form of the value the tools always answered. With no view the read says so; it never answers no modules.
|
|
func catalogRead(ask func(key string, body any) (json.RawMessage, error)) view.StateRead {
|
|
var (
|
|
once sync.Once
|
|
values map[string]json.RawMessage
|
|
rev uint64
|
|
failed error
|
|
)
|
|
load := func() {
|
|
ctx, cancel := context.WithTimeout(context.Background(), catalogWithin)
|
|
defer cancel()
|
|
c, err := meshcatalogclient.NewCatalog(ask, "main")
|
|
if err != nil {
|
|
failed = err
|
|
return
|
|
}
|
|
if err := c.Start(ctx); err != nil {
|
|
failed = fmt.Errorf("the catalog half has no first view yet (%s): %w", meshcatalogclient.RevisionVerb, err)
|
|
return
|
|
}
|
|
half, ok := c.View()
|
|
if !ok {
|
|
failed = fmt.Errorf("the catalog half has no first view yet (%s)", meshcatalogclient.RevisionVerb)
|
|
return
|
|
}
|
|
rev, values = uint64(half.Revision.Revision), map[string]json.RawMessage{}
|
|
for module, e := range half.Entries {
|
|
raw, _ := json.Marshal(e)
|
|
v, err := view.ModuleValue(raw)
|
|
if err != nil {
|
|
failed = fmt.Errorf("the catalog entry of %s: %w", module, err)
|
|
return
|
|
}
|
|
values[module] = v
|
|
}
|
|
}
|
|
read := func() (map[string]json.RawMessage, uint64, error) {
|
|
once.Do(load)
|
|
return values, rev, failed
|
|
}
|
|
return view.StateRead{
|
|
Keys: func() ([]string, error) {
|
|
v, _, err := read()
|
|
keys := make([]string, 0, len(v))
|
|
for k := range v {
|
|
keys = append(keys, k)
|
|
}
|
|
sort.Strings(keys)
|
|
return keys, err
|
|
},
|
|
Get: func(key string) (json.RawMessage, error) {
|
|
v, _, err := read()
|
|
return v[key], err
|
|
},
|
|
GetEntry: func(key string) (json.RawMessage, uint64, error) {
|
|
v, r, err := read()
|
|
return v[key], r, err
|
|
},
|
|
All: func() (map[string]json.RawMessage, error) {
|
|
v, _, err := read()
|
|
return v, err
|
|
},
|
|
}
|
|
}
|