class Jekyll::Reload::Reactor

Attributes

jekyll[R]
wsc[R]

Public Class Methods

new(jekyll) click to toggle source

# File lib/jekyll/reload/reactor.rb, line 18
def initialize(jekyll)
  @jekyll, @wsc = jekyll, []
end

Public Instance Methods

paths() click to toggle source

# File lib/jekyll/reload/reactor.rb, line 68
def paths
  (jekyll.pages + jekyll.posts.docs + jekyll.documents)
    .uniq.map(&:path)
end
reload() click to toggle source

# 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?() click to toggle source

# File lib/jekyll/reload/reactor.rb, line 30
def running?
  @thread&.alive?
end
start() click to toggle source

# 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
stop() click to toggle source

# File lib/jekyll/reload/reactor.rb, line 23
def stop
  @thread&.kill
  Jekyll.logger.debug("Reloader:",
    "stopped")
end

Private Instance Methods

connect(ws, v) click to toggle source

# File lib/jekyll/reload/reactor.rb, line 75
def connect(ws, v)
  msg = +"Browser Connected".green
  msg << " from origin #{v.headers['Origin'].yellow}"
  Jekyll.logger.debug("Reloader: ", msg)

  ws.send(JSON.dump({
    serverName: "Jekyll Reload v#{VERSION}",
    command: "hello",
    protocols: [
      "http://livereload.com/protocols/official-7",
    ],
  }))

  @wsc << ws
end