Class: Staticz::Modules::Reload

Inherits:
Object
  • Object
show all
Defined in:
lib/modules/reload.rb

Class Method Summary collapse

Class Method Details

.build_reload_jsObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/modules/reload.rb', line 10

def self.build_reload_js
  <<~JS
    var hash = null;

    function checkForChanges() {
      console.log("Check for file changes");
      var response = new XMLHttpRequest();
      response.open("GET", "/api/hash", true);
      response.onload = function() {
        if (response.status === 200) {
          if (!hash) {
            console.log("Set initial hash")
            hash = response.responseText
          }
          if (response.responseText !== hash) {
            location.reload();
          }
        }
      }
      response.send(null);
    }
    checkForChanges()
    setInterval(checkForChanges, 1000);
  JS
end

.generate_hashObject



36
37
38
# File 'lib/modules/reload.rb', line 36

def self.generate_hash
  @@hash = SecureRandom.uuid[0..6]
end

.hashObject



6
7
8
# File 'lib/modules/reload.rb', line 6

def self.hash
  @@hash
end