hotwire-spark 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -20,7 +20,7 @@ export function cacheBustedUrl(urlString) {
20
20
 
21
21
  export async function reloadHtmlDocument() {
22
22
  let currentUrl = cacheBustedUrl(urlWithParams(window.location.href, { hotwire_spark: "true" }))
23
- const response = await fetch(currentUrl)
23
+ const response = await fetch(currentUrl, { headers: { "Accept": "text/html" }})
24
24
 
25
25
  if (!response.ok) {
26
26
  throw new Error(`${response.status} when fetching ${currentUrl}`)
@@ -18,18 +18,26 @@ export class StimulusReloader {
18
18
  log("Reload Stimulus controllers...")
19
19
 
20
20
  this.application.stop()
21
- await this.#reloadStimulusControllers()
21
+
22
+ await this.#reloadChangedStimulusControllers()
23
+ this.#unloadDeletedStimulusControllers()
24
+
22
25
  this.application.start()
23
26
  }
24
27
 
25
- async #reloadStimulusControllers() {
28
+ async #reloadChangedStimulusControllers() {
26
29
  await Promise.all(
27
- this.#stimulusControllerPaths.map(async moduleName => this.#reloadStimulusController(moduleName))
30
+ this.#stimulusControllerPathsToReload.map(async moduleName => this.#reloadStimulusController(moduleName))
28
31
  )
29
32
  }
30
33
 
34
+ get #stimulusControllerPathsToReload() {
35
+ this.controllerPathsToReload = this.controllerPathsToReload || this.#stimulusControllerPaths.filter(path => this.#shouldReloadController(path))
36
+ return this.controllerPathsToReload
37
+ }
38
+
31
39
  get #stimulusControllerPaths() {
32
- return Object.keys(this.#stimulusPathsByModule).filter(path => path.endsWith("_controller") && this.#shouldReloadController(path))
40
+ return Object.keys(this.#stimulusPathsByModule).filter(path => path.endsWith("_controller"))
33
41
  }
34
42
 
35
43
  #shouldReloadController(path) {
@@ -57,6 +65,22 @@ export class StimulusReloader {
57
65
  this.#registerController(controllerName, module)
58
66
  }
59
67
 
68
+ #unloadDeletedStimulusControllers() {
69
+ this.#controllersToUnload.forEach(controller => this.#deregisterController(controller.identifier))
70
+ }
71
+
72
+ get #controllersToUnload() {
73
+ if (this.#didChangeTriggerAReload) {
74
+ return []
75
+ } else {
76
+ return this.application.controllers.filter(controller => this.filePattern.test(`${controller.identifier}_controller`))
77
+ }
78
+ }
79
+
80
+ get #didChangeTriggerAReload() {
81
+ return this.#stimulusControllerPathsToReload.length > 0
82
+ }
83
+
60
84
  #pathForModuleName(moduleName) {
61
85
  return this.#stimulusPathsByModule[moduleName]
62
86
  }
@@ -70,7 +94,12 @@ export class StimulusReloader {
70
94
  }
71
95
 
72
96
  #registerController(name, module) {
73
- this.application.unload(name)
97
+ this.#deregisterController(name)
74
98
  this.application.register(name, module.default)
75
99
  }
100
+
101
+ #deregisterController(name) {
102
+ log(`\tRemoving controller ${name}`)
103
+ this.application.unload(name)
104
+ }
76
105
  }
@@ -7,7 +7,7 @@ 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?,
10
+ enabled: Rails.env.development? && defined?(Rails::Server),
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 ]
@@ -1,5 +1,5 @@
1
1
  module Hotwire
2
2
  module Spark
3
- VERSION = "0.1.4"
3
+ VERSION = "0.1.5"
4
4
  end
5
5
  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.4
4
+ version: 0.1.5
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-19 00:00:00.000000000 Z
11
+ date: 2024-12-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails