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

Use locate_many to avoid N+1 #80

Merged
merged 1 commit into from
Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/futurism.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "cable_ready"
require "futurism/engine"
require "futurism/message_verifier"
require "futurism/resolver/resources"
require "futurism/resolver/controller"
require "futurism/resolver/controller/renderer"
require "futurism/channel"
Expand Down
26 changes: 2 additions & 24 deletions lib/futurism/channel.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module Futurism
class Channel < ActionCable::Channel::Base
include CableReady::Broadcaster
include Futurism::MessageVerifier

def stream_name
ids = connection.identifiers.map { |identifier| send(identifier).try(:id) || send(identifier) }
Expand All @@ -18,19 +17,8 @@ def subscribed
def receive(data)
resources = data.fetch_values("signed_params", "sgids", "signed_controllers", "urls") { |_key| Array.new(data["signed_params"].length, nil) }.transpose

resources.each do |signed_params, sgid, signed_controller, url|
selector = "[data-signed-params='#{signed_params}']"
selector << "[data-sgid='#{sgid}']" if sgid.present?

controller = Resolver::Controller.from(signed_string: signed_controller)
renderer = Resolver::Controller::Renderer.for(controller: controller,
connection: connection,
url: url,
params: @params)

resource = lookup_resource(signed_params: signed_params, sgid: sgid)
html = renderer.render(resource)

resolver = Resolver::Resources.new(resource_definitions: resources, connection: connection, params: @params)
resolver.resolve do |selector, html|
cable_ready[stream_name].outer_html(
selector: selector,
html: html
Expand All @@ -39,15 +27,5 @@ def receive(data)

cable_ready.broadcast
end

private

def lookup_resource(signed_params:, sgid:)
return GlobalID::Locator.locate_signed(sgid) if sgid.present?

message_verifier
.verify(signed_params)
.deep_transform_values { |value| value.is_a?(String) && value.start_with?("gid://") ? GlobalID::Locator.locate(value) : value }
end
end
end
68 changes: 68 additions & 0 deletions lib/futurism/resolver/resources.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
module Futurism
module Resolver
class Resources
include Futurism::MessageVerifier

# resource definitions are an array of [signed_params, sgid, signed_controller, url]
def initialize(resource_definitions:, connection:, params:)
@connection = connection
@params = params
@resources_with_sgids, @resources_without_sgids = resource_definitions
.partition { |signed_params, sgid, *| sgid.present? }
.map { |partition| partition.map { |definition| ResourceDefinition.new(definition) } }
end

def resolve
resolved_models.zip(@resources_with_sgids).each do |model, resource_definition|
html = renderer_for(resource_definition: resource_definition).render(model)

yield(resource_definition.selector, html)
end

@resources_without_sgids.each do |resource_definition|
resource = lookup_resource(resource_definition)
html = renderer_for(resource_definition: resource_definition).render(resource)

yield(resource_definition.selector, html)
end
end

private

class ResourceDefinition
attr_reader :signed_params, :sgid, :signed_controller, :url

def initialize(resource_definition)
@signed_params, @sgid, @signed_controller, @url = resource_definition
end

def selector
selector = "[data-signed-params='#{@signed_params}']"
selector << "[data-sgid='#{@sgid}']" if @sgid.present?
selector
end

def controller
Resolver::Controller.from(signed_string: @signed_controller)
end
end

def renderer_for(resource_definition:)
Resolver::Controller::Renderer.for(controller: resource_definition.controller,
connection: @connection,
url: resource_definition.url,
params: @params)
end

def resolved_models
GlobalID::Locator.locate_many_signed @resources_with_sgids.map(&:sgid)
end

def lookup_resource(resource_definition)
message_verifier
.verify(resource_definition.signed_params)
.deep_transform_values { |value| value.is_a?(String) && value.start_with?("gid://") ? GlobalID::Locator.locate(value) : value }
end
end
end
end