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.



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

def opts
  @opts
end

Class Method Details

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



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

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



154
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
# File 'lib/hyper-operation/transport/client_drivers.rb', line 154

def self.initialize_client_drivers_on_boot

  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]},
            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



200
201
202
203
204
# File 'lib/hyper-operation/transport/client_drivers.rb', line 200

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