mata 0.11.0 → 0.12.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 886a7db4fb956cd1191b502d73b582569e3dc680ca3c375fdf42296fb5d6197b
4
- data.tar.gz: d87cd56304b9eb144d3b9a997598f09df16d94292c9724cf3312f0e9b6298411
3
+ metadata.gz: c5d3bf8207f3d65c4f2403277f9b902a5a30b51e3812d1ce6aaf34d31fb2ebf9
4
+ data.tar.gz: 1d4372dc97e20d15337535ac33480a605a17ea4feaea420592e59ece6c66beb4
5
5
  SHA512:
6
- metadata.gz: 7e82ce5b92c7ae0ee99758373be421ac03abbc0546af2ccab1f51f74584b4d13fbeaa4efbf0c8af1c3efc6e6da9f9703ccf74cae7dc46e3fb3605364c3a0d7ff
7
- data.tar.gz: 59b37d520084e2afed7d2237cf6d99d42f5a15bd690958e65f66397237131bdda23cf62e0909b3456ae5a3247a2b37a52fb2ceb2b5470206f2d76a245ebcaefc
6
+ metadata.gz: 90be02b12107f5fbe3a389c103a500a7d9784d94aacb1608a9fd71414e352b928d366023e6ec02e34da7868b52e182430f8c8fc148120ba94761bfe4fa97c60f
7
+ data.tar.gz: 2f8a07fbe3fe915ec889f13a2949cfd0e6e1cd45597c1b2d77407e1e1e1057223b87fb99ae5707f0622a8429665939a10662adadad0ab0779d7db5805476c373
data/lib/mata/agent.rb CHANGED
@@ -19,7 +19,8 @@ class Mata
19
19
  script_tag += "></script>"
20
20
  modified_content = content.sub("</head>", "#{script_tag}\n</head>")
21
21
 
22
- headers["Content-Length"] = modified_content.bytesize.to_s if headers["Content-Length"]
22
+ length_header = headers.keys.find { it.to_s.casecmp?("content-length") }
23
+ headers[length_header] = modified_content.bytesize.to_s if length_header
23
24
 
24
25
  [status, headers, [modified_content]]
25
26
  end
@@ -32,12 +33,16 @@ class Mata
32
33
  content_type&.include?("text/html")
33
34
  end
34
35
 
36
+ # The original body is replaced, so it's closed here as the Rack spec asks:
37
+ # that releases a file body and runs Rack::BodyProxy callbacks.
35
38
  def extract(body)
36
39
  if body.respond_to?(:each)
37
40
  [].tap { |parts| body.each { |part| parts << part } }.join
38
41
  else
39
42
  body.to_s
40
43
  end
44
+ ensure
45
+ body.close if body.respond_to?(:close)
41
46
  end
42
47
  end
43
48
  end
@@ -17,6 +17,8 @@ class Mata
17
17
  }
18
18
  JS
19
19
 
20
+ HEARTBEAT_INTERVAL = 15
21
+
20
22
  def initialize(options = {})
21
23
  @clients = []
22
24
  @clients_mutex = Mutex.new
@@ -75,6 +77,8 @@ class Mata
75
77
  stream << message
76
78
  rescue
77
79
  @clients_mutex.synchronize { @clients.delete(stream) }
80
+
81
+ hang_up(stream)
78
82
  end
79
83
  end
80
84
 
@@ -89,7 +93,7 @@ class Mata
89
93
  def cleanup_periodically
90
94
  Thread.new do
91
95
  loop do
92
- sleep 60
96
+ sleep HEARTBEAT_INTERVAL
93
97
 
94
98
  cleanup_dead_clients
95
99
  end
@@ -100,13 +104,22 @@ class Mata
100
104
  def cleanup_dead_clients
101
105
  @clients_mutex.synchronize do
102
106
  @clients.reject! do |client|
103
- client << ""
107
+ # an SSE comment; writing nothing never notices a closed tab
108
+ client << ":\n\n"
104
109
 
105
110
  false
106
111
  rescue
112
+ hang_up(client)
113
+
107
114
  true
108
115
  end
109
116
  end
110
117
  end
118
+
119
+ def hang_up(stream)
120
+ stream.close
121
+ rescue
122
+ nil
123
+ end
111
124
  end
112
125
  end
data/lib/mata/client.js CHANGED
@@ -16,7 +16,15 @@
16
16
  if (!silent) console.error("[Mata]", ...arguments);
17
17
  }
18
18
 
19
- connect();
19
+ const channel = "BroadcastChannel" in window ? new BroadcastChannel("mata") : null;
20
+
21
+ if (channel && navigator.locks) {
22
+ channel.onmessage = (event) => handle(event.data);
23
+
24
+ navigator.locks.request("mata", () => new Promise(() => connect()));
25
+ } else {
26
+ connect();
27
+ }
20
28
 
21
29
  function connect() {
22
30
  const eventSource = new EventSource("/__mata/events");
@@ -24,24 +32,8 @@
24
32
  eventSource.onmessage = function(event) {
25
33
  const data = JSON.parse(event.data);
26
34
 
27
- switch(data.type) {
28
- case "reload":
29
- if (data.files?.some((file) => file.endsWith(".js"))) {
30
- log("JS file changed, reloading…");
31
-
32
- window.location.reload();
33
- } else {
34
- morphPage();
35
- }
36
-
37
- break;
38
- case "connected":
39
- retryDelay = retryDelayBase;
40
-
41
- log("Connected with DOM morphing");
42
-
43
- break;
44
- }
35
+ channel?.postMessage(data);
36
+ handle(data);
45
37
  };
46
38
 
47
39
  eventSource.onerror = function() {
@@ -54,6 +46,27 @@
54
46
  };
55
47
  }
56
48
 
49
+ function handle(data) {
50
+ switch(data.type) {
51
+ case "reload":
52
+ if (data.files?.some((file) => file.endsWith(".js"))) {
53
+ log("JS file changed, reloading…");
54
+
55
+ window.location.reload();
56
+ } else {
57
+ morphPage();
58
+ }
59
+
60
+ break;
61
+ case "connected":
62
+ retryDelay = retryDelayBase;
63
+
64
+ log("Connected with DOM morphing");
65
+
66
+ break;
67
+ }
68
+ }
69
+
57
70
  async function morphPage() {
58
71
  try {
59
72
  log("Fetching updated page…");
data/lib/mata/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Mata
2
- VERSION = "0.11.0"
2
+ VERSION = "0.12.0"
3
3
  end
data/lib/mata.rb CHANGED
@@ -23,6 +23,8 @@ class Mata
23
23
  end
24
24
 
25
25
  def call(environment)
26
+ return @app.call(environment) if @agent.nil?
27
+
26
28
  request = Rack::Request.new(environment)
27
29
 
28
30
  case request.path
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mata
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.0
4
+ version: 0.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rails Designer
@@ -72,7 +72,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
72
72
  - !ruby/object:Gem::Version
73
73
  version: '0'
74
74
  requirements: []
75
- rubygems_version: 4.0.22
75
+ rubygems_version: 4.0.24
76
76
  specification_version: 4
77
77
  summary: Hot module reloading for Rack applications
78
78
  test_files: []