Skip to content

Commit

Permalink
Merge pull request #37 from basecamp/isolated-ac-server
Browse files Browse the repository at this point in the history
Use a separate action cable server for hotwire-spark
  • Loading branch information
jorgemanrubia authored Dec 21, 2024
2 parents adb851d + 9a76cff commit dda1b6e
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 84 deletions.
2 changes: 1 addition & 1 deletion app/assets/javascripts/hotwire_spark.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ var HotwireSpark = (function () {
}
}

var consumer = createConsumer();
var consumer = createConsumer("/hotwire-spark");

function assetNameFromPath(path) {
return path.split("/").pop().split(".")[0];
Expand Down
2 changes: 1 addition & 1 deletion 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.

2 changes: 1 addition & 1 deletion app/javascript/hotwire/spark/channels/consumer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { createConsumer } from "@rails/actioncable"

export default createConsumer()
export default createConsumer("/hotwire-spark")
4 changes: 4 additions & 0 deletions lib/hotwire-spark.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,9 @@ def install_into(application)
def enabled?
enabled && defined?(Rails::Server)
end

def cable_server
@server ||= Hotwire::Spark::ActionCable::Server.new
end
end
end
43 changes: 0 additions & 43 deletions lib/hotwire/spark/action_cable/persistent_cable_middleware.rb

This file was deleted.

25 changes: 0 additions & 25 deletions lib/hotwire/spark/action_cable/persistent_cable_server.rb

This file was deleted.

7 changes: 7 additions & 0 deletions lib/hotwire/spark/action_cable/server.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Hotwire::Spark::ActionCable::Server < ActionCable::Server::Base
def initialize(config: nil)
config = ::ActionCable::Server::Base.config.dup
config.connection_class = -> { ::ActionCable::Connection::Base }
super(config: config)
end
end
2 changes: 1 addition & 1 deletion lib/hotwire/spark/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Engine < ::Rails::Engine
html_paths: %w[ app/controllers app/helpers app/models app/views ],
stimulus_paths: %w[ app/javascript/controllers ]

initializer "hotwire_spark.config" do |app|
initializer "hotwire_spark.config" do |application|
config.hotwire.spark.each do |key, value|
Hotwire::Spark.send("#{key}=", value)
end
Expand Down
12 changes: 8 additions & 4 deletions lib/hotwire/spark/installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,25 @@ def initialize(application)
end

def install
configure_cable_server
configure_middleware
monitor_paths
end

def configure_middleware
::ActionCable::Server::Base.prepend(Hotwire::Spark::ActionCable::PersistentCableServer)

middleware.insert_before ActionDispatch::Executor, Hotwire::Spark::ActionCable::PersistentCableMiddleware
middleware.use Hotwire::Spark::Middleware
end

private
attr_reader :application
delegate :middleware, to: :application

def configure_cable_server
application.routes.prepend do
mount Hotwire::Spark.cable_server => "/hotwire-spark", internal: true, anchor: true
end
end

def monitor_paths
register_monitored_paths
file_watcher.start
Expand All @@ -39,7 +43,7 @@ def monitor(paths_name, action:)
end

def broadcast_reload_action(action, file_path)
ActionCable.server.broadcast "hotwire_spark", reload_message_for(action, file_path)
Hotwire::Spark.cable_server.broadcast "hotwire_spark", reload_message_for(action, file_path)
end

def reload_message_for(action, file_path)
Expand Down
7 changes: 0 additions & 7 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,11 @@

require "helpers/files_helper"

::ActionCable::Server::Base.prepend(Hotwire::Spark::ActionCable::PersistentCableServer)

class ActiveSupport::TestCase
include FilesHelper

setup do
reload_rails_reloader
ActionCable.server.suppress_restarts = true
end

teardown do
ActionCable.server.suppress_restarts = false
end

private
Expand Down

0 comments on commit dda1b6e

Please sign in to comment.