matrix: connect() = add action or add receiver to existing action

This commit is contained in:
Helmut Merz 2023-07-25 12:36:58 +02:00
parent e55ce25190
commit c5f1fe6e81
3 changed files with 29 additions and 6 deletions

View file

@ -21,6 +21,10 @@ func (spec *baseSpec) Receivers() []lib.Addressable {
return spec.receivers
}
func (spec *baseSpec) AddReceiver(rcv lib.Addressable) {
spec.receivers = append(spec.receivers, rcv)
}
func Base(hdlr lib.ActionHandler, rcvs ...string) *baseSpec {
spec := baseSpec{handler: hdlr}
for _, rcv := range rcvs {
@ -29,6 +33,10 @@ func Base(hdlr lib.ActionHandler, rcvs ...string) *baseSpec {
return &spec
}
func BaseA(hdlr lib.ActionHandler, rcvs ...lib.Addressable) *baseSpec {
return &baseSpec{hdlr, rcvs}
}
// action selection
func Select(ctx lib.Context, msg lib.Message) []lib.Action {

View file

@ -14,13 +14,11 @@ func DefaultConfig(name string) lib.Config {
func create(act lib.Action) bool {
ctx := act.Context()
spec := act.Spec()
//spec := act.Spec()
msg := act.Message()
cctx := createCell(ctx)
//nmsg := message.New("matrix", "connect").WithSender(cctx)
//msg.Sender().Cell().Send(nmsg)
connect(ctx, cctx, spec, msg)
connect(cctx, msg.Sender().Cell(), spec, msg)
connect(ctx, cctx, msg)
connect(cctx, msg.Sender().Cell(), msg)
return true
}
@ -31,5 +29,21 @@ func createCell(ctx lib.Context) lib.Context {
return cctx
}
func connect(src, tgt lib.Context, spec lib.ActionSpec, msg lib.Message) {
func connect(src, tgt lib.Context, msg lib.Message) {
cfg := src.Config()
pattern := "matrix|data"
for _, act := range cfg.Actions() {
if act.Pattern().String() == pattern {
for _, spec := range act.Specs() {
spec.AddReceiver(tgt)
return
}
}
}
cfg.AddAction(pattern, action.BaseA(handle, tgt))
}
func handle(act lib.Action) bool {
// here comes the real stuff
return action.Forward(act)
}

View file

@ -83,6 +83,7 @@ type Action interface {
type ActionSpec interface {
Handler() ActionHandler
Receivers() []Addressable
AddReceiver(Addressable)
}
// procedures and handlers