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 =

Returns:

  • (::String)
"solid_objects_invalidation:"
INVALIDATION_PATTERN =

Returns:

  • (::Regexp)
/<!--solid_objects_invalidation:([A-Za-z0-9_=-]+)-->/

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.component_refresh(registration, instance_id, revision) ⇒ Object

RBS:

  • (ComponentRegistration, Integer, Integer) -> String



42
43
44
45
46
47
48
49
50
51
# File 'lib/solid_objects/turbo_stream_renderer.rb', line 42

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

RBS:

  • (String) -> Hash[String, untyped]?



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/solid_objects/turbo_stream_renderer.rb', line 54

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

RBS:

  • (Broadcast) -> String



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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 = observable_value(
    reference,
    broadcast.observable_name,
    broadcast.value
  )
   = Base64.urlsafe_encode64(
    JSON.generate(
      "instance_id" => broadcast.instance_id,
      "revision" => broadcast.message.sequence,
      "observable_name" => broadcast.observable_name
    )
  )
  "#{stream}<!--#{INVALIDATION_MARKER}#{}-->"
end

.observable_value(reference, name, value) ⇒ Object

RBS:

  • (Reference, Symbol | String, untyped) -> String



35
36
37
38
39
# File 'lib/solid_objects/turbo_stream_renderer.rb', line 35

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

Instance Method Details

#self?.component_refreshString

RBS:

  • (ComponentRegistration, Integer, Integer) -> String

Parameters:

Returns:

  • (String)


16
# File 'sig/generated/lib/solid_objects/turbo_stream_renderer.rbs', line 16

def self?.component_refresh: (ComponentRegistration, Integer, Integer) -> String

#self?.display_valueString

RBS:

  • (untyped) -> String

Parameters:

  • (Object)

Returns:

  • (String)


22
# File 'sig/generated/lib/solid_objects/turbo_stream_renderer.rbs', line 22

def self?.display_value: (untyped) -> String

#self?.invalidationHash[String, untyped]?

RBS:

  • (String) -> Hash[String, untyped]?

Parameters:

  • (String)

Returns:

  • (Hash[String, untyped], nil)


19
# File 'sig/generated/lib/solid_objects/turbo_stream_renderer.rbs', line 19

def self?.invalidation: (String) -> Hash[String, untyped]?

#self?.morph_component_refreshString

RBS:

  • (ComponentRegistration, Integer, Integer) -> String

Parameters:

Returns:

  • (String)


25
# File 'sig/generated/lib/solid_objects/turbo_stream_renderer.rbs', line 25

def self?.morph_component_refresh: (ComponentRegistration, Integer, Integer) -> String

#self?.observableString

RBS:

  • (Broadcast) -> String

Parameters:

Returns:

  • (String)


10
# File 'sig/generated/lib/solid_objects/turbo_stream_renderer.rbs', line 10

def self?.observable: (Broadcast) -> String

#self?.observable_valueString

RBS:

  • (Reference, Symbol | String, untyped) -> String

Parameters:

Returns:

  • (String)


13
# File 'sig/generated/lib/solid_objects/turbo_stream_renderer.rbs', line 13

def self?.observable_value: (Reference, Symbol | String, untyped) -> String