hotwire-spark 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
  }
@@ -73,4 +97,9 @@ export class StimulusReloader {
73
97
  this.application.unload(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
  }
@@ -1,5 +1,5 @@
1
1
  module Hotwire
2
2
  module Spark
3
- VERSION = "0.1.6"
3
+ VERSION = "0.1.7"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hotwire-spark
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jorge Manrubia