Module: Phlex::Reactive::Streamable
- Extended by:
- ActiveSupport::Concern
- Included in:
- Component
- Defined in:
- lib/phlex/reactive/streamable.rb
Overview
Streamable gives a self-contained Phlex component the ability to render
ITSELF as a Turbo Stream and to broadcast itself over a stream. Every
streamable component must implement #id returning a stable DOM id —
that id is the Turbo Stream target, so you never hand-pick targets.
Class methods (use in controllers):
render turbo_stream: Counter.replace(counter)
render turbo_stream: [Row.append(target: "items", model: @item),
Totals.update(@order)]
Broadcast methods (use in models/jobs/actions):
Counter.broadcast_replace_to(counter, model: counter)
Row.broadcast_append_to(@list, target: "items", model: @item)
Convention: the id you set on the root element in view_template must
equal what #id returns, so replace/broadcast_replace target it.
NOTE: we intentionally do NOT include Turbo::Streams::ActionHelper — it
pulls in ActionView::Helpers::TagHelper, which overrides Phlex's internal
tag method and breaks rendering. We use Turbo::Streams::TagBuilder
directly instead.
Defined Under Namespace
Classes: ThreadViewContext
Constant Summary collapse
- BROADCAST_REFUSED_OPS =
Focus ops are actor-only (they steal focus): broadcast_js_to rejects a broadcast that carries one. Names mirror Phlex::Reactive::JS's focus verbs.
%w[focus focus_first].freeze
Class Method Summary collapse
- .register(klass) ⇒ Object
-
.registered_classes ⇒ Object
A point-in-time snapshot of every registered streamable class, taken under the registry lock (the doctor iterates it — issue #106).
-
.reset_all_view_contexts! ⇒ Object
Reset every streamable class's cached view context + builder.
Instance Method Summary collapse
-
#dom_id(record, prefix = nil) ⇒ Object
Render-context-free dom_id, safe to use inside
#id. -
#id ⇒ Object
The stable DOM id used as the Turbo Stream target.
-
#to_stream_morph ⇒ Object
Render THIS instance as a MORPHING replace (issue #28):
<turbo-stream action="replace" method="morph">. -
#to_stream_remove ⇒ Object
Render THIS instance as a remove stream.
-
#to_stream_replace ⇒ Object
Render THIS already-built instance as a replace stream (used by the reactive action endpoint after an action mutated state).
-
#to_stream_token ⇒ Object
Render a TOKEN-ONLY refresh stream (issue #30): a tiny
<turbo-stream action="reactive:token">carrying the component's fresh signed token, with NO rendered body. -
#to_stream_update(morph: false) ⇒ Object
morph: trueemits<turbo-stream action="update" method="morph">(issue #113) so Turbo 8 morphs the inner HTML in place, preserving a focused + caret across a per-field update.
Class Method Details
.register(klass) ⇒ Object
59 60 61 |
# File 'lib/phlex/reactive/streamable.rb', line 59 def register(klass) @registry_mutex.synchronize { @registry[klass] = true } end |
.registered_classes ⇒ Object
A point-in-time snapshot of every registered streamable class, taken under the registry lock (the doctor iterates it — issue #106). Returns a plain Array so callers never hold the live WeakMap or the lock while working; a class GC'd later simply won't appear next time. Call Rails.application.eager_load! FIRST if you need every app class loaded — the WeakMap only holds classes that have actually been loaded.
69 70 71 72 73 74 75 |
# File 'lib/phlex/reactive/streamable.rb', line 69 def registered_classes @registry_mutex.synchronize do classes = [] @registry.each_key { classes << it } classes end end |
.reset_all_view_contexts! ⇒ Object
Reset every streamable class's cached view context + builder. Called from the engine's reloader (config.to_prepare) so a reloaded renderer controller is never served from a stale memo. No-op outside Rails.
53 54 55 56 57 |
# File 'lib/phlex/reactive/streamable.rb', line 53 def reset_all_view_contexts! @registry_mutex.synchronize do @registry.each_key(&:reset_turbo_view_context!) end end |
Instance Method Details
#dom_id(record, prefix = nil) ⇒ Object
Render-context-free dom_id, safe to use inside #id. The streamable
machinery calls #id BEFORE rendering, so Phlex's render-time dom_id
helper would raise HelpersCalledBeforeRenderError. This delegates to
ActionView::RecordIdentifier, which works anywhere — so
def id = dom_id(@todo) is safe.
532 533 534 |
# File 'lib/phlex/reactive/streamable.rb', line 532 def dom_id(record, prefix = nil) ::ActionView::RecordIdentifier.dom_id(record, prefix) end |
#id ⇒ Object
The stable DOM id used as the Turbo Stream target. It MUST match the id
set on the component's root element in view_template.
Record-backed components (Component's reactive_record :x) get a
default for free: dom_id(record) — the id virtually every such
component wrote by hand (issue #81). An explicit def id always wins
via normal method lookup. Everything else (state-backed, a bare
Streamable) still raises: a class-name default would silently collide
the moment two instances render on one page. Two DIFFERENT component
classes rendering the SAME record on one page also collide on the
default — give one a prefixed id: def id = dom_id(@todo, "rich").
Called on every render/stream build, so the default stays lean: a respond_to? (reactive_record_key is Component API — a bare Streamable keeps raising) plus ivar reads via the memoized reactive_record_ivar.
514 515 516 517 518 519 520 521 522 523 524 525 |
# File 'lib/phlex/reactive/streamable.rb', line 514 def id klass = self.class if klass.respond_to?(:reactive_record_key) && (record_ivar = klass.reactive_record_ivar) && (record = instance_variable_get(record_ivar)) return dom_id(record) end raise NotImplementedError, "#{klass} must implement #id (the stable DOM id Turbo Streams target) — add e.g. " \ "`def id = \"my-thing\"`. Record-backed components (reactive_record :x) get " \ "`dom_id(record)` as the default automatically." end |
#to_stream_morph ⇒ Object
Render THIS instance as a MORPHING replace (issue #28):
<turbo-stream action="replace" method="morph">. Turbo 8's bundled
Idiomorph morphs the subtree in place — preserving the focused +
caret across the re-render — while still carrying the root's fresh
data-reactive-token-value (so the signed token refreshes). Used by
Response.morph / Response.replace(self, morph: true).
554 555 556 557 558 559 |
# File 'lib/phlex/reactive/streamable.rb', line 554 def to_stream_morph Phlex::Reactive::Stream.wrap( self.class.turbo_stream_builder.replace(id, html: self.class.render_component(self), method: :morph), action: "replace", target: id, renders_root: true ) end |
#to_stream_remove ⇒ Object
Render THIS instance as a remove stream. The component already knows its own #id, so no record/class reconstruction is needed (works for record- and state-backed components alike). Used by Response.remove.
608 609 610 611 612 613 |
# File 'lib/phlex/reactive/streamable.rb', line 608 def to_stream_remove Phlex::Reactive::Stream.wrap( self.class.turbo_stream_builder.remove(id), action: "remove", target: id, renders_root: false ) end |
#to_stream_replace ⇒ Object
Render THIS already-built instance as a replace stream (used by the reactive action endpoint after an action mutated state). Wrapped in a Phlex::Reactive::Stream (issue #114) so the endpoint reads the action / target / token-ness structurally instead of regexing the markup; the wire bytes are byte-identical.
541 542 543 544 545 546 |
# File 'lib/phlex/reactive/streamable.rb', line 541 def to_stream_replace Phlex::Reactive::Stream.wrap( self.class.turbo_stream_builder.replace(id, html: self.class.render_component(self)), action: "replace", target: id, renders_root: true ) end |
#to_stream_token ⇒ Object
Render a TOKEN-ONLY refresh stream (issue #30): a tiny
<turbo-stream action="reactive:token"> carrying the component's fresh
signed token, with NO rendered body. It lets an action update only PART
of a component (its own hand-built streams) while still rolling the
signed identity token forward — the client reads the next token from this
attribute (#extractToken) and an inert client action writes it onto the
root (a pure attribute set, so a focused + caret survive). Unlike
to_stream_replace, it does NOT re-render the children, so a live input
the user is typing into is never torn down. Used by Response.streams.
The component carries its token via Component#reactive_token; a Streamable that isn't a Component (no token) simply has nothing to refresh — guarded by respond_to? so the primitive stays usable on a bare Streamable.
respond_to? MUST include private methods (the true arg): Component
defines reactive_token as PRIVATE, so a plain respond_to?(:reactive_token)
is false for every Component and the stream silently carries an EMPTY token —
which makes any non-self-rendering reply (reply.streams #30, reply.append /
reply.remove #35) add-once-only: the first action works, then the stale (here
empty) token is rejected on the next dispatch (cosmos#1939). A bare Streamable
has no reactive_token method at all, so it still returns false correctly.
595 596 597 598 599 600 601 602 603 |
# File 'lib/phlex/reactive/streamable.rb', line 595 def to_stream_token token = respond_to?(:reactive_token, true) ? reactive_token : nil html = %(<turbo-stream action="reactive:token" target="#{ERB::Util.html_escape(id)}" data-reactive-token-value="#{ERB::Util.html_escape(token)}"></turbo-stream>) # Both interpolations are ERB::Util.html_escape'd, so .html_safe is a # byte-identical relabel. renders_root: true — reactive:token IS a # self-render for token purposes (it's in SELF_RENDER_ACTIONS) — unifies # the actor's own token stream onto the structural path (issue #114). Phlex::Reactive::Stream.wrap(html.html_safe, action: "reactive:token", target: id, renders_root: true) end |
#to_stream_update(morph: false) ⇒ Object
morph: true emits <turbo-stream action="update" method="morph">
(issue #113) so Turbo 8 morphs the inner HTML in place, preserving a
focused + caret across a per-field update. Default (morph:
false) is the unchanged plain update. Passing method: :morph inline
(as #to_stream_morph does) keeps the plain call's wire byte-identical.
Used by Response.update.
567 568 569 570 571 572 |
# File 'lib/phlex/reactive/streamable.rb', line 567 def to_stream_update(morph: false) builder = self.class.turbo_stream_builder html = self.class.render_component(self) rendered = morph ? builder.update(id, html:, method: :morph) : builder.update(id, html:) Phlex::Reactive::Stream.wrap(rendered, action: "update", target: id, renders_root: true) end |