It asks and cancels as accepted kinds of the seat operator-channel through the connection's Accept, and reads the router's record by a get in the seat holder's state asks, never through a bucket named in code. Builds: design 47 G §677, §700; lead answers A-272, A-273 Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
52 lines
2.4 KiB
Go
52 lines
2.4 KiB
Go
package operatorchannelclient
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
seat "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel"
|
|
stdio "git.novox.be/novox/mesh-sdk/go"
|
|
"git.novox.be/novox/mesh-sdk/go/process"
|
|
)
|
|
|
|
// asksRead is a state of another module read by key: process.State is one.
|
|
type asksRead interface {
|
|
Get(key string) (*stdio.StateEntry, error)
|
|
}
|
|
|
|
// NewGateChannel is the operator channel as a process holder's gate reaches it over its own connection (design 47 G §677,
|
|
// §700; lead answers A-272, A-273): an ask and its cancel are sent on the seat operator-channel's accept subjects through
|
|
// conn's Accept, and the router's record is read by a get of the key `<module>.<id>` in the seat's state asks, whose bucket
|
|
// is formed from holder (the module the seat list names for the seat operator-channel) and the contract's StateAsks, on the
|
|
// process's own account. state keeps the asks, under keyPrefix (the gate's state approvals and its node).
|
|
func NewGateChannel(conn *process.Conn, state Store, keyPrefix string, holder func() (string, error)) *GateChannel {
|
|
return gateChannelOver(conn.Accept, func(read string) asksRead { return conn.Read(read) }, state, keyPrefix, holder)
|
|
}
|
|
|
|
// gateChannelOver is NewGateChannel over the two things it reaches a connection through.
|
|
func gateChannelOver(accept func(seat, kind string, body any, opts ...stdio.EmitOptions) error, read func(read string) asksRead,
|
|
state Store, keyPrefix string, holder func() (string, error)) *GateChannel {
|
|
return &GateChannel{State: state, KeyPrefix: keyPrefix,
|
|
Publish: func(module, subject string, body any, id string) error {
|
|
var kind string
|
|
switch subject {
|
|
case seat.AcceptSubjectAsk(module):
|
|
kind = seat.AcceptAsk
|
|
case seat.AcceptSubjectCancel(module):
|
|
kind = seat.AcceptCancel
|
|
default:
|
|
return fmt.Errorf("%s is no accept subject of the seat %s that %s may use", subject, seat.Seat, module)
|
|
}
|
|
return accept(seat.Seat, kind, body, stdio.EmitOptions{MessageID: id})
|
|
},
|
|
ReadRecord: func(module, id string) (*stdio.StateEntry, error) {
|
|
held, err := holder()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("the router's record of %s cannot be read: %w", id, err)
|
|
}
|
|
if held == "" {
|
|
return nil, fmt.Errorf("the router's record of %s cannot be read: the seat list names no holder of the seat %s", id, seat.Seat)
|
|
}
|
|
return read(held + "." + seat.StateAsks).Get(seat.AsksByAskerIDKey(module, id))
|
|
}}
|
|
}
|