File: subscription.rb

package info (click to toggle)
ruby-faye 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 1,792 kB
  • sloc: javascript: 14,833; ruby: 5,068; makefile: 30
file content (39 lines) | stat: -rw-r--r-- 729 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
module Faye
  class Subscription
    include Deferrable

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

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

    def call(*args)
      message = args.first

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

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

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

    def unsubscribe
      cancel
    end

  end
end