Skip to content
Snippets Groups Projects
Commit acb82c92 authored by ashmaroli's avatar ashmaroli Committed by jekyllbot
Browse files

Cleanup LiveReloadReactor (#6607)

Merge pull request 6607
parent 71e3bce6
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -14,7 +14,6 @@ module Jekyll
attr_reader :thread
 
def initialize
@thread = nil
@websockets = []
@connections_count = 0
@started_event = Utils::ThreadEvent.new
Loading
Loading
@@ -25,7 +24,7 @@ module Jekyll
# There is only one EventMachine instance per Ruby process so stopping
# it here will stop the reactor thread we have running.
EM.stop if EM.reactor_running?
Jekyll.logger.debug("LiveReload Server:", "halted")
Jekyll.logger.debug "LiveReload Server:", "halted"
end
 
def running?
Loading
Loading
@@ -33,32 +32,18 @@ module Jekyll
end
 
def handle_websockets_event(ws)
ws.onopen do |handshake|
connect(ws, handshake)
end
ws.onclose do
disconnect(ws)
end
ws.onmessage do |msg|
print_message(msg)
end
ws.onerror do |error|
log_error(error)
end
ws.onopen { |handshake| connect(ws, handshake) }
ws.onclose { disconnect(ws) }
ws.onmessage { |msg| print_message(msg) }
ws.onerror { |error| log_error(error) }
end
 
# rubocop:disable Metrics/MethodLength
def start(opts)
@thread = Thread.new do
# Use epoll if the kernel supports it
EM.epoll
EM.run do
EM.error_handler do |e|
log_error(e)
end
EM.error_handler { |e| log_error(e) }
 
EM.start_server(
opts["host"],
Loading
Loading
@@ -70,17 +55,11 @@ module Jekyll
end
 
# Notify blocked threads that EventMachine has started or shutdown
EM.schedule do
@started_event.set
end
EM.add_shutdown_hook do
@stopped_event.set
end
EM.schedule { @started_event.set }
EM.add_shutdown_hook { @stopped_event.set }
 
Jekyll.logger.info(
"LiveReload address:", "#{opts["host"]}:#{opts["livereload_port"]}"
)
Jekyll.logger.info "LiveReload address:",
"http://#{opts["host"]}:#{opts["livereload_port"]}"
end
end
@thread.abort_on_exception = true
Loading
Loading
@@ -90,17 +69,15 @@ module Jekyll
# http://feedback.livereload.com/knowledgebase/articles/86174-livereload-protocol
def reload(pages)
pages.each do |p|
msg = {
json_message = JSON.dump({
:command => "reload",
:path => p.url,
:liveCSS => true,
}
})
 
Jekyll.logger.debug("LiveReload:", "Reloading #{p.url}")
Jekyll.logger.debug(JSON.dump(msg))
@websockets.each do |ws|
ws.send(JSON.dump(msg))
end
Jekyll.logger.debug "LiveReload:", "Reloading #{p.url}"
Jekyll.logger.debug "", json_message
@websockets.each { |ws| ws.send(json_message) }
end
end
 
Loading
Loading
@@ -110,7 +87,7 @@ module Jekyll
if @connections_count == 1
message = "Browser connected"
message += " over SSL/TLS" if handshake.secure?
Jekyll.logger.info("LiveReload:", message)
Jekyll.logger.info "LiveReload:", message
end
ws.send(
JSON.dump(
Loading
Loading
@@ -134,18 +111,15 @@ module Jekyll
# Not sure what the 'url' command even does in LiveReload. The spec is silent
# on its purpose.
if msg["command"] == "url"
Jekyll.logger.info("LiveReload:", "Browser URL: #{msg["url"]}")
Jekyll.logger.info "LiveReload:", "Browser URL: #{msg["url"]}"
end
end
 
private
def log_error(e)
Jekyll.logger.warn(
"LiveReload experienced an error. "\
"Run with --verbose for more information."
)
Jekyll.logger.debug("LiveReload Error:", e.message)
Jekyll.logger.debug("LiveReload Error:", e.backtrace.join("\n"))
Jekyll.logger.error "LiveReload experienced an error. " \
"Run with --trace for more information."
raise e
end
end
end
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment