go-scopes/matrix/matrix.go

49 lines
1.1 KiB
Go

package matrix
import (
lib "git.sr.ht/~cco/go-scopes"
"git.sr.ht/~cco/go-scopes/config"
"git.sr.ht/~cco/go-scopes/core/action"
)
func DefaultConfig(name string) lib.Config {
cfg := config.Default(name)
cfg.AddAction("create", action.Base(create))
return cfg
}
func create(act lib.Action) bool {
ctx := act.Context()
//spec := act.Spec()
msg := act.Message()
cctx := createCell(ctx)
connect(ctx, cctx, msg)
connect(cctx, msg.Sender().Cell(), msg)
return true
}
func createCell(ctx lib.Context) lib.Context {
cfg := DefaultConfig("")
cctx := ctx.ChildContext(cfg)
cfg.Starter()(cctx)
return cctx
}
func connect(src, tgt lib.Context, msg lib.Message) {
cfg := src.Config()
pattern := config.Pattern(msg.Domain(), "data", msg.Class(), msg.Item()).String()
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)
}