dispatcher
API
dispatcher
packageAPI reference for the dispatcher
package.
Imports
(4)
F
function
TestDispatcher_RegisterAndDispatch
Parameters
t
pkg/dispatcher/dispatcher_test.go:8-27
func TestDispatcher_RegisterAndDispatch(t *testing.T)
{
d := New()
d.Register("greet", func(ctx context.Context, payload ...any) (any, error) {
name := "world"
if len(payload) > 0 {
if s, ok := payload[0].(string); ok {
name = s
}
}
return "hello " + name, nil
})
result, err := d.Dispatch(context.Background(), "greet", "alice")
if err != nil {
t.Fatalf("Dispatch: %v", err)
}
if result != "hello alice" {
t.Errorf("result = %v, want hello alice", result)
}
}
F
function
TestDispatcher_NotFound
Parameters
t
pkg/dispatcher/dispatcher_test.go:29-35
func TestDispatcher_NotFound(t *testing.T)
{
d := New()
_, err := d.Dispatch(context.Background(), "missing")
if err == nil {
t.Fatal("expected error for missing handler")
}
}
F
function
TestDispatcher_Has
Parameters
t
pkg/dispatcher/dispatcher_test.go:37-48
func TestDispatcher_Has(t *testing.T)
{
d := New()
if d.Has("action") {
t.Error("Has should be false before register")
}
d.Register("action", func(ctx context.Context, payload ...any) (any, error) {
return nil, nil
})
if !d.Has("action") {
t.Error("Has should be true after register")
}
}
F
function
TestDispatcher_Names
Parameters
t
pkg/dispatcher/dispatcher_test.go:50-59
func TestDispatcher_Names(t *testing.T)
{
d := New()
d.Register("a", func(ctx context.Context, payload ...any) (any, error) { return nil, nil })
d.Register("b", func(ctx context.Context, payload ...any) (any, error) { return nil, nil })
names := d.Names()
if len(names) != 2 {
t.Errorf("Names length = %d, want 2", len(names))
}
}
F
function
TestDispatcher_DuplicatePanic
Parameters
t
pkg/dispatcher/dispatcher_test.go:61-70
func TestDispatcher_DuplicatePanic(t *testing.T)
{
defer func() {
if r := recover(); r == nil {
t.Error("expected panic on duplicate register")
}
}()
d := New()
d.Register("dup", func(ctx context.Context, payload ...any) (any, error) { return nil, nil })
d.Register("dup", func(ctx context.Context, payload ...any) (any, error) { return nil, nil })
}
T
type
HandlerFunc
HandlerFunc is a named dispatch handler.
pkg/dispatcher/dispatcher.go:10-10
type HandlerFunc func(ctx context.Context, payload ...any) (any, error)
S
struct
Dispatcher
Dispatcher provides synchronous named dispatch of handlers.
pkg/dispatcher/dispatcher.go:13-16
type Dispatcher struct
Methods
Register
Method
Register registers a handler by name. Panics if name is already registered.
Parameters
name
string
handler
HandlerFunc
func (*Dispatcher) Register(name string, handler HandlerFunc)
{
d.mu.Lock()
defer d.mu.Unlock()
if _, ok := d.handlers[name]; ok {
panic(fmt.Sprintf("dispatcher: handler %q already registered", name))
}
d.handlers[name] = handler
}
Dispatch
Method
Dispatch calls the handler registered under name, passing the payload. Returns an error if no handler is found.
Parameters
Returns
any
error
func (*Dispatcher) Dispatch(ctx context.Context, name string, payload ...any) (any, error)
{
d.mu.RLock()
handler, ok := d.handlers[name]
d.mu.RUnlock()
if !ok {
return nil, fmt.Errorf("dispatcher: no handler for %q", name)
}
return handler(ctx, payload...)
}
Has
Method
Has returns true if a handler is registered under name.
Parameters
name
string
Returns
bool
func (*Dispatcher) Has(name string) bool
{
d.mu.RLock()
defer d.mu.RUnlock()
_, ok := d.handlers[name]
return ok
}
Names
Method
Names returns all registered handler names.
Returns
[]string
func (*Dispatcher) Names() []string
{
d.mu.RLock()
defer d.mu.RUnlock()
names := make([]string, 0, len(d.handlers))
for name := range d.handlers {
names = append(names, name)
}
return names
}
Fields
| Name | Type | Description |
|---|---|---|
| mu | sync.RWMutex | |
| handlers | map[string]HandlerFunc |
F
function
New
New creates a new Dispatcher.
Returns
pkg/dispatcher/dispatcher.go:19-21
func New() *Dispatcher
{
return &Dispatcher{handlers: make(map[string]HandlerFunc)}
}