// Package `scopes` provides a set of core types (mostly interfaces // and function declarations) and some basic functions. package scopes import ( stdlib_context "context" "fmt" "sync" ) // services - context type Services map[string]Context type ContextState interface{} type Context interface { stdlib_context.Context Addressable Config() Config Name() string Parent() Context Services() Services ChildContext(Config) Context State() ContextState WithState(ContextState) Context Mailbox() chan Message WaitGroup() *sync.WaitGroup Stop() Send(Message) } // message, address type MsgHead interface { fmt.Stringer Slice() []string Domain() string Action() string Class() string Item() string } type Message interface { MsgHead Head() MsgHead Sender() Addressable Payload() Payload WithSender(Addressable) Message WithPayload(Payload) Message } type Addressable interface { Address() Address Cell() Context } type Address interface { fmt.Stringer Addressable Url() string Service() string Interaction() (string, string) SetUrl(string) SetInteraction(string, string) } type Payload interface { fmt.Stringer Data() interface{} FromJson(string) Payload } // action type Action interface { Context() Context Spec() ActionSpec Message() Message Handle() bool } type ActionSpec interface { Handler() ActionHandler Receivers() []string } // procedures and handlers type Proc = func(Context) type StartProc = Proc type StepProc = func(Context) bool type MessageHandler = func(Context, Message) bool type ActionHandler = func(Action) bool // standard configuration interfaces type Config interface { Name() string SetName(string) Starter() StartProc Step() StepProc MessageHandler() MessageHandler DoneHandler() StepProc Actions() []ActionConfig AddAction(string, ...ActionSpec) Config Children() []Config Add(...Config) Config } type ActionConfig interface { Pattern() Pattern Specs() []ActionSpec } type Pattern interface { fmt.Stringer Slice() []string } // core library functions func RunCtx(ctx Context, fct Proc) { ctx.WaitGroup().Add(1) go func() { defer ctx.WaitGroup().Done() fct(ctx) }() } func HandleMsg(ctx Context, msg Message) bool { return ctx.Config().MessageHandler()(ctx, msg) } func Send(ctx Context, addr Address, msg Message) { if msg.Sender() == nil { msg.WithSender(ctx) } if svc, ok := ctx.Services()[addr.Service()]; ok { // TODO: check Address for sid, iid svc.Send(msg) } // TODO: Warn().Msg("receiver not found") } func GetState[St ContextState](ctx Context) St { return ctx.State().(St) } func GetCfg[C Config](ctx Context) C { return ctx.Config().(C) }