Skip to content

Hyperstack.connect should behave nicely if passed a dummy value #159

Closed
@catmando

Description

@catmando

for example:

class CurrentUser < HyperStore
  receives LoginOperation do |user|
    Hyperstack.connect(User.find_by_email(user.email)) 
  end

will not work, because connect needs the actual id, which will be unknown (i.e. a dummy value) until the next fetch cycle completes.

So internally connect needs to make sure that id is loaded before connecting:

module Hyperstack
  def self.connect(*channels)
    channels.each do |channel|
      if channel.is_a? Class
        IncomingBroadcast.connect_to(channel.name)
      elsif channel.is_a?(String) || channel.is_a?(Array)
        IncomingBroadcast.connect_to(*channel)
      elsif channel.id
        Hyperstack::Model.load do
          channel.id
        end.then do |id|
          IncomingBroadcast.connect_to(channel.class.name, id)
        end
      else
        raise "cannot connect to model before it has been saved"
      end
    end
  end
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestready-to-releaseInternal Use Only: Has been fixed, specs passing and pushed to edge branch

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions