class PusherFake::Channel::Public
A public channel.
Constants
- CACHE_CHANNEL_PREFIX
Attributes
@return [Array] Connections in this channel.
@return [Array] Arguments for the last event emitted.
@return [String] The channel name.
Public Class Methods
Create a new {Public} object.
@param [String] name The channel name.
# File lib/pusher-fake/channel/public.rb, line 18 def initialize(name) @name = name @last_event = nil @connections = [] end
Public Instance Methods
Add the connection to the channel.
@param [Connection] connection The connection to add. @param [Hash] options The options for the channel.
# File lib/pusher-fake/channel/public.rb, line 28 def add(connection, options = {}) subscription_succeeded(connection, options) emit_last_event(connection) end
Emit an event to the channel.
@param [String] event The event name. @param [Hash] data The event data.
# File lib/pusher-fake/channel/public.rb, line 37 def emit(event, data, options = {}) if cache_channel? @last_event = [event, data] end connections.each do |connection| unless connection.id == options[:socket_id] connection.emit(event, data, name) end end end
Determine if the connection
is in the channel.
@param [Connection] connection The connection. @return [Boolean] If the connection is in the channel or not.
# File lib/pusher-fake/channel/public.rb, line 53 def includes?(connection) connections.index(connection) end
Remove the connection
from the channel.
If it is the last connection, trigger the channel_vacated webhook.
@param [Connection] connection The connection to remove.
# File lib/pusher-fake/channel/public.rb, line 62 def remove(connection) connections.delete(connection) trigger("channel_vacated", channel: name) if connections.empty? end
Return subscription data for the channel.
@abstract @return [Hash] Subscription data for the channel.
# File lib/pusher-fake/channel/public.rb, line 72 def subscription_data {} end
# File lib/pusher-fake/channel/public.rb, line 76 def trigger(name, data = {}) PusherFake::Webhook.trigger(name, data) end
Private Instance Methods
Whether or not the channel is a cache channel.
@return [Boolean]
# File lib/pusher-fake/channel/public.rb, line 88 def cache_channel? @cache_channel ||= name.match?(CACHE_CHANNEL_PREFIX) end
Emit the last event if present and a cache channel.
@param [Connection] connection The connection to emit to.
# File lib/pusher-fake/channel/public.rb, line 95 def emit_last_event(connection) return unless cache_channel? if last_event connection.emit(*last_event, name) else connection.emit("pusher:cache_miss", nil, name) trigger("cache_miss", channel: name) end end
Notify the connection
of the successful subscription and add the connection to the channel.
If it is the first connection, trigger the channel_occupied webhook.
@param [Connection] connection Connection
a subscription succeeded for. @param [Hash] options The options for the channel.
# File lib/pusher-fake/channel/public.rb, line 113 def subscription_succeeded(connection, _options = {}) connection.emit("pusher_internal:subscription_succeeded", subscription_data, name) connections.push(connection) trigger("channel_occupied", channel: name) if connections.length == 1 end