Extract Grammaratur, Dregarnuhr, Geduldh, Mestionora, Ordoschnellic, Schutzaria, Kunstzeal and Seheweit behind compatible application contracts. Include accumulated editor resilience, navigation and theme UX, remote CLI, storage/schema and DR groundwork, tests and design/operations documentation. Verified: uncached full Go suite; go vet ./...; race tests for all eight components; 11 targeted Chromium tests; Node suites; 19 Python CLI tests; reproducible editor build. No push, release tag or deployment in this checkpoint.
50 lines
1.0 KiB
Go
50 lines
1.0 KiB
Go
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)
|
|
}
|