Class: Datastar::ServerSentEventGenerator
- Inherits:
-
Object
- Object
- Datastar::ServerSentEventGenerator
- Defined in:
- lib/datastar/server_sent_event_generator.rb
Constant Summary collapse
- MSG_END =
"\n\n"- SSE_OPTION_MAPPING =
{ 'eventId' => 'id', 'retryDuration' => 'retry', 'id' => 'id', 'retry' => 'retry', }.freeze
- OPTION_DEFAULTS =
{ 'retry' => Consts::DEFAULT_SSE_RETRY_DURATION, Consts::AUTO_REMOVE_DATALINE_LITERAL => Consts::DEFAULT_EXECUTE_SCRIPT_AUTO_REMOVE, Consts::MERGE_MODE_DATALINE_LITERAL => Consts::DEFAULT_FRAGMENT_MERGE_MODE, Consts::USE_VIEW_TRANSITION_DATALINE_LITERAL => Consts::DEFAULT_FRAGMENTS_USE_VIEW_TRANSITIONS, Consts::ONLY_IF_MISSING_DATALINE_LITERAL => Consts::DEFAULT_MERGE_SIGNALS_ONLY_IF_MISSING, }.freeze
- ATTRIBUTE_DEFAULTS =
ATTRIBUTE_DEFAULTS =
'type' => 'module'.freeze
Consts::DEFAULT_EXECUTE_SCRIPT_ATTRIBUTES .split("\n") .map { |attr| attr.split(' ') } .to_h .freeze
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.
- #merge_fragments(fragments, options = BLANK_OPTIONS) ⇒ Object
- #merge_signals(signals, options = BLANK_OPTIONS) ⇒ Object
- #redirect(url) ⇒ Object
- #remove_fragments(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.
35 36 37 38 39 |
# File 'lib/datastar/server_sent_event_generator.rb', line 35 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.
33 34 35 |
# File 'lib/datastar/server_sent_event_generator.rb', line 33 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.
44 45 46 |
# File 'lib/datastar/server_sent_event_generator.rb', line 44 def check_connection! @stream << MSG_END end |
#execute_script(script, options = BLANK_OPTIONS) ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'lib/datastar/server_sent_event_generator.rb', line 93 def execute_script(script, = BLANK_OPTIONS) buffer = +"event: datastar-execute-script\n" (, buffer) scripts = script.to_s.split("\n") scripts.each do |sc| buffer << "data: script #{sc}\n" end write(buffer) end |
#merge_fragments(fragments, options = BLANK_OPTIONS) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/datastar/server_sent_event_generator.rb', line 48 def merge_fragments(fragments, = BLANK_OPTIONS) # Support Phlex components # And Rails' #render_in interface fragments = if fragments.respond_to?(:render_in) fragments.render_in(view_context) elsif fragments.respond_to?(:call) fragments.call(view_context:) else fragments.to_s end fragment_lines = fragments.to_s.split("\n") buffer = +"event: datastar-merge-fragments\n" (, buffer) fragment_lines.each { |line| buffer << "data: fragments #{line}\n" } write(buffer) end |
#merge_signals(signals, options = BLANK_OPTIONS) ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'lib/datastar/server_sent_event_generator.rb', line 75 def merge_signals(signals, = BLANK_OPTIONS) signals = JSON.dump(signals) unless signals.is_a?(String) buffer = +"event: datastar-merge-signals\n" (, buffer) buffer << "data: signals #{signals}\n" write(buffer) end |
#redirect(url) ⇒ Object
103 104 105 |
# File 'lib/datastar/server_sent_event_generator.rb', line 103 def redirect(url) execute_script %(setTimeout(() => { window.location = '#{url}' })) end |
#remove_fragments(selector, options = BLANK_OPTIONS) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/datastar/server_sent_event_generator.rb', line 68 def remove_fragments(selector, = BLANK_OPTIONS) buffer = +"event: datastar-remove-fragments\n" (, buffer) buffer << "data: selector #{selector}\n" write(buffer) end |
#remove_signals(paths, options = BLANK_OPTIONS) ⇒ Object
84 85 86 87 88 89 90 91 |
# File 'lib/datastar/server_sent_event_generator.rb', line 84 def remove_signals(paths, = BLANK_OPTIONS) paths = [paths].flatten buffer = +"event: datastar-remove-signals\n" (, buffer) paths.each { |path| buffer << "data: paths #{path}\n" } write(buffer) end |
#write(buffer) ⇒ Object
107 108 109 110 |
# File 'lib/datastar/server_sent_event_generator.rb', line 107 def write(buffer) buffer << MSG_END @stream << buffer end |