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_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

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.



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

#signalsObject (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, 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



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, 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



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, 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



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, options = BLANK_OPTIONS)
  patch_elements(
    nil, 
    options.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, 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



159
160
161
162
# File 'lib/datastar/server_sent_event_generator.rb', line 159

def write(buffer)
  buffer << MSG_END
  @stream << buffer
end