Module: SolidObjects::TurboStreamRenderer
- Defined in:
- lib/solid_objects/turbo_stream_renderer.rb,
sig/generated/lib/solid_objects/turbo_stream_renderer.rbs
Constant Summary collapse
- INVALIDATION_MARKER =
"solid_objects_invalidation:"- INVALIDATION_PATTERN =
/<!--solid_objects_invalidation:([A-Za-z0-9_=-]+)-->/
Class Method Summary collapse
- .batch_refresh(registrations, instance_id, revision) ⇒ Object
- .component_refresh(registration, instance_id, revision) ⇒ Object
- .invalidation(stream) ⇒ Object
- .observable(broadcast) ⇒ Object
- .observable_value(reference, name, value) ⇒ Object
- .state_payload(payload) ⇒ Object
Instance Method Summary collapse
- #self?.batch_refresh ⇒ String
- #self?.component_refresh ⇒ String
- #self?.display_value ⇒ String
- #self?.invalidation ⇒ Hash[String, untyped]?
- #self?.morph_component_refresh ⇒ String
- #self?.observable ⇒ String
- #self?.observable_value ⇒ String
- #self?.state_payload ⇒ String
Class Method Details
.batch_refresh(registrations, instance_id, revision) ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/solid_objects/turbo_stream_renderer.rb', line 58 def batch_refresh(registrations, instance_id, revision) first = registrations.first scope = DomIdentity.scope(first.reference) batch = ERB::Util.html_escape(first.batch) source = ERB::Util.html_escape( first.batch_refresh_url(registrations, instance_id, revision) ) targets = ERB::Util.html_escape(registrations.map(&:dom_id).join(" ")) %(<turbo-stream action="append" target="#{scope}"><template><solid-objects-batch-refresh data-batch="#{batch}" data-revision="#{instance_id}:#{revision}" data-targets="#{targets}" data-source="#{source}"></solid-objects-batch-refresh></template></turbo-stream>) end |
.component_refresh(registration, instance_id, revision) ⇒ Object
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/solid_objects/turbo_stream_renderer.rb', line 46 def component_refresh(registration, instance_id, revision) return morph_component_refresh(registration, instance_id, revision) if registration.morph? target = registration.dom_id source = ERB::Util.html_escape( registration.refresh_url(instance_id, revision) ) revision_value = "#{instance_id}:#{revision}" %(<turbo-stream action="replace" target="#{target}"><template><turbo-frame id="#{target}" src="#{source}" data-solid-objects-revision="#{revision_value}" data-solid-objects-refresh="replace"></turbo-frame></template></turbo-stream>) end |
.invalidation(stream) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/solid_objects/turbo_stream_renderer.rb', line 83 def invalidation(stream) encoded = stream[INVALIDATION_PATTERN, 1] return unless encoded payload = JSON.parse(Base64.urlsafe_decode64(encoded)) return unless payload["instance_id"].is_a?(Integer) return unless payload["revision"].is_a?(Integer) return unless payload["observable_name"].is_a?(String) payload rescue ArgumentError, JSON::ParserError nil end |
.observable(broadcast) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/solid_objects/turbo_stream_renderer.rb', line 14 def observable(broadcast) reference = Reference.new( actor_type: broadcast.instance.actor_type, actor_id: broadcast.instance.actor_id ) stream = if broadcast.observable_name == PayloadBroadcast::REVISION_OBSERVABLE "" else observable_value( reference, broadcast.observable_name, broadcast.value ) end = Base64.urlsafe_encode64( JSON.generate( "instance_id" => broadcast.instance_id, "revision" => broadcast..sequence, "observable_name" => broadcast.observable_name ) ) "#{stream}<!--#{INVALIDATION_MARKER}#{}-->" end |
.observable_value(reference, name, value) ⇒ Object
39 40 41 42 43 |
# File 'lib/solid_objects/turbo_stream_renderer.rb', line 39 def observable_value(reference, name, value) target = DomIdentity.observable(reference, name) content = ERB::Util.html_escape(display_value(value)) %(<turbo-stream action="replace" target="#{target}"><template><span id="#{target}">#{content}</span></template></turbo-stream>) end |
.state_payload(payload) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/solid_objects/turbo_stream_renderer.rb', line 70 def state_payload(payload) reference = Reference.new( actor_type: payload.fetch("actor_type"), actor_id: payload.fetch("actor_id") ) scope = DomIdentity.scope(reference) name = ERB::Util.html_escape(payload.fetch("name")) revision = "#{payload.fetch("instance_id")}:#{payload.fetch("revision")}" body = ERB::Util.html_escape(JSON.generate(payload.fetch("payload"))) %(<turbo-stream action="append" target="#{scope}"><template><solid-objects-payload data-name="#{name}" data-revision="#{revision}">#{body}</solid-objects-payload></template></turbo-stream>) end |
Instance Method Details
#self?.batch_refresh ⇒ String
19 |
# File 'sig/generated/lib/solid_objects/turbo_stream_renderer.rbs', line 19
def self?.batch_refresh: (Array[ComponentRegistration], Integer, Integer) -> String
|
#self?.component_refresh ⇒ String
16 |
# File 'sig/generated/lib/solid_objects/turbo_stream_renderer.rbs', line 16
def self?.component_refresh: (ComponentRegistration, Integer, Integer) -> String
|
#self?.display_value ⇒ String
28 |
# File 'sig/generated/lib/solid_objects/turbo_stream_renderer.rbs', line 28
def self?.display_value: (untyped) -> String
|
#self?.invalidation ⇒ Hash[String, untyped]?
25 |
# File 'sig/generated/lib/solid_objects/turbo_stream_renderer.rbs', line 25
def self?.invalidation: (String) -> Hash[String, untyped]?
|
#self?.morph_component_refresh ⇒ String
31 |
# File 'sig/generated/lib/solid_objects/turbo_stream_renderer.rbs', line 31
def self?.morph_component_refresh: (ComponentRegistration, Integer, Integer) -> String
|
#self?.observable ⇒ String
10 |
# File 'sig/generated/lib/solid_objects/turbo_stream_renderer.rbs', line 10
def self?.observable: (Broadcast) -> String
|
#self?.observable_value ⇒ String
13 |
# File 'sig/generated/lib/solid_objects/turbo_stream_renderer.rbs', line 13
def self?.observable_value: (Reference, Symbol | String, untyped) -> String
|
#self?.state_payload ⇒ String
22 |
# File 'sig/generated/lib/solid_objects/turbo_stream_renderer.rbs', line 22
def self?.state_payload: (Hash[String, untyped]) -> String
|