Skip to content

Commit

Permalink
Drop turbo dependency and rely on vanilla action cable
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgemanrubia committed Dec 5, 2024
1 parent 5c80e36 commit 1e7cd99
Show file tree
Hide file tree
Showing 20 changed files with 3,503 additions and 10,602 deletions.
4 changes: 0 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ PATH
hotwire_spark (0.1.0)
listen
rails (>= 8.0.0)
turbo-rails
zeitwerk

GEM
Expand Down Expand Up @@ -239,9 +238,6 @@ GEM
stringio (3.1.2)
thor (1.3.2)
timeout (0.4.2)
turbo-rails (2.0.11)
actionpack (>= 6.0.0)
railties (>= 6.0.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.6.0)
Expand Down
13,926 changes: 3,440 additions & 10,486 deletions app/assets/javascripts/hotwire_spark.js

Large diffs are not rendered by default.

29 changes: 1 addition & 28 deletions app/assets/javascripts/hotwire_spark.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion app/assets/javascripts/hotwire_spark.min.js.map

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions app/channels/hotwire_spark/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class HotwireSpark::Channel < ActionCable::Channel::Base
def subscribed
stream_from "hotwire_spark"
end
end
5 changes: 4 additions & 1 deletion app/javascript/hotwire_spark/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import "./stream_actions"
import { ReloadingMonitor } from "./reloading_monitor.js";

const HotwireSpark = {
config: {
Expand All @@ -7,3 +7,6 @@ const HotwireSpark = {
}

export default HotwireSpark

ReloadingMonitor.start()

1 change: 0 additions & 1 deletion app/javascript/hotwire_spark/logger.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import "./stream_actions"
import HotwireSpark from "./index.js"

export function log(...args) {
Expand Down
9 changes: 1 addition & 8 deletions app/javascript/hotwire_spark/reloaders/html_reloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ export class HtmlReloader {
}

#updateBody(newBody) {
Idiomorph.morph(document.body, newBody, {
callbacks: {
beforeNodeMorphed: function (oldNode, newNode) {
const value = !(oldNode instanceof HTMLElement) || !oldNode.closest("turbo-cable-stream-source")
return value
}
}
})
Idiomorph.morph(document.body, newBody)
}
}
48 changes: 48 additions & 0 deletions app/javascript/hotwire_spark/reloading_monitor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { HtmlReloader } from "./reloaders/html_reloader.js"
import { CssReloader } from "./reloaders/css_reloader.js";
import { StimulusReloader } from "./reloaders/stimulus_reloader.js";
import { nameFromFilePath } from "./helpers.js";
import { createConsumer } from "@rails/actioncable"

export class ReloadingMonitor {
static start() {
new ReloadingMonitor().start()
}

start() {
const consumer = createConsumer()

consumer.subscriptions.create({ channel: "HotwireSpark::Channel" }, {
received(data) {
this.dispatchMessage(data)
},

dispatchMessage({ action, path }) {
const filePath = nameFromFilePath(path)
switch (action) {
case "reload_html":
this.reloadHtml()
break
case "reload_css":
this.reloadCss(filePath)
break
case "reload_stimulus":
this.reloadStimulus(filePath)
break
}
},

reloadHtml() {
HtmlReloader.reload()
},

reloadCss(path) {
CssReloader.reload(new RegExp(path))
},

reloadStimulus(path) {
StimulusReloader.reload(new RegExp(path))
}
})
}
}
3 changes: 0 additions & 3 deletions app/javascript/hotwire_spark/stream_actions/index.js

This file was deleted.

8 changes: 0 additions & 8 deletions app/javascript/hotwire_spark/stream_actions/reload_css.js

This file was deleted.

6 changes: 0 additions & 6 deletions app/javascript/hotwire_spark/stream_actions/reload_html.js

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion hotwire_spark.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ Gem::Specification.new do |spec|
spec.add_dependency "rails", ">= 8.0.0"
spec.add_dependency "zeitwerk"
spec.add_dependency "listen"
spec.add_dependency "turbo-rails"

spec.add_development_dependency "rubocop"
spec.add_development_dependency "rubocop-rails-omakase"
Expand Down
16 changes: 0 additions & 16 deletions lib/hotwire_spark/action_cable/streams_channel.rb

This file was deleted.

1 change: 0 additions & 1 deletion lib/hotwire_spark/engine.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require "turbo-rails"
require "action_cable/server/base"

module HotwireSpark
Expand Down
8 changes: 3 additions & 5 deletions lib/hotwire_spark/installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,15 @@ def monitor_paths

def monitor(paths_name, action:)
file_watcher.monitor HotwireSpark.public_send(paths_name) do |file_path|
ActionCable.server.broadcast "hotwire_spark", stream_action_for(action, file_path)
ActionCable.server.broadcast "hotwire_spark", change_message_for(action, file_path)
end
end

def file_watcher
@file_watches ||= HotwireSpark::FileWatcher.new
end

def stream_action_for(action, file_path)
<<~HTML
<turbo-stream action="#{action}" file_path="#{file_path}">
HTML
def change_message_for(action, file_path)
{ action: action, path: file_path }
end
end
6 changes: 0 additions & 6 deletions lib/hotwire_spark/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ def call(env)
if html_response?(headers)
html = html_from(response)
html = inject_javascript(html)
html = inject_turbo_stream(html)
headers["Content-Length"] = html.bytesize.to_s if html
response = [ html ]
end
Expand All @@ -33,9 +32,4 @@ def inject_javascript(html)
script_tag = ActionController::Base.helpers.javascript_include_tag(script_path)
html.sub("</head>", "#{script_tag}</head>")
end

def inject_turbo_stream(html)
turbo_stream_tag = ActionController::Base.helpers.turbo_stream_from "hotwire_spark", channel: HotwireSpark::ActionCable::StreamsChannel
html.sub("</body>", "#{turbo_stream_tag}</body>")
end
end
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"@babel/core": "^7.23.0",
"@babel/preset-env": "^7.26.0",
"@hotwired/stimulus": "^3.2.2",
"@hotwired/turbo-rails": "^8.0.12",
"@rails/actioncable": "^8.0.0",
"@rollup/plugin-babel": "^6.0.4",
"@rollup/plugin-commonjs": "^28.0.1",
Expand Down
18 changes: 0 additions & 18 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -806,19 +806,6 @@
resolved "https://registry.yarnpkg.com/@hotwired/stimulus/-/stimulus-3.2.2.tgz#071aab59c600fed95b97939e605ff261a4251608"
integrity sha512-eGeIqNOQpXoPAIP7tC1+1Yc1yl1xnwYqg+3mzqxyrbE5pg5YFBZcA6YoTiByJB6DKAEsiWtl6tjTJS4IYtbB7A==

"@hotwired/turbo-rails@^8.0.12":
version "8.0.12"
resolved "https://registry.yarnpkg.com/@hotwired/turbo-rails/-/turbo-rails-8.0.12.tgz#6f1a2661122c0a2bf717f3bc68b5106638798c89"
integrity sha512-ZXwu9ez+Gd4RQNeHIitqOQgi/LyqY8J4JqsUN0nnYiZDBRq7IreeFdMbz29VdJpIsmYqwooE4cFzPU7QvJkQkA==
dependencies:
"@hotwired/turbo" "^8.0.12"
"@rails/actioncable" "^7.0"

"@hotwired/turbo@^8.0.12":
version "8.0.12"
resolved "https://registry.yarnpkg.com/@hotwired/turbo/-/turbo-8.0.12.tgz#50aa8345d7f62402680c6d2d9814660761837001"
integrity sha512-l3BiQRkD7qrnQv6ms6sqPLczvwbQpXt5iAVwjDvX0iumrz6yEonQkNAzNjeDX25/OJMFDTxpHjkJZHGpM9ikWw==

"@jridgewell/gen-mapping@^0.3.5":
version "0.3.5"
resolved "https://registry.yarnpkg.com/@jridgewell/gen-mapping/-/gen-mapping-0.3.5.tgz#dcce6aff74bdf6dad1a95802b69b04a2fcb1fb36"
Expand Down Expand Up @@ -859,11 +846,6 @@
"@jridgewell/resolve-uri" "^3.1.0"
"@jridgewell/sourcemap-codec" "^1.4.14"

"@rails/actioncable@^7.0":
version "7.2.200"
resolved "https://registry.yarnpkg.com/@rails/actioncable/-/actioncable-7.2.200.tgz#7f56b3313762dbb85b64490aa33c5f431419aafd"
integrity sha512-gVmi3MabEa+Bkatvw0/k1Y3WTidcIf3qNbb9L8qc+AmT2UmkVqUZhJpSLvKmH10twCYIGzn7yySW/lOpg81Duw==

"@rails/actioncable@^8.0.0":
version "8.0.0"
resolved "https://registry.yarnpkg.com/@rails/actioncable/-/actioncable-8.0.0.tgz#b12d87d4c2bb152c4445e2c168df93b585068e7c"
Expand Down

0 comments on commit 1e7cd99

Please sign in to comment.