Merge pull request #461 from go-redis/fix/subscribe-without-channel

Allow creating PubSub without channels.
This commit is contained in:
Vladimir Mihailenco 2016-12-22 13:46:03 +02:00 committed by GitHub
commit 176e8309e2
1 changed files with 14 additions and 2 deletions

View File

@ -359,11 +359,23 @@ func (c *Client) pubSub() *PubSub {
// Subscribe subscribes the client to the specified channels. // Subscribe subscribes the client to the specified channels.
func (c *Client) Subscribe(channels ...string) (*PubSub, error) { func (c *Client) Subscribe(channels ...string) (*PubSub, error) {
pubsub := c.pubSub() pubsub := c.pubSub()
return pubsub, pubsub.Subscribe(channels...) if len(channels) > 0 {
if err := pubsub.Subscribe(channels...); err != nil {
pubsub.Close()
return nil, err
}
}
return pubsub, nil
} }
// PSubscribe subscribes the client to the given patterns. // PSubscribe subscribes the client to the given patterns.
func (c *Client) PSubscribe(channels ...string) (*PubSub, error) { func (c *Client) PSubscribe(channels ...string) (*PubSub, error) {
pubsub := c.pubSub() pubsub := c.pubSub()
return pubsub, pubsub.PSubscribe(channels...) if len(channels) > 0 {
if err := pubsub.PSubscribe(channels...); err != nil {
pubsub.Close()
return nil, err
}
}
return pubsub, nil
} }