diff --git a/push_notifications.go b/push_notifications.go index ec251ed2..b49e6cfe 100644 --- a/push_notifications.go +++ b/push_notifications.go @@ -225,10 +225,8 @@ const ( // PushNotificationInfo contains metadata about a push notification. type PushNotificationInfo struct { - Command string - Args []interface{} - Timestamp int64 - Source string + Command string + Args []interface{} } // ParsePushNotificationInfo extracts information from a push notification. diff --git a/redis.go b/redis.go index 67188875..c45ba953 100644 --- a/redis.go +++ b/redis.go @@ -982,6 +982,11 @@ func newConn(opt *Options, connPool pool.Pooler, parentHooks *hooksMixin) *Conn c.hooksMixin = parentHooks.clone() } + // Set push notification processor if available in options + if opt.PushNotificationProcessor != nil { + c.pushProcessor = opt.PushNotificationProcessor + } + c.cmdable = c.Process c.statefulCmdable = c.Process c.initHooks(hooks{ @@ -1000,6 +1005,29 @@ func (c *Conn) Process(ctx context.Context, cmd Cmder) error { return err } +// RegisterPushNotificationHandler registers a handler for a specific push notification command. +// Returns an error if a handler is already registered for this command. +func (c *Conn) RegisterPushNotificationHandler(command string, handler PushNotificationHandler) error { + if c.pushProcessor != nil { + return c.pushProcessor.RegisterHandler(command, handler) + } + return nil +} + +// RegisterPushNotificationHandlerFunc registers a function as a handler for a specific push notification command. +// Returns an error if a handler is already registered for this command. +func (c *Conn) RegisterPushNotificationHandlerFunc(command string, handlerFunc func(ctx context.Context, notification []interface{}) bool) error { + if c.pushProcessor != nil { + return c.pushProcessor.RegisterHandlerFunc(command, handlerFunc) + } + return nil +} + +// GetPushNotificationProcessor returns the push notification processor. +func (c *Conn) GetPushNotificationProcessor() *PushNotificationProcessor { + return c.pushProcessor +} + func (c *Conn) Pipelined(ctx context.Context, fn func(Pipeliner) error) ([]Cmder, error) { return c.Pipeline().Pipelined(ctx, fn) }