Browse Source

proxy_stream

main
pvincent 4 days ago
parent
commit
d1eb945ec3
  1. 32
      app/controllers/application_controller.rb
  2. 11
      app/controllers/notification_controller.rb
  3. 2
      app/views/notification/index.html.erb

32
app/controllers/application_controller.rb

@ -5,17 +5,27 @@ class ApplicationController < ActionController::Base
def render_stream(notice: nil, alert: nil, &)
flash.now[:notice] = notice if notice
flash.now[:alert] = alert if alert
streams = []
streams << turbo_stream.replace(:flash, partial: 'layouts/components/flash') if flash.any?
# TODO: turbo_stream_elements which stores any append, prepend, replace, update, remove, before, after or refresh actions
if block_given?
yields = yield(turbo_stream)
if yields.is_a? Array
streams += yields
else
streams << yields
end
proxy_stream = ProxyStream.new(turbo_stream)
proxy_stream.replace(:flash, partial: 'layouts/components/flash') if flash.any?
yield(proxy_stream) if block_given?
render turbo_stream: proxy_stream.elements
end
class ProxyStream
attr_reader :elements
def initialize(turbo_stream)
@turbo_stream = turbo_stream
@elements = []
end
def append(*streamables, **attributes)
@elements << @turbo_stream.append(*streamables, **attributes)
end
def replace(*streamables, **attributes)
@elements << @turbo_stream.replace(*streamables, **attributes)
end
render turbo_stream: streams
end
end

11
app/controllers/notification_controller.rb

@ -24,18 +24,19 @@ class NotificationController < ApplicationController
end
def turbo_more
flash.now[:alert] = "alert, do the stuff right now ##{inc_count}"
flash.now[:notice] = ["this is extra notice ##{inc_count}", 'one more time']
flash.alert = "alert, do the stuff right now ##{inc_count}"
flash.notice = ["this is extra notice ##{inc_count}", 'one more time']
render_stream do |stream|
# TODO: turbo_stream_elements which stores any append, prepend, replace, update, remove, before, after or refresh actions
stream.replace(:turbo_placeholder, partial: 'turbo_placeholder')
stream.replace(:turbo_placeholder2, partial: 'turbo_placeholder2')
end
end
def turbo_none
render_stream
render_stream do |stream|
stream.replace(:turbo_placeholder, partial: 'turbo_placeholder')
stream.replace(:turbo_placeholder2, partial: 'turbo_placeholder2')
end
end
private

2
app/views/notification/index.html.erb

@ -53,7 +53,7 @@
<% end %>
</li>
<li class="p-4 pb-2 text-xs opacity-60 tracking-wide">
<%= link_to notification_turbo_none_path,
<%= button_to notification_turbo_none_path,
class: "btn btn-accent btn-outline",
data: { turbo_stream: true, turbo_prefetch: false } do %>
Turbo None

Loading…
Cancel
Save