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


171
172
173
# File 'lib/hyper-operation/transport/client_drivers.rb', line 171

def opts
  @opts
end

Class Method Details

.complete_connection(channel, retries = 10) ⇒ Object


185
186
187
188
189
# File 'lib/hyper-operation/transport/client_drivers.rb', line 185

def self.complete_connection(channel, retries = 10)
  get_queued_data('connect-to-transport', channel).fail do
    after(0.25) { complete_connection(channel, retries - 1) } unless retries.zero?
  end
end

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


191
192
193
194
195
196
197
198
# File 'lib/hyper-operation/transport/client_drivers.rb', line 191

def self.get_queued_data(operation, channel = nil, opts = {})
  Hyperloop::HTTP.get(polling_path(operation, channel), opts).then do |response|
    response.json.each do |data|
      `console.log("simple_poller received: ", data)` if ClientDrivers.env == 'development'
      sync_dispatch(data[1])
    end
  end
end

.initialize_client_drivers_on_bootObject


200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/hyper-operation/transport/client_drivers.rb', line 200

def self.initialize_client_drivers_on_boot

  if @initialized
    # 1) skip initialization if already initialized
    if on_opal_client? && Hyperloop.action_cable_consumer
      # 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
      Hyperloop::HTTP.get("#{`window.HyperloopEnginePath`}/server_up") do
        `#{Hyperloop.action_cable_consumer}.connection.open()` if `#{Hyperloop.action_cable_consumer}.connection.disconnected`
      end
    end
    return
  end

  @initialized = true
  @opts = {}

  if on_opal_client?

    @opts = Hash.new(`window.HyperloopOpts`)


    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)
      end
    end
  end
end

.polling_path(to, id = nil) ⇒ Object


261
262
263
264
265
# File 'lib/hyper-operation/transport/client_drivers.rb', line 261

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


113
114
115
116
117
# File 'lib/hyper-operation/transport/client_drivers.rb', line 113

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