Class: Redis::PubSub::Subscription

Inherits:
Object
  • Object
show all
Includes:
EventMachine::Deferrable
Defined in:
lib/redis/pubsub.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Subscription

Returns a new instance of Subscription.



28
29
30
31
32
# File 'lib/redis/pubsub.rb', line 28

def initialize connection
  @connection = connection
  @subs = []
  @psubs = []
end

Class Method Details

.publish(channel, message) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/redis/pubsub.rb', line 12

def self.publish channel, message
  subs = @@channels[channel] || []
  subs.each do |sub|
    sub.call channel, message
  end
  sent = subs.size
  (@@pchannels || []).each do |pchannel, subs|
    next unless File.fnmatch(pchannel, channel)
    subs.each do |sub|
      sub.pcall pchannel, channel, message
    end
    sent += subs.size
  end
  sent
end

Instance Method Details

#boundObject



34
35
36
# File 'lib/redis/pubsub.rb', line 34

def bound
  size > 0
end

#call(channel, message) ⇒ Object



91
92
93
# File 'lib/redis/pubsub.rb', line 91

def call channel, message
  @connection.send_redis ['message', channel, message]
end

#pcall(pchannel, channel, message) ⇒ Object



95
96
97
# File 'lib/redis/pubsub.rb', line 95

def pcall pchannel, channel, message
  @connection.send_redis ['pmessage', pchannel, channel, message]
end

#psubscribe(channel) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/redis/pubsub.rb', line 70

def psubscribe channel
  c = (@@pchannels[channel] ||= [])
  unless c.include? self
    c << self 
    @psubs << channel
  end
  @connection.send_redis ['psubscribe', channel, size]
end

#punsubscribe(channel) ⇒ Object



85
86
87
88
89
# File 'lib/redis/pubsub.rb', line 85

def punsubscribe channel
  c = (@@pchannels[channel] || [])
  @psubs.delete channel if c.delete self
  @connection.send_redis ['punsubscribe', channel, size]
end

#sizeObject



38
39
40
# File 'lib/redis/pubsub.rb', line 38

def size
  @subs.size + @psubs.size
end

#subscribe(channel) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/redis/pubsub.rb', line 61

def subscribe channel
  c = (@@channels[channel] ||= [])
  unless c.include? self
    c << self 
    @subs << channel
  end
  @connection.send_redis ['subscribe', channel, size]
end

#unbindObject



42
43
44
45
# File 'lib/redis/pubsub.rb', line 42

def unbind
  unbind_channels
  unbind_patterns
end

#unbind_channelsObject



47
48
49
50
51
52
# File 'lib/redis/pubsub.rb', line 47

def unbind_channels
  @subs.each do |channel|
    (@@channels[channel] || []).delete self
  end
  @subs.clear
end

#unbind_patternsObject



54
55
56
57
58
59
# File 'lib/redis/pubsub.rb', line 54

def unbind_patterns
  @psubs.each do |channel|
    (@@pchannels[channel] || []).delete self
  end
  @psubs.clear
end

#unsubscribe(channel) ⇒ Object



79
80
81
82
83
# File 'lib/redis/pubsub.rb', line 79

def unsubscribe channel
  c = (@@channels[channel] || [])
  @subs.delete channel if c.delete self
  @connection.send_redis ['unsubscribe', channel, size]
end