Class: Datastar::ServerSentEventGenerator
- Inherits:
-
Object
- Object
- Datastar::ServerSentEventGenerator
- Defined in:
- lib/datastar/server_sent_event_generator.rb
Constant Summary collapse
- MSG_END =
"\n"- DEFAULT_SSE_RETRY_DURATION =
1000- DEFAULT_ELEMENTS_USE_VIEW_TRANSITIONS =
false- DEFAULT_PATCH_SIGNALS_ONLY_IF_MISSING =
false- SELECTOR_DATALINE_LITERAL =
'selector'- MODE_DATALINE_LITERAL =
'mode'- ELEMENTS_DATALINE_LITERAL =
'elements'- USE_VIEW_TRANSITION_DATALINE_LITERAL =
'useViewTransition'- SIGNALS_DATALINE_LITERAL =
'signals'- ONLY_IF_MISSING_DATALINE_LITERAL =
'onlyIfMissing'- SSE_OPTION_MAPPING =
{ 'eventId' => 'id', 'retryDuration' => 'retry', 'id' => 'id', 'retry' => 'retry', }.freeze
- DEFAULT_ELEMENT_PATCH_MODE =
ElementPatchMode::OUTER
- OPTION_DEFAULTS =
{ 'retry' => DEFAULT_SSE_RETRY_DURATION, MODE_DATALINE_LITERAL => DEFAULT_ELEMENT_PATCH_MODE, USE_VIEW_TRANSITION_DATALINE_LITERAL => DEFAULT_ELEMENTS_USE_VIEW_TRANSITIONS, ONLY_IF_MISSING_DATALINE_LITERAL => DEFAULT_PATCH_SIGNALS_ONLY_IF_MISSING, }.freeze
- SIGNAL_SEPARATOR =
'.'
Instance Attribute Summary collapse
-
#signals ⇒ Object
readonly
Returns the value of attribute signals.
Instance Method Summary collapse
-
#check_connection! ⇒ Object
Sometimes we’ll want to run periodic checks to ensure the connection is still alive ie.
- #execute_script(script, options = BLANK_OPTIONS) ⇒ Object
-
#initialize(stream, signals:, view_context: nil) ⇒ ServerSentEventGenerator
constructor
A new instance of ServerSentEventGenerator.
- #patch_elements(elements, options = BLANK_OPTIONS) ⇒ Object
- #patch_signals(signals, options = BLANK_OPTIONS) ⇒ Object
- #redirect(url) ⇒ Object
- #remove_elements(selector, options = BLANK_OPTIONS) ⇒ Object
- #remove_signals(paths, options = BLANK_OPTIONS) ⇒ Object
- #write(buffer) ⇒ Object
Constructor Details
#initialize(stream, signals:, view_context: nil) ⇒ ServerSentEventGenerator
Returns a new instance of ServerSentEventGenerator.
69 70 71 72 73 |
# File 'lib/datastar/server_sent_event_generator.rb', line 69 def initialize(stream, signals:, view_context: nil) @stream = stream @signals = signals @view_context = view_context end |
Instance Attribute Details
#signals ⇒ Object (readonly)
Returns the value of attribute signals.
64 65 66 |
# File 'lib/datastar/server_sent_event_generator.rb', line 64 def signals @signals end |
Instance Method Details
#check_connection! ⇒ Object
Sometimes we’ll want to run periodic checks to ensure the connection is still alive ie. the browser hasn’t disconnected For example when idle listening on an event bus.
78 79 80 |
# File 'lib/datastar/server_sent_event_generator.rb', line 78 def check_connection! @stream << MSG_END end |
#execute_script(script, options = BLANK_OPTIONS) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/datastar/server_sent_event_generator.rb', line 132 def execute_script(script, = BLANK_OPTIONS) = camelize_keys() auto_remove = .key?('autoRemove') ? .delete('autoRemove') : true attributes = .delete('attributes') || BLANK_OPTIONS script_tag = +"<script" attributes.each do |k, v| script_tag << %( #{camelize(k)}="#{v}") end script_tag << %( data-effect="el.remove()") if auto_remove script_tag << ">#{script}</script>" [SELECTOR_DATALINE_LITERAL] = 'body' [MODE_DATALINE_LITERAL] = ElementPatchMode::APPEND patch_elements(script_tag, ) end |
#patch_elements(elements, options = BLANK_OPTIONS) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/datastar/server_sent_event_generator.rb', line 82 def patch_elements(elements, = BLANK_OPTIONS) elements = Array(elements).compact rendered_elements = elements.map do |element| render_element(element) end element_lines = rendered_elements.flat_map do |el| el.to_s.split("\n") end buffer = +"event: datastar-patch-elements\n" (, buffer) element_lines.each { |line| buffer << "data: #{ELEMENTS_DATALINE_LITERAL} #{line}\n" } write(buffer) end |
#patch_signals(signals, options = BLANK_OPTIONS) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/datastar/server_sent_event_generator.rb', line 109 def patch_signals(signals, = BLANK_OPTIONS) buffer = +"event: datastar-patch-signals\n" (, buffer) case signals when Hash signals = JSON.dump(signals) buffer << "data: signals #{signals}\n" when String multi_data_lines(signals, buffer, SIGNALS_DATALINE_LITERAL) end write(buffer) end |
#redirect(url) ⇒ Object
149 150 151 |
# File 'lib/datastar/server_sent_event_generator.rb', line 149 def redirect(url) execute_script %(setTimeout(() => { window.location = '#{url}' })) end |
#remove_elements(selector, options = BLANK_OPTIONS) ⇒ Object
99 100 101 102 103 104 105 106 107 |
# File 'lib/datastar/server_sent_event_generator.rb', line 99 def remove_elements(selector, = BLANK_OPTIONS) patch_elements( nil, .merge( MODE_DATALINE_LITERAL => ElementPatchMode::REMOVE, selector: ) ) end |
#remove_signals(paths, options = BLANK_OPTIONS) ⇒ Object
122 123 124 125 126 127 128 129 130 |
# File 'lib/datastar/server_sent_event_generator.rb', line 122 def remove_signals(paths, = BLANK_OPTIONS) paths = [paths].flatten signals = paths.each.with_object({}) do |path, acc| parts = path.split(SIGNAL_SEPARATOR) set_nested_value(acc, parts, nil) end patch_signals(signals, ) end |
#write(buffer) ⇒ Object
153 154 155 156 |
# File 'lib/datastar/server_sent_event_generator.rb', line 153 def write(buffer) buffer << MSG_END @stream << buffer end |