Class: Jekyll::Reload::Reactor

Inherits:
Object
  • Object
show all
Extended by:
Forwardable::Extended
Defined in:
lib/jekyll/reload/reactor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(jekyll) ⇒ Reactor



18
19
20
# File 'lib/jekyll/reload/reactor.rb', line 18

def initialize(jekyll)
  @jekyll, @wsc = jekyll, []
end

Instance Attribute Details

#jekyllObject (readonly)

Returns the value of attribute jekyll.



11
12
13
# File 'lib/jekyll/reload/reactor.rb', line 11

def jekyll
  @jekyll
end

#wscObject (readonly)

Returns the value of attribute wsc.



11
12
13
# File 'lib/jekyll/reload/reactor.rb', line 11

def wsc
  @wsc
end

Instance Method Details

#pathsObject



68
69
70
71
# File 'lib/jekyll/reload/reactor.rb', line 68

def paths
  (jekyll.pages + jekyll.posts.docs + jekyll.documents)
    .uniq.map(&:path)
end

#reloadObject



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/jekyll/reload/reactor.rb', line 54

def reload
  Jekyll.logger.debug("Reloader: ", "reloaded at #{Time.now}")
  paths.map do |v|
    @wsc.each do |sv|
      sv.send(JSON.dump({
        liveCSS: true,
        command: "reload",
        path: v,
      }))
    end
  end
end

#running?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/jekyll/reload/reactor.rb', line 30

def running?
  @thread&.alive?
end

#startObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/jekyll/reload/reactor.rb', line 35

def start
  Thread.abort_on_exception = true
  @thread = Thread.new do
    EventMachine.run do
      o = {
        jekyll: jekyll,
      }

      a = jekyll.config["reloader"].values_at("host", "port")
      Jekyll.logger.info("Reloader at: ", "http://#{a[0]}:#{a[1]}")
      EventMachine.start_server(a[0], a[1], Server, o) do |w|
        w.onclose   { |_| disconnect(w) }
        w.onopen    { |v| connect(w, v) }
      end
    end
  end
end

#stopObject



23
24
25
26
27
# File 'lib/jekyll/reload/reactor.rb', line 23

def stop
  @thread&.kill
  Jekyll.logger.debug("Reloader:",
    "stopped")
end