rack-reloader 0.0.1 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rack/reloader.rb +62 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 58e0698cc207984a6bd5fefa58ac5232b6466f05
|
4
|
+
data.tar.gz: 58b1a2849d4bb8e72278c496e23bd8298b88343d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 62cec5ffdc63f4465b2c110fa2ddb901c2854371949bac6689b7ea417b05c163f0c07314d44c12b3eddc7614eb1a86f619784876ad3a387959db443ad86b44bb
|
7
|
+
data.tar.gz: 380137e43009d5cb77fc0b0bd0af72566c0e0defbb3fd30803571e5fcc7a395f6a042f302fbdb79d69bc74efb3fb7d51b7a031c0b827dbcabb1082be7332c1f9
|
data/lib/rack/reloader.rb
CHANGED
@@ -1,5 +1,67 @@
|
|
1
1
|
module Rack
|
2
2
|
class Reloader
|
3
|
+
class Config
|
4
|
+
attr_accessor :interval, :ajax
|
3
5
|
|
6
|
+
def initialize
|
7
|
+
@interval = 5000
|
8
|
+
@ajax = false
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize(app)
|
13
|
+
@app = app
|
14
|
+
@config = Config.new
|
15
|
+
@mark = rand(Time.now.to_i)
|
16
|
+
yield(@config) if block_given?
|
17
|
+
end
|
18
|
+
|
19
|
+
def call(env)
|
20
|
+
status, headers, body = @app.call(env)
|
21
|
+
[status, headers, inject_script(body)]
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def inject_script(body)
|
27
|
+
interval = @config.interval
|
28
|
+
body = body.join("\n")
|
29
|
+
template_name = @config.ajax ? :ajax_template : :template
|
30
|
+
injection = send(template_name, interval)
|
31
|
+
[body.gsub!(/<\/body>/, injection)]
|
32
|
+
end
|
33
|
+
|
34
|
+
def template(interval)
|
35
|
+
<<-HTML
|
36
|
+
</body>
|
37
|
+
<script>
|
38
|
+
var reloader_#{@mark} = setTimeout(() => { location.reload(); }, #{interval});
|
39
|
+
</script>
|
40
|
+
HTML
|
41
|
+
end
|
42
|
+
|
43
|
+
def ajax_template(interval)
|
44
|
+
<<-HTML
|
45
|
+
</body>
|
46
|
+
<script>
|
47
|
+
var reloadBody_#{@mark} = function() {
|
48
|
+
var url = window.location.href;
|
49
|
+
var xhttp = new XMLHttpRequest();
|
50
|
+
xhttp.onreadystatechange = function() {
|
51
|
+
if (this.readyState == 4 && this.status == 200) {
|
52
|
+
let bodyTag = document.getElementsByTagName("body")[0];
|
53
|
+
var parser = new DOMParser();
|
54
|
+
var remoteDocument = parser.parseFromString(this.responseText, 'text/html');
|
55
|
+
let remoteBodyTag = remoteDocument.getElementsByTagName("body")[0];
|
56
|
+
bodyTag.innerHTML = remoteBodyTag.innerHTML;
|
57
|
+
}
|
58
|
+
};
|
59
|
+
xhttp.open("GET", url, true);
|
60
|
+
xhttp.send();
|
61
|
+
}
|
62
|
+
var reloader_#{@mark} = setInterval(() => { reloadBody_#{@mark}(); }, #{interval});
|
63
|
+
</script>
|
64
|
+
HTML
|
65
|
+
end
|
4
66
|
end
|
5
67
|
end
|