matrix: connect() = add action or add receiver to existing action
This commit is contained in:
parent
e55ce25190
commit
c5f1fe6e81
3 changed files with 29 additions and 6 deletions
|
@ -21,6 +21,10 @@ func (spec *baseSpec) Receivers() []lib.Addressable {
|
||||||
return spec.receivers
|
return spec.receivers
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (spec *baseSpec) AddReceiver(rcv lib.Addressable) {
|
||||||
|
spec.receivers = append(spec.receivers, rcv)
|
||||||
|
}
|
||||||
|
|
||||||
func Base(hdlr lib.ActionHandler, rcvs ...string) *baseSpec {
|
func Base(hdlr lib.ActionHandler, rcvs ...string) *baseSpec {
|
||||||
spec := baseSpec{handler: hdlr}
|
spec := baseSpec{handler: hdlr}
|
||||||
for _, rcv := range rcvs {
|
for _, rcv := range rcvs {
|
||||||
|
@ -29,6 +33,10 @@ func Base(hdlr lib.ActionHandler, rcvs ...string) *baseSpec {
|
||||||
return &spec
|
return &spec
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BaseA(hdlr lib.ActionHandler, rcvs ...lib.Addressable) *baseSpec {
|
||||||
|
return &baseSpec{hdlr, rcvs}
|
||||||
|
}
|
||||||
|
|
||||||
// action selection
|
// action selection
|
||||||
|
|
||||||
func Select(ctx lib.Context, msg lib.Message) []lib.Action {
|
func Select(ctx lib.Context, msg lib.Message) []lib.Action {
|
||||||
|
|
|
@ -14,13 +14,11 @@ func DefaultConfig(name string) lib.Config {
|
||||||
|
|
||||||
func create(act lib.Action) bool {
|
func create(act lib.Action) bool {
|
||||||
ctx := act.Context()
|
ctx := act.Context()
|
||||||
spec := act.Spec()
|
//spec := act.Spec()
|
||||||
msg := act.Message()
|
msg := act.Message()
|
||||||
cctx := createCell(ctx)
|
cctx := createCell(ctx)
|
||||||
//nmsg := message.New("matrix", "connect").WithSender(cctx)
|
connect(ctx, cctx, msg)
|
||||||
//msg.Sender().Cell().Send(nmsg)
|
connect(cctx, msg.Sender().Cell(), msg)
|
||||||
connect(ctx, cctx, spec, msg)
|
|
||||||
connect(cctx, msg.Sender().Cell(), spec, msg)
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,5 +29,21 @@ func createCell(ctx lib.Context) lib.Context {
|
||||||
return cctx
|
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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -83,6 +83,7 @@ type Action interface {
|
||||||
type ActionSpec interface {
|
type ActionSpec interface {
|
||||||
Handler() ActionHandler
|
Handler() ActionHandler
|
||||||
Receivers() []Addressable
|
Receivers() []Addressable
|
||||||
|
AddReceiver(Addressable)
|
||||||
}
|
}
|
||||||
|
|
||||||
// procedures and handlers
|
// procedures and handlers
|
||||||
|
|
Loading…
Add table
Reference in a new issue