ruby-hyperloop / hyper-mesh

The project has moved to Hyperstack!! - Synchronization of active record models across multiple clients using Pusher, ActionCable, or Polling
https://hyperstack.org/
MIT License
22 stars 12 forks source link

Don't send an updates to clients if the filtered changes are empty #107

Open catmando opened 6 years ago

catmando commented 6 years ago

when using auth_logic (for example) when a logged user makes ANY request to the server, this updates the user model, which causes data to be pushed to the client

which causes the client to update its scopes

which is another request

and so on...

so the solution should be don't sync those attributes to the client

so hyper-mesh filters out those attributes, but the problem is even if you have an empty set of attributes, the update is still being sent, causing the scope resync...

catmando commented 6 years ago

this may fix it:


module ReactiveRecord
  class Broadcast
    def self.after_commit(operation, model)
      Hyperloop::InternalPolicy.regulate_broadcast(model) do |data|
        if !Hyperloop.on_server? && Hyperloop::Connection.root_path
          send_to_server(operation, data)
        elsif operation == :update && data[:previous_changes].empty?
          SendPacket.run(data, operation: operation)
        end
      end
    rescue ActiveRecord::StatementInvalid => e
      raise e unless e.message == "Could not find table 'hyperloop_connections'"
    end unless RUBY_ENGINE == 'opal'
  end
end
catmando commented 6 years ago

you can't monkey patch this but a slightly cleaner (if not quite as effecient) way is to change the SendPacket operation's dispatch to be:

dispatch_to { params.channel if params.operation == :update && params.previous_changes.present? }
catmando commented 6 years ago

actually first case (patching after_commit) won't work if the change was made on the console... So best update SendPacket