fix: prevent double IPC registration + empty service placeholder
- HandleIPCEvents only auto-registered for services the factory didn't
register itself (prevents double handler registration)
- Auto-discovery only creates Service{} placeholder when factory didn't
call c.Service() — factories that register themselves keep full lifecycle
Addresses Codex review findings 1 and 2 from third pass.
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
b5dcdbb216
commit
3a9ac82275
1 changed files with 13 additions and 12 deletions
25
contract.go
25
contract.go
|
|
@ -146,10 +146,10 @@ func WithService(factory func(*Core) Result) CoreOption {
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the factory returned a service instance, auto-discover and register
|
// If the factory returned a service instance, auto-discover and register.
|
||||||
|
// Only applies when the factory didn't register the service itself.
|
||||||
if r.Value != nil {
|
if r.Value != nil {
|
||||||
instance := r.Value
|
instance := r.Value
|
||||||
// Service name discovery from package path
|
|
||||||
typeOf := reflect.TypeOf(instance)
|
typeOf := reflect.TypeOf(instance)
|
||||||
if typeOf.Kind() == reflect.Ptr {
|
if typeOf.Kind() == reflect.Ptr {
|
||||||
typeOf = typeOf.Elem()
|
typeOf = typeOf.Elem()
|
||||||
|
|
@ -159,18 +159,19 @@ func WithService(factory func(*Core) Result) CoreOption {
|
||||||
name := Lower(parts[len(parts)-1])
|
name := Lower(parts[len(parts)-1])
|
||||||
|
|
||||||
if name != "" {
|
if name != "" {
|
||||||
// IPC handler discovery
|
// Only auto-register if the factory didn't already do it
|
||||||
instanceValue := reflect.ValueOf(instance)
|
|
||||||
handlerMethod := instanceValue.MethodByName("HandleIPCEvents")
|
|
||||||
if handlerMethod.IsValid() {
|
|
||||||
if handler, ok := handlerMethod.Interface().(func(*Core, Message) Result); ok {
|
|
||||||
c.RegisterAction(handler)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Register the service if not already registered by the factory
|
|
||||||
if sr := c.Service(name); !sr.OK {
|
if sr := c.Service(name); !sr.OK {
|
||||||
c.Service(name, Service{})
|
c.Service(name, Service{})
|
||||||
|
|
||||||
|
// IPC handler discovery — only on auto-registered services
|
||||||
|
// to avoid double-registration when the factory already wired handlers
|
||||||
|
instanceValue := reflect.ValueOf(instance)
|
||||||
|
handlerMethod := instanceValue.MethodByName("HandleIPCEvents")
|
||||||
|
if handlerMethod.IsValid() {
|
||||||
|
if handler, ok := handlerMethod.Interface().(func(*Core, Message) Result); ok {
|
||||||
|
c.RegisterAction(handler)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue