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 single @output_buffer when rendering #974

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ if RUBY_VERSION >= "3.1"
gem "net-pop", require: false
gem "net-smtp", require: false
end

gem "ruby-prof-flamegraph", github: "oozou/ruby-prof-flamegraph", branch: "master"
133 changes: 72 additions & 61 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,74 +1,82 @@
GIT
remote: https://github.com/oozou/ruby-prof-flamegraph.git
revision: fc3c4370627597557db31e93b6008b123d75ab14
branch: master
specs:
ruby-prof-flamegraph (0.3.0)
ruby-prof (>= 1.4.2)

PATH
remote: .
specs:
view_component (2.48.0)
view_component (2.49.0)
activesupport (>= 5.0.0, < 8.0)
method_source (~> 1.0)

GEM
remote: https://rubygems.org/
specs:
actioncable (7.0.1)
actionpack (= 7.0.1)
activesupport (= 7.0.1)
actioncable (7.0.2)
actionpack (= 7.0.2)
activesupport (= 7.0.2)
nio4r (~> 2.0)
websocket-driver (>= 0.6.1)
actionmailbox (7.0.1)
actionpack (= 7.0.1)
activejob (= 7.0.1)
activerecord (= 7.0.1)
activestorage (= 7.0.1)
activesupport (= 7.0.1)
actionmailbox (7.0.2)
actionpack (= 7.0.2)
activejob (= 7.0.2)
activerecord (= 7.0.2)
activestorage (= 7.0.2)
activesupport (= 7.0.2)
mail (>= 2.7.1)
net-imap
net-pop
net-smtp
actionmailer (7.0.1)
actionpack (= 7.0.1)
actionview (= 7.0.1)
activejob (= 7.0.1)
activesupport (= 7.0.1)
actionmailer (7.0.2)
actionpack (= 7.0.2)
actionview (= 7.0.2)
activejob (= 7.0.2)
activesupport (= 7.0.2)
mail (~> 2.5, >= 2.5.4)
net-imap
net-pop
net-smtp
rails-dom-testing (~> 2.0)
actionpack (7.0.1)
actionview (= 7.0.1)
activesupport (= 7.0.1)
actionpack (7.0.2)
actionview (= 7.0.2)
activesupport (= 7.0.2)
rack (~> 2.0, >= 2.2.0)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.2.0)
actiontext (7.0.1)
actionpack (= 7.0.1)
activerecord (= 7.0.1)
activestorage (= 7.0.1)
activesupport (= 7.0.1)
actiontext (7.0.2)
actionpack (= 7.0.2)
activerecord (= 7.0.2)
activestorage (= 7.0.2)
activesupport (= 7.0.2)
globalid (>= 0.6.0)
nokogiri (>= 1.8.5)
actionview (7.0.1)
activesupport (= 7.0.1)
actionview (7.0.2)
activesupport (= 7.0.2)
builder (~> 3.1)
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.1, >= 1.2.0)
activejob (7.0.1)
activesupport (= 7.0.1)
activejob (7.0.2)
activesupport (= 7.0.2)
globalid (>= 0.3.6)
activemodel (7.0.1)
activesupport (= 7.0.1)
activerecord (7.0.1)
activemodel (= 7.0.1)
activesupport (= 7.0.1)
activestorage (7.0.1)
actionpack (= 7.0.1)
activejob (= 7.0.1)
activerecord (= 7.0.1)
activesupport (= 7.0.1)
activemodel (7.0.2)
activesupport (= 7.0.2)
activerecord (7.0.2)
activemodel (= 7.0.2)
activesupport (= 7.0.2)
activestorage (7.0.2)
actionpack (= 7.0.2)
activejob (= 7.0.2)
activerecord (= 7.0.2)
activesupport (= 7.0.2)
marcel (~> 1.0)
mini_mime (>= 1.1.0)
activesupport (7.0.1)
activesupport (7.0.2)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
Expand Down Expand Up @@ -120,13 +128,13 @@ GEM
temple (>= 0.8.0)
tilt
html_tokenizer (0.0.7)
i18n (1.8.11)
i18n (1.10.0)
concurrent-ruby (~> 1.0)
io-wait (0.2.1)
jbuilder (2.11.5)
actionview (>= 5.0.0)
activesupport (>= 5.0.0)
loofah (2.13.0)
loofah (2.14.0)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
mail (2.7.1)
Expand Down Expand Up @@ -167,35 +175,35 @@ GEM
rack (2.2.3)
rack-test (1.1.0)
rack (>= 1.0, < 3)
rails (7.0.1)
actioncable (= 7.0.1)
actionmailbox (= 7.0.1)
actionmailer (= 7.0.1)
actionpack (= 7.0.1)
actiontext (= 7.0.1)
actionview (= 7.0.1)
activejob (= 7.0.1)
activemodel (= 7.0.1)
activerecord (= 7.0.1)
activestorage (= 7.0.1)
activesupport (= 7.0.1)
rails (7.0.2)
actioncable (= 7.0.2)
actionmailbox (= 7.0.2)
actionmailer (= 7.0.2)
actionpack (= 7.0.2)
actiontext (= 7.0.2)
actionview (= 7.0.2)
activejob (= 7.0.2)
activemodel (= 7.0.2)
activerecord (= 7.0.2)
activestorage (= 7.0.2)
activesupport (= 7.0.2)
bundler (>= 1.15.0)
railties (= 7.0.1)
railties (= 7.0.2)
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
rails-html-sanitizer (1.4.2)
loofah (~> 2.3)
railties (7.0.1)
actionpack (= 7.0.1)
activesupport (= 7.0.1)
railties (7.0.2)
actionpack (= 7.0.2)
activesupport (= 7.0.2)
method_source
rake (>= 12.2)
thor (~> 1.0)
zeitwerk (~> 2.5)
rainbow (3.1.1)
rake (13.0.6)
regexp_parser (2.2.0)
regexp_parser (2.2.1)
rexml (3.2.5)
rubocop (1.13.0)
parallel (~> 1.10)
Expand All @@ -206,7 +214,7 @@ GEM
rubocop-ast (>= 1.2.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.15.1)
rubocop-ast (1.15.2)
parser (>= 3.0.1.1)
rubocop-github (0.16.2)
rubocop (<= 1.13.0)
Expand All @@ -219,6 +227,7 @@ GEM
activesupport (>= 4.2.0)
rack (>= 1.1)
rubocop (>= 0.87.0)
ruby-prof (1.4.3)
ruby-progressbar (1.11.0)
simplecov (0.18.5)
docile (~> 1.1)
Expand Down Expand Up @@ -259,7 +268,7 @@ GEM
webrick (~> 1.7.0)
yard-activesupport-concern (0.0.1)
yard (>= 0.8)
zeitwerk (2.5.3)
zeitwerk (2.5.4)

PLATFORMS
ruby
Expand All @@ -278,9 +287,11 @@ DEPENDENCIES
net-pop
net-smtp
pry (~> 0.13)
rails (~> 7.0.0)
rails (= 7.0.2)
rake (~> 13.0)
rubocop-github (~> 0.16.1)
ruby-prof (~> 1.4)
ruby-prof-flamegraph!
simplecov (~> 0.18.0)
simplecov-console (~> 0.7.2)
slim (~> 4.0)
Expand All @@ -290,4 +301,4 @@ DEPENDENCIES
yard-activesupport-concern

BUNDLED WITH
2.2.33
2.3.7
1 change: 1 addition & 0 deletions lib/view_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module ViewComponent
autoload :CompileCache
autoload :ComponentError
autoload :Deprecation
autoload :GlobalBuffer
autoload :Instrumentation
autoload :Preview
autoload :PreviewTemplateError
Expand Down
29 changes: 27 additions & 2 deletions lib/view_component/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,22 @@ def render_in(view_context, &block)
self.class.compile(raise_errors: true)

@view_context = view_context
self.__vc_original_view_context ||= view_context

# TODO flag with initializer
if !self.__vc_original_view_context
view_context.output_buffer = ActionView::OutputBuffer.new if !view_context.output_buffer

if view_context.global_buffer_coordinator.nil?
coordinator = view_context.global_buffer_coordinator = GlobalBuffer::Coordinator.new
coordinator.subscribe(view_context)
end

self.__vc_original_view_context = view_context
end

@output_buffer = self.__vc_original_view_context.output_buffer
self.global_buffer_coordinator = self.__vc_original_view_context.global_buffer_coordinator
self.global_buffer_coordinator.subscribe(self)

@lookup_context ||= view_context.lookup_context

Expand Down Expand Up @@ -104,14 +119,23 @@ def render_in(view_context, &block)
before_render

if render?
render_template_for(@__vc_variant).to_s + _output_postamble
fast_capture do
render_template_for(@__vc_variant).to_s + _output_postamble
end
else
""
end
ensure
global_buffer_coordinator&.unsubscribe(self)
@current_template = old_current_template
end

def fast_capture
value = nil
with_output_buffer(ActionView::OutputBuffer.new) { value = yield }
value
end

# EXPERIMENTAL: Optional content to be returned after the rendered template.
#
# @return [String]
Expand Down Expand Up @@ -155,6 +179,7 @@ def initialize(*); end
def render(options = {}, args = {}, &block)
if options.is_a? ViewComponent::Base
options.__vc_original_view_context = __vc_original_view_context
# maybe capture, but only if block given?
super
else
__vc_original_view_context.render(options, args, &block)
Expand Down
3 changes: 1 addition & 2 deletions lib/view_component/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,8 @@ def compile(raise_errors: false)
component_class.send(:undef_method, method_name.to_sym)
end

component_class.class_eval <<-RUBY, template[:path], -1
component_class.class_eval <<-RUBY, template[:path], 0
def #{method_name}
@output_buffer = ActionView::OutputBuffer.new
#{compiled_template(template[:path])}
end
RUBY
Expand Down
5 changes: 5 additions & 0 deletions lib/view_component/engine.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require "rails"
require 'view_component/global_buffer'

module ViewComponent
class Engine < Rails::Engine # :nodoc:
Expand Down Expand Up @@ -93,6 +94,10 @@ class Engine < Rails::Engine # :nodoc:
end
end

initializer "view_component.global_buffer_patch" do |app|
ActionView::Base.include(ViewComponent::GlobalBuffer::Patch)
end

initializer "view_component.include_render_component" do |app|
next if Rails.version.to_f >= 6.1

Expand Down