Module: LucidChannel::Mixin

Defined in:
lib/lucid_channel/mixin.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/lucid_channel/mixin.rb', line 3

def self.included(base)
  Isomorfeus.add_valid_channel_class(base) unless base == LucidChannel::Base

  base.instance_exec do
    def subscription_channels
      @subscription_channels ||= {}
    end

    def channel(name, options = {})
      subscription_channels[name.to_s] = options
    end

    def valid_channel?(name)
      name = name.to_s
      subscription_channels.key?(name) || name == self.name
    end

    def process_message(message, error, channel = nil)
      channel = self.name unless channel
      channel = channel.to_s
      unless valid_channel?(channel)
        Isomorfeus.raise_error(message: "No such channel '#{channel}' declared for #{self.name}! Cannot process message")
      end
      block = subscription_channels[channel][:block]
      Isomorfeus.raise_error(message: "#{self} received: #{channel} #{message}, but no 'on_message' block defined!") unless block
      block.call(message, error)
      nil
    end

    def on_message(channel = nil, &block)
      channel = self.name unless channel
      channel = channel.to_s
      unless valid_channel?(channel)
        Isomorfeus.raise_error(message: "No such channel #{channel} declared, please declare it first!")
      end
      subscription_channels[channel] = {} unless subscription_channels.key?(channel)
      subscription_channels[channel][:block] = block
    end

    def send_message(message, channel = nil)
      channel = self.name unless channel
      unless valid_channel?(channel)
        Isomorfeus.raise_error(message: "No such channel '#{channel}' declared for #{self.name}! Cannot send message")
      end
      Isomorfeus::Transport.send_message(self, channel, message)
    end

    def subscribe(channel = nil)
      promise_subscribe(channel)
      nil
    end

    def promise_subscribe(channel = nil)
      channel = channel ? channel : self.name
      Isomorfeus::Transport.promise_subscribe(self.name, channel)
    end

    def unsubscribe(channel = nil)
      promise_unsubscribe(channel)
      nil
    end

    def promise_unsubscribe(channel = nil)
      channel = channel ? channel : self.name
      Isomorfeus::Transport.promise_unsubscribe(self.name, channel)
    end

    if RUBY_ENGINE == 'opal'
      def server_subscription_channels; end
      def server_process_message(message, channel = nil); end
      def server_on_message(channel = nil, &block); end
    else
      def server_is_processing_messages?(channel)
        return false if server_subscription_channels.empty?
        return true if server_subscription_channels.key?(channel) && server_subscription_channels[channel].key?(:block)
        false
      end

      def server_subscription_channels
        @server_subscription_channels ||= {}
      end

      def server_process_message(message, channel = nil)
        channel = self.name unless channel
        channel = channel.to_s
        block = server_subscription_channels[channel][:block]
        block.call(message)
      end

      def server_on_message(channel = nil, &block)
        channel = self.name unless channel
        channel = channel.to_s
        unless valid_channel?(channel)
          Isomorfeus.raise_error(message: "No such channel #{channel} declared, please declare it first!")
        end
        server_subscription_channels[channel] = {} unless server_subscription_channels.key?(channel)
        server_subscription_channels[channel][:block] = block
      end
    end
  end
end