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_ELEMENTS_VIEW_TRANSITION_SELECTOR =
''- DEFAULT_ELEMENTS_NAMESPACE =
'html'- 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'- VIEW_TRANSITION_SELECTOR_DATALINE_LITERAL =
'viewTransitionSelector'- NAMESPACE_DATALINE_LITERAL =
'namespace'- 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, VIEW_TRANSITION_SELECTOR_DATALINE_LITERAL => DEFAULT_ELEMENTS_VIEW_TRANSITION_SELECTOR, NAMESPACE_DATALINE_LITERAL => DEFAULT_ELEMENTS_NAMESPACE, 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.
75 76 77 78 79 |
# File 'lib/datastar/server_sent_event_generator.rb', line 75 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.
70 71 72 |
# File 'lib/datastar/server_sent_event_generator.rb', line 70 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.
84 85 86 |
# File 'lib/datastar/server_sent_event_generator.rb', line 84 def check_connection! @stream << MSG_END end |
#execute_script(script, options = BLANK_OPTIONS) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/datastar/server_sent_event_generator.rb', line 138 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
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/datastar/server_sent_event_generator.rb', line 88 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
115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/datastar/server_sent_event_generator.rb', line 115 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
155 156 157 |
# File 'lib/datastar/server_sent_event_generator.rb', line 155 def redirect(url) execute_script %(setTimeout(() => { window.location = '#{url}' })) end |
#remove_elements(selector, options = BLANK_OPTIONS) ⇒ Object
105 106 107 108 109 110 111 112 113 |
# File 'lib/datastar/server_sent_event_generator.rb', line 105 def remove_elements(selector, = BLANK_OPTIONS) patch_elements( nil, .merge( MODE_DATALINE_LITERAL => ElementPatchMode::REMOVE, selector: ) ) end |
#remove_signals(paths, options = BLANK_OPTIONS) ⇒ Object
128 129 130 131 132 133 134 135 136 |
# File 'lib/datastar/server_sent_event_generator.rb', line 128 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
159 160 161 162 |
# File 'lib/datastar/server_sent_event_generator.rb', line 159 def write(buffer) buffer << MSG_END @stream << buffer end |