优化结构
This commit is contained in:
@@ -17,13 +17,6 @@ type Method struct {
|
||||
Run MethodFunc
|
||||
}
|
||||
|
||||
// Contributor registers task methods whose handlers need constructed runtime
|
||||
// dependencies. Modules expose a contributor and the application composition
|
||||
// root activates it; the task runtime never imports business modules.
|
||||
type Contributor interface {
|
||||
RegisterTasks(*Registry)
|
||||
}
|
||||
|
||||
type Registry struct {
|
||||
mu sync.RWMutex
|
||||
methods map[string]Method
|
||||
@@ -31,17 +24,6 @@ type Registry struct {
|
||||
|
||||
func NewRegistry() *Registry { return &Registry{methods: make(map[string]Method)} }
|
||||
|
||||
func Apply(registry *Registry, contributors ...Contributor) {
|
||||
if registry == nil {
|
||||
return
|
||||
}
|
||||
for _, contributor := range contributors {
|
||||
if contributor != nil {
|
||||
contributor.RegisterTasks(registry)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Registry) Register(method Method) {
|
||||
if r == nil || method.Name == "" || method.Run == nil {
|
||||
return
|
||||
|
||||
@@ -15,18 +15,3 @@ func TestRegistryIsComposableAndIdempotent(t *testing.T) {
|
||||
t.Fatalf("unexpected methods: %#v", methods)
|
||||
}
|
||||
}
|
||||
|
||||
type testContributor struct{ name string }
|
||||
|
||||
func (contributor testContributor) RegisterTasks(registry *Registry) {
|
||||
registry.Register(Method{Name: contributor.name, Run: func(context.Context, json.RawMessage) error { return nil }})
|
||||
}
|
||||
|
||||
func TestApplyCollectsModuleContributors(t *testing.T) {
|
||||
registry := NewRegistry()
|
||||
Apply(registry, testContributor{name: "system.cleanup"}, testContributor{name: "orders.expire"})
|
||||
methods := registry.List()
|
||||
if len(methods) != 2 || methods[0].Name != "orders.expire" || methods[1].Name != "system.cleanup" {
|
||||
t.Fatalf("unexpected contributed methods: %#v", methods)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user