// Package `lib` provides a set of common types (mostly interfaces // and function declarations) and some basic functions. package lib import ( stdlib_context "context" "sync" ) type ActionSpec interface { Handler() ActionHandler Receivers() []string } type ActionConfig interface { Pattern() string Specs() []ActionSpec } type Config interface { Name() string Starter() StartProc Step() StepProc MessageHandler() MessageHandler Actions() []ActionConfig AddAction(string, ...ActionSpec) Config Children() []Config Add(...Config) Config } type Services map[string]Context type ContextState interface{} type Context interface { stdlib_context.Context Config() Config Parent() Context Services() Services ChildContext(Config) Context State() ContextState WithState(ContextState) Context Mailbox() chan Message WaitGroup() *sync.WaitGroup Stop() } type Address interface { Service() string Send(Context, Message) } type Message interface { Action() string Sender() Address } type Action interface { Context() Context Spec() ActionSpec Message() Message Handle() bool } type Proc = func(Context) type StartProc = Proc type StepProc = func(Context) bool type MessageHandler = func(Context, Message) bool type ActionHandler = func(Action) bool // 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 srv, ok := ctx.Services()[addr.Service()]; ok { srv.Mailbox() <- msg } }