Class: HyperMesh::IncomingBroadcast

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ IncomingBroadcast

Returns a new instance of IncomingBroadcast.



130
131
132
133
134
135
# File 'lib/synchromesh/client_drivers.rb', line 130

def initialize(id)
  @id = id
  @received = Set.new
  @record = {}
  @previous_changes = {}
end

Instance Attribute Details

#recordObject (readonly)

private



120
121
122
# File 'lib/synchromesh/client_drivers.rb', line 120

def record
  @record
end

Class Method Details

.add_connection(channel_name, id = nil) ⇒ Object



29
30
31
32
33
# File 'lib/synchromesh/client_drivers.rb', line 29

def self.add_connection(channel_name, id = nil)
  channel_string = "#{channel_name}#{'-'+id.to_s if id}"
  open_channels << channel_string
  channel_string
end

.connect_to(channel_name, id = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/synchromesh/client_drivers.rb', line 35

def self.connect_to(channel_name, id = nil)
  channel_string = add_connection(channel_name, id)
  if ClientDrivers.opts[:transport] == :pusher
    channel = "#{ClientDrivers.opts[:channel]}-#{channel_string}"
    %x{
      var channel = #{ClientDrivers.opts[:pusher_api]}.subscribe(#{channel});
      channel.bind('create', #{ClientDrivers.opts[:create]});
      channel.bind('change', #{ClientDrivers.opts[:change]});
      channel.bind('destroy', #{ClientDrivers.opts[:destroy]});
      channel.bind('pusher:subscription_succeeded', #{lambda {ClientDrivers.get_queued_data("connect-to-transport", channel_string)}})
    }
  elsif ClientDrivers.opts[:transport] == :action_cable
    channel = "#{ClientDrivers.opts[:channel]}-#{channel_string}"
    HTTP.post(ClientDrivers.polling_path('action-cable-auth', channel)).then do |response|
      %x{
        #{HyperMesh.action_cable_consumer}.subscriptions.create(
          {
            channel: "HyperMesh::ActionCableChannel",
            client_id: #{ClientDrivers.opts[:id]},
            synchromesh_channel: #{channel_string},
            authorization: #{response.json[:authorization]},
            salt: #{response.json[:salt]}
          },
          {
            connected: function() {
              #{ClientDrivers.get_queued_data("connect-to-transport", channel_string)}
            },
            received: function(data) {
              var data = #{JSON.parse(`JSON.stringify(data)`)}
              #{ClientDrivers.send("sync_#{`data`['message']}", `data`['data'])}
              return true
            }
          }
        )
      }
    end
  else
    HTTP.get(ClientDrivers.polling_path(:subscribe, channel_string))
  end
end

.in_transitObject



126
127
128
# File 'lib/synchromesh/client_drivers.rb', line 126

def self.in_transit
  @in_transit ||= Hash.new { |h, k| h[k] = new(k) }
end

.open_channelsObject



122
123
124
# File 'lib/synchromesh/client_drivers.rb', line 122

def self.open_channels
  @open_channels ||= Set.new
end

.receive(data, operation, &block) ⇒ Object



76
77
78
# File 'lib/synchromesh/client_drivers.rb', line 76

def self.receive(data, operation, &block)
  in_transit[data[:broadcast_id]].receive(data, operation, &block)
end

Instance Method Details

#complete!Object



167
168
169
# File 'lib/synchromesh/client_drivers.rb', line 167

def complete!
  self.class.in_transit.delete @id
end

#destroyed?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/synchromesh/client_drivers.rb', line 106

def destroyed?
  @destroyed
end

#klassObject



110
111
112
# File 'lib/synchromesh/client_drivers.rb', line 110

def klass
  Object.const_get(@klass)
end

#local(operation, record, data) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/synchromesh/client_drivers.rb', line 137

def local(operation, record, data)
  @destroyed = operation == :destroy
  @is_new = operation == :create
  @klass = record.class.name
  @record = data
  record.backing_record.destroyed = false
  @record.merge!(id: record.id) if record.id
  record.backing_record.destroyed = @destroyed
  @backing_record = record.backing_record
  attributes = record.backing_record.attributes
  data.each do |k, v|
    next if klass.reflect_on_association(k) || attributes[k] == v
    @previous_changes[k] = [attributes[k], v]
  end
  self
end

#merge_current_values(br) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/synchromesh/client_drivers.rb', line 171

def merge_current_values(br)
  current_values = Hash[*@previous_changes.collect do |attr, values|
    value = attr == :id ? record[:id] : values.first
    begin
    if br.attributes.key?(attr) &&
       br.attributes[attr] != br.convert(attr, value) &&
       br.attributes[attr] != br.convert(attr, values.last)
      puts "warning #{attr} has changed locally - will force a reload.\n"\
           "local value: #{br.attributes[attr]} remote value: #{br.convert(attr, value)}->#{br.convert(attr, values.last)}"
      return nil
    end
    rescue Exception => e
      debugger
      nil
    end
    [attr, value]
  end.compact.flatten].merge(br.attributes)
  klass._react_param_conversion(current_values)
rescue Exception => e
  debugger
  nil
end

#new?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/synchromesh/client_drivers.rb', line 102

def new?
  @is_new
end

#receive(data, operation) {|complete! if | ... } ⇒ Object

Yields:

  • (complete! if )


154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/synchromesh/client_drivers.rb', line 154

def receive(data, operation)
  @destroyed = operation == :destroy
  @is_new = operation == :create
  @channels ||= self.class.open_channels.intersection data[:channels]
  raise 'synchromesh security violation' unless @channels.include? data[:channel]
  @received << data[:channel]
  @klass ||= data[:klass]
  @record.merge! data[:record]
  @previous_changes.merge! data[:previous_changes]
  @backing_record = ReactiveRecord::Base.exists?(klass, record[:id])
  yield complete! if @channels == @received
end

#record_with_current_valuesObject



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/synchromesh/client_drivers.rb', line 80

def record_with_current_values
  ReactiveRecord::Base.load_data do

    backing_record = @backing_record || klass.find(record[:id]).backing_record
    if destroyed?
      backing_record.ar_instance
    else
      merge_current_values(backing_record)
    end
  end
end

#record_with_new_valuesObject



92
93
94
95
96
97
98
99
100
# File 'lib/synchromesh/client_drivers.rb', line 92

def record_with_new_values
  klass._react_param_conversion(record).tap do |ar_instance|
    if destroyed?
      ar_instance.backing_record.destroy_associations
    elsif new?
      ar_instance.backing_record.initialize_collections
    end
  end
end

#to_sObject



114
115
116
# File 'lib/synchromesh/client_drivers.rb', line 114

def to_s
  "klass: #{klass} record: #{record} new?: #{new?} destroyed?: #{destroyed?}"
end