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

.optsObject (readonly)

Returns the value of attribute opts.



276
277
278
# File 'lib/synchromesh/client_drivers.rb', line 276

def opts
  @opts
end

.public_columns_hashObject (readonly)

Returns the value of attribute public_columns_hash.



277
278
279
# File 'lib/synchromesh/client_drivers.rb', line 277

def public_columns_hash
  @public_columns_hash
end

Class Method Details

.define_attribute_methodsObject



294
295
296
297
298
# File 'lib/synchromesh/client_drivers.rb', line 294

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



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

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_mountObject

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



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
350
351
352
353
354
355
356
357
# File 'lib/synchromesh/client_drivers.rb', line 303

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



359
360
361
362
363
# File 'lib/synchromesh/client_drivers.rb', line 359

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



219
220
221
222
223
224
225
# File 'lib/synchromesh/client_drivers.rb', line 219

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.



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

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.



230
231
232
233
234
# File 'lib/synchromesh/client_drivers.rb', line 230

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