Class: Faye::Subscription

Inherits:
Object
  • Object
show all
Includes:
Deferrable
Defined in:
lib/faye/protocol/subscription.rb

Instance Method Summary collapse

Methods included from Deferrable

#set_deferred_status

Constructor Details

#initialize(client, channels, callback) ⇒ Subscription

Returns a new instance of Subscription.



5
6
7
8
9
10
# File 'lib/faye/protocol/subscription.rb', line 5

def initialize(client, channels, callback)
  @client    = client
  @channels  = channels
  @callback  = callback
  @cancelled = false
end

Instance Method Details

#call(*args) ⇒ Object



17
18
19
20
21
22
# File 'lib/faye/protocol/subscription.rb', line 17

def call(*args)
  message = args.first

  @callback.call(message['data']) if @callback
  @with_channel.call(message['channel'], message['data']) if @with_channel
end

#cancelObject



28
29
30
31
32
# File 'lib/faye/protocol/subscription.rb', line 28

def cancel
  return if @cancelled
  @client.unsubscribe(@channels, self)
  @cancelled = true
end

#to_procObject



24
25
26
# File 'lib/faye/protocol/subscription.rb', line 24

def to_proc
  @to_proc ||= lambda { |*a| call(*a) }
end

#unsubscribeObject



34
35
36
# File 'lib/faye/protocol/subscription.rb', line 34

def unsubscribe
  cancel
end

#with_channel(&callback) ⇒ Object



12
13
14
15
# File 'lib/faye/protocol/subscription.rb', line 12

def with_channel(&callback)
  @with_channel = callback
  self
end