Class: HyperMesh::ClientDrivers

Inherits:
Object
  • Object
show all
Includes:
React::IsomorphicHelpers
Defined in:
lib/synchromesh/client_drivers.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.opts ⇒ Object (readonly)

Returns the value of attribute opts.



268
269
270
# File 'lib/synchromesh/client_drivers.rb', line 268

def opts
  @opts
end

.public_columns_hash ⇒ Object (readonly)

Returns the value of attribute public_columns_hash.



269
270
271
# File 'lib/synchromesh/client_drivers.rb', line 269

def public_columns_hash
  @public_columns_hash
end

Class Method Details

.define_attribute_methods ⇒ Object



286
287
288
289
290
# File 'lib/synchromesh/client_drivers.rb', line 286

def self.define_attribute_methods
  public_columns_hash.keys.each do |model|
    Object.const_get(model).define_attribute_methods rescue nil
  end
end

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



278
279
280
281
282
283
284
# File 'lib/synchromesh/client_drivers.rb', line 278

def self.get_queued_data(operation, channel = nil, opts = {})
  HTTP.get(polling_path(operation, channel), opts).then do |response|
    response.json.each do |update|
      send "sync_#{update[0]}", update[1]
    end
  end
end

.on_first_mount ⇒ Object

called from ReactiveRecord::Base before_first_mount hook to insure this is done first.



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/synchromesh/client_drivers.rb', line 295

def self.on_first_mount

  if RUBY_ENGINE == 'opal'
    @opts = Hash.new(`window.HyperMeshOpts`)
    @public_columns_hash = get_public_columns_hash
    define_attribute_methods
  end

  if on_opal_client?

    if opts[:transport] == :pusher

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

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

      opts[:destroy] = lambda do |data|
        sync_destroy 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.ReactiveRecordEnginePath+'/synchromesh-pusher-auth',
            auth: {headers: {'X-CSRF-Token': #{opts[:form_authenticity_token]}}}
          };
          pusher_api = new Pusher(#{opts[:key]}, h)
        }
        opts[:pusher_api] = pusher_api
      end
      HyperMesh.connect(*opts[:auto_connect])
    elsif opts[:transport] == :action_cable
      opts[:action_cable_consumer] =
        `ActionCable.createConsumer.apply(ActionCable, #{[*opts[:action_cable_consumer_url]]})`
      HyperMesh.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-SYNCHROMESH-SILENT-REQUEST' =>  true })
      end
    end
  end
end

.polling_path(to, id = nil) ⇒ Object



351
352
353
354
355
# File 'lib/synchromesh/client_drivers.rb', line 351

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

.sync_change(data) ⇒ Object



211
212
213
214
215
216
217
# File 'lib/synchromesh/client_drivers.rb', line 211

def self.sync_change(data)
  IncomingBroadcast.receive(data, :change) do |broadcast|
    ReactiveRecord::Base.when_not_saving(broadcast.klass) do
      ReactiveRecord::Collection.sync_scopes broadcast
    end
  end
end

.sync_create(data) ⇒ Object

sync_changes: Wait till we are done with any concurrent model saves, then hydrate the data (which will update any attributes) and sync the scopes.



203
204
205
206
207
208
209
# File 'lib/synchromesh/client_drivers.rb', line 203

def self.sync_create(data)
  IncomingBroadcast.receive(data, :create) do |broadcast|
    ReactiveRecord::Base.when_not_saving(broadcast.klass) do
      ReactiveRecord::Collection.sync_scopes broadcast
    end
  end
end

.sync_destroy(data) ⇒ Object

sync_destroy: Hydrate the data, then destroy the record, cleanup and syncronize the scopes.



222
223
224
225
226
# File 'lib/synchromesh/client_drivers.rb', line 222

def self.sync_destroy(data)
  IncomingBroadcast.receive(data, :destroy) do |broadcast|
    ReactiveRecord::Collection.sync_scopes broadcast
  end
end