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::SETTLE_DURATION_DATALINE_LITERAL => Consts::DEFAULT_FRAGMENTS_SETTLE_DURATION, 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.
36 37 38 39 40 |
# File 'lib/datastar/server_sent_event_generator.rb', line 36 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.
34 35 36 |
# File 'lib/datastar/server_sent_event_generator.rb', line 34 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.
45 46 47 |
# File 'lib/datastar/server_sent_event_generator.rb', line 45 def check_connection! @stream << MSG_END end |
#execute_script(script, options = BLANK_OPTIONS) ⇒ Object
94 95 96 97 98 99 100 101 102 |
# File 'lib/datastar/server_sent_event_generator.rb', line 94 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
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/datastar/server_sent_event_generator.rb', line 49 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
76 77 78 79 80 81 82 83 |
# File 'lib/datastar/server_sent_event_generator.rb', line 76 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
104 105 106 |
# File 'lib/datastar/server_sent_event_generator.rb', line 104 def redirect(url) execute_script %(setTimeout(() => { window.location = '#{url}' })) end |
#remove_fragments(selector, options = BLANK_OPTIONS) ⇒ Object
69 70 71 72 73 74 |
# File 'lib/datastar/server_sent_event_generator.rb', line 69 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
85 86 87 88 89 90 91 92 |
# File 'lib/datastar/server_sent_event_generator.rb', line 85 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
108 109 110 111 |
# File 'lib/datastar/server_sent_event_generator.rb', line 108 def write(buffer) buffer << MSG_END @stream << buffer end |