Class: Datastar::ServerSentEventGenerator

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(stream, signals:, view_context: nil) ⇒ ServerSentEventGenerator

Returns a new instance of ServerSentEventGenerator.

Parameters:

  • stream (IO, Queue)

    The IO stream or Queue to write to

  • signals (Hash)

    a customizable set of options

  • view_context (Hash) (defaults to: nil)

    a customizable set of options

Options Hash (signals:):

  • A (Hash)

    hash of signals (params)

Options Hash (view_context:):

  • The (Object)

    view context for rendering elements, if applicable.



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

#signalsObject (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, options = BLANK_OPTIONS)
  options = camelize_keys(options)
  auto_remove = options.key?('autoRemove') ? options.delete('autoRemove') : true
  attributes = options.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>"

  options[SELECTOR_DATALINE_LITERAL] = 'body'
  options[MODE_DATALINE_LITERAL] = ElementPatchMode::APPEND

  patch_elements(script_tag, options)
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, options = 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"
  build_options(options, 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, options = BLANK_OPTIONS)
  buffer = +"event: datastar-patch-signals\n"
  build_options(options, 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, options = BLANK_OPTIONS)
  patch_elements(
    nil, 
    options.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, options = 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, options)
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