Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
catmando opened this issue Apr 9, 2019 · 1 comment
Closed

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

catmando opened this issue Apr 9, 2019 · 1 comment
Labels
enhancement New feature or request ready-to-release Internal Use Only: Has been fixed, specs passing and pushed to edge branch
Milestone

Comments

@catmando
Copy link
Contributor

catmando commented Apr 9, 2019

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
@catmando catmando added enhancement New feature or request good first issue Good for newcomers labels Apr 9, 2019
@catmando
Copy link
Contributor Author

catmando commented Apr 9, 2019

fyi above code needs a bit better error reporting while its being changed:

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.respond_to?(:id)
        if channel.id
          Hyperstack::Model.load do
            channel.id
          end.then do |id|
            IncomingBroadcast.connect_to(channel.class.name, id)
          end
        else
          raise "Hyperstack.connect cannot connect to #{channel.inspect}.  The id is nil.  Possibly an unsaved model?"
        end
      else
        raise "Hyperstack.connect cannot connect to #{channel.inspect}.\n"\
              "Channels must be either a class, or a class name,\n"\
              "a string in the form 'ClassName-id',\n"\
              "an array in the form [class, id] or [class-name, id],\n"\
              "or an object that responds to the id method with a non-nil value"
      end
    end
  end
end

@catmando catmando added this to the alpha1.4 milestone Apr 15, 2019
@catmando catmando added ready-to-release Internal Use Only: Has been fixed, specs passing and pushed to edge branch and removed good first issue Good for newcomers labels Apr 15, 2019
@catmando catmando modified the milestones: alpha1.4, alpha1.5 Apr 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request ready-to-release Internal Use Only: Has been fixed, specs passing and pushed to edge branch
Projects
None yet
Development

No branches or pull requests

1 participant