Class: Hyperloop::ClientDrivers

Inherits:
Object
  • Object
show all
Includes:
React::IsomorphicHelpers
Defined in:
lib/hyper-operation/transport/client_drivers.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.optsObject (readonly)

Returns the value of attribute opts.



144
145
146
# File 'lib/hyper-operation/transport/client_drivers.rb', line 144

def opts
  @opts
end

Class Method Details

.get_queued_data(operation, channel = nil, opts = {}) ⇒ Object



147
148
149
150
151
152
153
# File 'lib/hyper-operation/transport/client_drivers.rb', line 147

def self.get_queued_data(operation, channel = nil, opts = {})
  HTTP.get(polling_path(operation, channel), opts).then do |response|
    response.json.each do |data|
      sync_dispatch(data[1])
    end
  end
end

.initialize_client_drivers_on_bootObject



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/hyper-operation/transport/client_drivers.rb', line 155

def self.initialize_client_drivers_on_boot

  if @initialized
    # 1) skip initialization if already initialized
    # 2) if running action_cable make sure connection is up after pinging the server_up
    #    action cable closes the connection if files change on the server
    HTTP.get("#{`window.HyperloopEnginePath`}/server_up") do
      `#{Hyperloop.action_cable_consumer}.connection.open()` if `#{Hyperloop.action_cable_consumer}.connection.disconnected`
    end if Hyperloop.action_cable_consumer
    return
  end

  @initialized = true

  if RUBY_ENGINE == 'opal'
    @opts = Hash.new(`window.HyperloopOpts`)
  end

  if on_opal_client?

    if opts[:transport] == :pusher

      opts[:dispatch] = lambda do |data|
        sync_dispatch JSON.parse(`JSON.stringify(#{data})`)
      end

      if opts[:client_logging] && `window.console && window.console.log`
        `Pusher.log = function(message) {window.console.log(message);}`
      end
      if opts[:pusher_fake_js]
        opts[:pusher_api] = `eval(#{opts[:pusher_fake_js]})`
      else
        h = nil
        pusher_api = nil
        %x{
          h = {
            encrypted: #{opts[:encrypted]},
            cluster: #{opts[:cluster]},
            authEndpoint: window.HyperloopEnginePath+'/hyperloop-pusher-auth',
            auth: {headers: {'X-CSRF-Token': #{opts[:form_authenticity_token]}}}
          };
          pusher_api = new Pusher(#{opts[:key]}, h)
        }
        opts[:pusher_api] = pusher_api
      end
      Hyperloop.connect(*opts[:auto_connect])
    elsif opts[:transport] == :action_cable
      opts[:action_cable_consumer] =
        `ActionCable.createConsumer.apply(ActionCable, #{[*opts[:action_cable_consumer_url]]})`
      Hyperloop.connect(*opts[:auto_connect])
    elsif opts[:transport] == :simple_poller
      opts[:auto_connect].each { |channel| IncomingBroadcast.add_connection(*channel) }
      every(opts[:seconds_between_poll]) do
        get_queued_data(:read, nil, headers: {'X-HYPERLOOP-SILENT-REQUEST' =>  true })
      end
    end
  end
end

.polling_path(to, id = nil) ⇒ Object



214
215
216
217
218
# File 'lib/hyper-operation/transport/client_drivers.rb', line 214

def self.polling_path(to, id = nil)
  s = "#{`window.HyperloopEnginePath`}/hyperloop-#{to}/#{opts[:id]}"
  s = "#{s}/#{id}" if id
  s
end

.sync_dispatch(data) ⇒ Object



89
90
91
92
93
# File 'lib/hyper-operation/transport/client_drivers.rb', line 89

def self.sync_dispatch(data)
  # TODO old synchromesh double checked at this point to make sure that this client
  # expected to recieve from the channel the data was sent on.  Was that really needed?
  data[:operation].constantize.dispatch_from_server(data[:params])
end