hotwire-spark 0.1.7 → 0.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,3 @@
1
1
  import { createConsumer } from "@rails/actioncable"
2
2
 
3
- export default createConsumer()
3
+ export default createConsumer("/hotwire-spark")
@@ -1,4 +1,3 @@
1
- import { Application } from "@hotwired/stimulus"
2
1
  import { log } from "../logger.js"
3
2
  import { cacheBustedUrl, reloadHtmlDocument } from "../helpers.js"
4
3
 
@@ -11,7 +10,7 @@ export class StimulusReloader {
11
10
  constructor(document, filePattern = /./) {
12
11
  this.document = document
13
12
  this.filePattern = filePattern
14
- this.application = window.Stimulus || Application.start()
13
+ this.application = window.Stimulus
15
14
  }
16
15
 
17
16
  async reload() {
@@ -0,0 +1,7 @@
1
+ class Hotwire::Spark::ActionCable::Server < ActionCable::Server::Base
2
+ def initialize
3
+ config = ::ActionCable::Server::Base.config.dup
4
+ config.connection_class = -> { ::ActionCable::Connection::Base }
5
+ super(config: config)
6
+ end
7
+ end
@@ -7,17 +7,21 @@ module Hotwire::Spark
7
7
  config.hotwire = ActiveSupport::OrderedOptions.new unless config.respond_to?(:hotwire)
8
8
  config.hotwire.spark = ActiveSupport::OrderedOptions.new
9
9
  config.hotwire.spark.merge! \
10
- enabled: Rails.env.development? && defined?(Rails::Server),
10
+ enabled: Rails.env.development?,
11
11
  css_paths: File.directory?("app/assets/builds") ? %w[ app/assets/builds ] : %w[ app/assets/stylesheets ],
12
12
  html_paths: %w[ app/controllers app/helpers app/models app/views ],
13
13
  stimulus_paths: %w[ app/javascript/controllers ]
14
14
 
15
- initializer "hotwire_spark.config" do |app|
15
+ initializer "hotwire_spark.config" do |application|
16
16
  config.hotwire.spark.each do |key, value|
17
17
  Hotwire::Spark.send("#{key}=", value)
18
18
  end
19
19
  end
20
20
 
21
+ initializer "hotwire_spark.assets" do |application|
22
+ application.config.assets.precompile << "hotwire_spark.js"
23
+ end
24
+
21
25
  initializer "hotwire_spark.install" do |application|
22
26
  Hotwire::Spark.install_into application if Hotwire::Spark.enabled?
23
27
  end
@@ -1,26 +1,28 @@
1
1
  class Hotwire::Spark::Installer
2
- attr_reader :file_watcher
3
-
4
2
  def initialize(application)
5
3
  @application = application
6
4
  end
7
5
 
8
6
  def install
7
+ configure_cable_server
9
8
  configure_middleware
10
9
  monitor_paths
11
10
  end
12
11
 
13
- def configure_middleware
14
- ::ActionCable::Server::Base.prepend(Hotwire::Spark::ActionCable::PersistentCableServer)
15
-
16
- middleware.insert_before ActionDispatch::Executor, Hotwire::Spark::ActionCable::PersistentCableMiddleware
17
- middleware.use Hotwire::Spark::Middleware
18
- end
19
-
20
12
  private
21
13
  attr_reader :application
22
14
  delegate :middleware, to: :application
23
15
 
16
+ def configure_cable_server
17
+ application.routes.prepend do
18
+ mount Hotwire::Spark.cable_server => "/hotwire-spark", internal: true, anchor: true
19
+ end
20
+ end
21
+
22
+ def configure_middleware
23
+ middleware.use Hotwire::Spark::Middleware
24
+ end
25
+
24
26
  def monitor_paths
25
27
  register_monitored_paths
26
28
  file_watcher.start
@@ -39,7 +41,7 @@ class Hotwire::Spark::Installer
39
41
  end
40
42
 
41
43
  def broadcast_reload_action(action, file_path)
42
- ActionCable.server.broadcast "hotwire_spark", reload_message_for(action, file_path)
44
+ Hotwire::Spark.cable_server.broadcast "hotwire_spark", reload_message_for(action, file_path)
43
45
  end
44
46
 
45
47
  def reload_message_for(action, file_path)
@@ -1,5 +1,5 @@
1
1
  module Hotwire
2
2
  module Spark
3
- VERSION = "0.1.7"
3
+ VERSION = "0.1.9"
4
4
  end
5
5
  end
data/lib/hotwire-spark.rb CHANGED
@@ -21,7 +21,11 @@ module Hotwire::Spark
21
21
  end
22
22
 
23
23
  def enabled?
24
- enabled
24
+ enabled && defined?(Rails::Server)
25
+ end
26
+
27
+ def cable_server
28
+ @server ||= Hotwire::Spark::ActionCable::Server.new
25
29
  end
26
30
  end
27
31
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hotwire-spark
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jorge Manrubia
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-12-20 00:00:00.000000000 Z
11
+ date: 2024-12-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -135,21 +135,20 @@ files:
135
135
  - app/javascript/hotwire/spark/reloaders/stimulus_reloader.js
136
136
  - config/routes.rb
137
137
  - lib/hotwire-spark.rb
138
- - lib/hotwire/spark/action_cable/persistent_cable_middleware.rb
139
- - lib/hotwire/spark/action_cable/persistent_cable_server.rb
138
+ - lib/hotwire/spark/action_cable/server.rb
140
139
  - lib/hotwire/spark/engine.rb
141
140
  - lib/hotwire/spark/file_watcher.rb
142
141
  - lib/hotwire/spark/installer.rb
143
142
  - lib/hotwire/spark/middleware.rb
144
143
  - lib/hotwire/spark/version.rb
145
144
  - lib/tasks/hotwire_spark_tasks.rake
146
- homepage: https://github.com/basecamp/hotwire_spark
145
+ homepage: https://github.com/hotwired/spark
147
146
  licenses:
148
147
  - MIT
149
148
  metadata:
150
- homepage_uri: https://github.com/basecamp/hotwire_spark
151
- source_code_uri: https://github.com/basecamp/hotwire_spark
152
- changelog_uri: https://github.com/basecamp/hotwire_spark
149
+ homepage_uri: https://github.com/hotwired/spark
150
+ source_code_uri: https://github.com/hotwired/spark
151
+ changelog_uri: https://github.com/hotwired/spark
153
152
  post_install_message:
154
153
  rdoc_options: []
155
154
  require_paths:
@@ -1,43 +0,0 @@
1
- class Hotwire::Spark::ActionCable::PersistentCableMiddleware
2
- def initialize(app)
3
- @app = app
4
- end
5
-
6
- def call(env)
7
- request = Rack::Request.new(env)
8
-
9
- if supress_action_cable_restarts?(request)
10
- respond_suppressing_action_cable_restarts(env)
11
- else
12
- @app.call(env)
13
- end
14
- end
15
-
16
- private
17
- COOKIE_NAME = "hotwire_spark_disable_cable_restarts"
18
- RESTARTS_SUPPRESSED_GRACE_PERIOD = 10.seconds
19
-
20
- def supress_action_cable_restarts?(request)
21
- request.params["hotwire_spark"] || request.cookies[COOKIE_NAME]
22
- end
23
-
24
- def respond_suppressing_action_cable_restarts(env)
25
- status, headers, body = suppressing_action_cable_restarts { @app.call(env) }
26
- headers["Set-Cookie"] = append_cookie_to_disable_cable_restarts(headers["Set-Cookie"])
27
-
28
- [ status, headers, body ]
29
- end
30
-
31
- def suppressing_action_cable_restarts(&block)
32
- ActionCable.server.without_restarting(&block)
33
- end
34
-
35
- def append_cookie_to_disable_cable_restarts(existing_cookies)
36
- [ existing_cookies, cookie_to_disable_cable_restarts ].compact
37
- end
38
-
39
- def cookie_to_disable_cable_restarts
40
- expiration = RESTARTS_SUPPRESSED_GRACE_PERIOD.from_now.utc
41
- "#{COOKIE_NAME}=true; Path=/; Expires=#{expiration.httpdate}; HttpOnly"
42
- end
43
- end
@@ -1,25 +0,0 @@
1
- module Hotwire::Spark::ActionCable::PersistentCableServer
2
- def self.prepended(base)
3
- base.class_eval do
4
- thread_mattr_accessor :suppress_restarts
5
- end
6
- end
7
-
8
- def restart
9
- return if restarts_suppressed?
10
-
11
- super
12
- end
13
-
14
- def without_restarting
15
- old_suppress_restarts, self.suppress_restarts = self.suppress_restarts, true
16
- yield
17
- ensure
18
- self.suppress_restarts = old_suppress_restarts
19
- end
20
-
21
- private
22
- def restarts_suppressed?
23
- suppress_restarts
24
- end
25
- end