Skip to content

Commit d67b8ff

Browse files
authored
fix: compact and remove unused constants (#11)
1 parent 48a067d commit d67b8ff

4 files changed

Lines changed: 49 additions & 75 deletions

File tree

README.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -364,12 +364,6 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
364364
365365
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
366366
367-
### Building
368-
369-
To build `consts.rb` file from template, run Docker and run `make task build`
370-
371-
The template is located at `build/consts_ruby.gtpl`.
372-
373367
## Contributing
374368
375369
Bug reports and pull requests are welcome on GitHub at https://github.com/starfederation/datastar.

lib/datastar.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# frozen_string_literal: true
22

33
require_relative 'datastar/version'
4-
require_relative 'datastar/consts'
5-
64
module Datastar
75
BLANK_OPTIONS = {}.freeze
86

lib/datastar/consts.rb

Lines changed: 0 additions & 57 deletions
This file was deleted.

lib/datastar/server_sent_event_generator.rb

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,60 @@
33
require 'json'
44

55
module Datastar
6+
module ElementPatchMode
7+
# Morphs the element into the existing element.
8+
OUTER = 'outer'
9+
10+
# Replaces the inner HTML of the existing element.
11+
INNER = 'inner'
12+
13+
# Removes the existing element.
14+
REMOVE = 'remove'
15+
16+
# Replaces the existing element with the new element.
17+
REPLACE = 'replace'
18+
19+
# Prepends the element inside to the existing element.
20+
PREPEND = 'prepend'
21+
22+
# Appends the element inside the existing element.
23+
APPEND = 'append'
24+
25+
# Inserts the element before the existing element.
26+
BEFORE = 'before'
27+
28+
# Inserts the element after the existing element.
29+
AFTER = 'after'
30+
end
31+
632
class ServerSentEventGenerator
733
MSG_END = "\n"
834

35+
DEFAULT_SSE_RETRY_DURATION = 1000
36+
DEFAULT_ELEMENTS_USE_VIEW_TRANSITIONS = false
37+
DEFAULT_PATCH_SIGNALS_ONLY_IF_MISSING = false
38+
39+
SELECTOR_DATALINE_LITERAL = 'selector'
40+
MODE_DATALINE_LITERAL = 'mode'
41+
ELEMENTS_DATALINE_LITERAL = 'elements'
42+
USE_VIEW_TRANSITION_DATALINE_LITERAL = 'useViewTransition'
43+
SIGNALS_DATALINE_LITERAL = 'signals'
44+
ONLY_IF_MISSING_DATALINE_LITERAL = 'onlyIfMissing'
45+
946
SSE_OPTION_MAPPING = {
1047
'eventId' => 'id',
1148
'retryDuration' => 'retry',
1249
'id' => 'id',
1350
'retry' => 'retry',
1451
}.freeze
1552

53+
DEFAULT_ELEMENT_PATCH_MODE = ElementPatchMode::OUTER
54+
1655
OPTION_DEFAULTS = {
17-
'retry' => Consts::DEFAULT_SSE_RETRY_DURATION,
18-
Consts::MODE_DATALINE_LITERAL => Consts::DEFAULT_ELEMENT_PATCH_MODE,
19-
Consts::USE_VIEW_TRANSITION_DATALINE_LITERAL => Consts::DEFAULT_ELEMENTS_USE_VIEW_TRANSITIONS,
20-
Consts::ONLY_IF_MISSING_DATALINE_LITERAL => Consts::DEFAULT_PATCH_SIGNALS_ONLY_IF_MISSING,
56+
'retry' => DEFAULT_SSE_RETRY_DURATION,
57+
MODE_DATALINE_LITERAL => DEFAULT_ELEMENT_PATCH_MODE,
58+
USE_VIEW_TRANSITION_DATALINE_LITERAL => DEFAULT_ELEMENTS_USE_VIEW_TRANSITIONS,
59+
ONLY_IF_MISSING_DATALINE_LITERAL => DEFAULT_PATCH_SIGNALS_ONLY_IF_MISSING,
2160
}.freeze
2261

2362
SIGNAL_SEPARATOR = '.'
@@ -52,7 +91,7 @@ def patch_elements(elements, options = BLANK_OPTIONS)
5291

5392
buffer = +"event: datastar-patch-elements\n"
5493
build_options(options, buffer)
55-
element_lines.each { |line| buffer << "data: #{Consts::ELEMENTS_DATALINE_LITERAL} #{line}\n" }
94+
element_lines.each { |line| buffer << "data: #{ELEMENTS_DATALINE_LITERAL} #{line}\n" }
5695

5796
write(buffer)
5897
end
@@ -61,7 +100,7 @@ def remove_elements(selector, options = BLANK_OPTIONS)
61100
patch_elements(
62101
nil,
63102
options.merge(
64-
Consts::MODE_DATALINE_LITERAL => Consts::ElementPatchMode::REMOVE,
103+
MODE_DATALINE_LITERAL => ElementPatchMode::REMOVE,
65104
selector:
66105
)
67106
)
@@ -75,7 +114,7 @@ def patch_signals(signals, options = BLANK_OPTIONS)
75114
signals = JSON.dump(signals)
76115
buffer << "data: signals #{signals}\n"
77116
when String
78-
multi_data_lines(signals, buffer, Consts::SIGNALS_DATALINE_LITERAL)
117+
multi_data_lines(signals, buffer, SIGNALS_DATALINE_LITERAL)
79118
end
80119
write(buffer)
81120
end
@@ -101,8 +140,8 @@ def execute_script(script, options = BLANK_OPTIONS)
101140
script_tag << %( data-effect="el.remove()") if auto_remove
102141
script_tag << ">#{script}</script>"
103142

104-
options[Consts::SELECTOR_DATALINE_LITERAL] = 'body'
105-
options[Consts::MODE_DATALINE_LITERAL] = Consts::ElementPatchMode::APPEND
143+
options[SELECTOR_DATALINE_LITERAL] = 'body'
144+
options[MODE_DATALINE_LITERAL] = ElementPatchMode::APPEND
106145

107146
patch_elements(script_tag, options)
108147
end
@@ -143,7 +182,7 @@ def build_options(options, buffer)
143182
buffer << "data: #{k} #{kk} #{vv}\n"
144183
end
145184
elsif v.is_a?(Array)
146-
if k == Consts::SELECTOR_DATALINE_LITERAL
185+
if k == SELECTOR_DATALINE_LITERAL
147186
buffer << "data: #{k} #{v.join(', ')}\n"
148187
else
149188
buffer << "data: #{k} #{v.join(' ')}\n"

0 commit comments

Comments
 (0)