go-scopes/matrix/matrix.go

60 lines
1.3 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)
pattern := config.Pattern(msg.Domain(), "data", msg.Class(), msg.Item()).String()
connect(ctx, cctx, pattern)
connect(cctx, msg.Sender().Cell(), pattern)
return true
}
func createCell(ctx lib.Context) lib.Context {
cfg := DefaultConfig("")
cctx := ctx.ChildContext(cfg) // .WithState(...)
cfg.Starter()(cctx)
return cctx
}
func connect(src, tgt lib.Context, pattern string) {
cfg := src.Config()
for _, act := range cfg.Actions() {
if act.Pattern().String() == pattern {
for _, spec := range act.Specs() {
spec.AddReceiver(tgt)
return
}
}
}
cfg.AddAction(pattern, action.Base(handle).AddReceiver(tgt))
}
func disconnect(src, tgt lib.Context) { //, pattern string) {
cfg := src.Config()
for _, act := range cfg.Actions() {
//if act.Pattern().String() == pattern {
for _, spec := range act.Specs() {
spec.RemoveReceiver(tgt)
}
//}
}
}
func handle(act lib.Action) bool {
// here comes the real stuff
return action.Forward(act)
}