Class: Pubsubstub::Subscription

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/pubsubstub/subscription.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#debug, #error, #info

Constructor Details

#initialize(channels, connection) ⇒ Subscription

Returns a new instance of Subscription.



7
8
9
10
11
12
# File 'lib/pubsubstub/subscription.rb', line 7

def initialize(channels, connection)
  @id = Random.rand(2 ** 64)
  @connection = connection
  @channels = channels
  @queue = Queue.new
end

Instance Attribute Details

#channelsObject (readonly)

Returns the value of attribute channels.



5
6
7
# File 'lib/pubsubstub/subscription.rb', line 5

def channels
  @channels
end

#connectionObject (readonly)

Returns the value of attribute connection.



5
6
7
# File 'lib/pubsubstub/subscription.rb', line 5

def connection
  @connection
end

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/pubsubstub/subscription.rb', line 5

def id
  @id
end

#queueObject (readonly)

Returns the value of attribute queue.



5
6
7
# File 'lib/pubsubstub/subscription.rb', line 5

def queue
  @queue
end

Instance Method Details

#push(event) ⇒ Object



27
28
29
# File 'lib/pubsubstub/subscription.rb', line 27

def push(event)
  queue.push(event)
end

#stream(last_event_id) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/pubsubstub/subscription.rb', line 14

def stream(last_event_id)
  info { "Connecting client ##{id} (#{channels.map(&:name).join(', ')})" }
  subscribe
  fetch_scrollback(last_event_id)
  while event = queue.pop
    debug { "Sending event ##{event.id} to client ##{id}"}
    connection << event.to_message
  end
ensure
  info { "Disconnecting client ##{id}" }
  unsubscribe
end