You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.6 KiB

  1. package push
  2. import (
  3. "sync"
  4. )
  5. // Registry manages push notification handlers
  6. type Registry struct {
  7. mu sync.RWMutex
  8. handlers map[string]NotificationHandler
  9. protected map[string]bool
  10. }
  11. // NewRegistry creates a new push notification registry
  12. func NewRegistry() *Registry {
  13. return &Registry{
  14. handlers: make(map[string]NotificationHandler),
  15. protected: make(map[string]bool),
  16. }
  17. }
  18. // RegisterHandler registers a handler for a specific push notification name
  19. func (r *Registry) RegisterHandler(pushNotificationName string, handler NotificationHandler, protected bool) error {
  20. if handler == nil {
  21. return ErrHandlerNil
  22. }
  23. r.mu.Lock()
  24. defer r.mu.Unlock()
  25. // Check if handler already exists
  26. if _, exists := r.protected[pushNotificationName]; exists {
  27. return ErrHandlerExists(pushNotificationName)
  28. }
  29. r.handlers[pushNotificationName] = handler
  30. r.protected[pushNotificationName] = protected
  31. return nil
  32. }
  33. // GetHandler returns the handler for a specific push notification name
  34. func (r *Registry) GetHandler(pushNotificationName string) NotificationHandler {
  35. r.mu.RLock()
  36. defer r.mu.RUnlock()
  37. return r.handlers[pushNotificationName]
  38. }
  39. // UnregisterHandler removes a handler for a specific push notification name
  40. func (r *Registry) UnregisterHandler(pushNotificationName string) error {
  41. r.mu.Lock()
  42. defer r.mu.Unlock()
  43. // Check if handler is protected
  44. if protected, exists := r.protected[pushNotificationName]; exists && protected {
  45. return ErrProtectedHandler(pushNotificationName)
  46. }
  47. delete(r.handlers, pushNotificationName)
  48. delete(r.protected, pushNotificationName)
  49. return nil
  50. }