package dregarnuhr import ( "context" "errors" "fmt" "strings" "sync" ) type Handler func(context.Context, *Job) error // Registry holds existing event identifiers, not new provider or consumer IDs. type Registry struct { mu sync.RWMutex handlers map[string]Handler } func (r *Registry) Register(kind string, handler Handler) error { if strings.TrimSpace(kind) == "" || handler == nil { return errors.New("event type and handler are required") } r.mu.Lock() defer r.mu.Unlock() if r.handlers == nil { r.handlers = make(map[string]Handler) } if _, ok := r.handlers[kind]; ok { return fmt.Errorf("duplicate event type %q", kind) } r.handlers[kind] = handler return nil } func (r *Registry) Run(ctx context.Context, job *Job) error { if job == nil { return errors.New("job is required") } if err := ctx.Err(); err != nil { return err } r.mu.RLock() handler := r.handlers[job.Type] r.mu.RUnlock() if handler == nil { return fmt.Errorf("unknown event type %q", job.Type) } return handler(ctx, job) }