go-scopes/lib/lib.go

69 lines
1.2 KiB
Go

// 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
Actions() []ActionConfig
Children() []Config
Add(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
HandleMsg(Message) bool
WaitGroup() *sync.WaitGroup
Stop()
}
type Message interface {
Action() string
}
type Action interface {
Context() Context
Spec() ActionSpec
Message() Message
}
type Proc = func(Context)
type StartProc = Proc
type MessageHandler = func(Context, Message)
type ActionHandler = func(Action)
// async Runners
func RunCtx(ctx Context, fct Proc) {
ctx.WaitGroup().Add(1)
go func() {
defer ctx.WaitGroup().Done()
fct(ctx)
}()
}