-
-
Notifications
You must be signed in to change notification settings - Fork 10k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cleanup LiveReloadReactor #6607
Changes from 4 commits
7541820
0810a20
69eec47
aaee438
4de0137
6d90e87
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,6 @@ class LiveReloadReactor | |
attr_reader :thread | ||
|
||
def initialize | ||
@thread = nil | ||
@websockets = [] | ||
@connections_count = 0 | ||
@started_event = Utils::ThreadEvent.new | ||
|
@@ -25,29 +24,18 @@ def stop | |
# 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? | ||
EM.reactor_running? | ||
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 | ||
|
@@ -56,9 +44,7 @@ def start(opts) | |
# 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"], | ||
|
@@ -70,17 +56,11 @@ def start(opts) | |
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:", | ||
"#{opts["host"]}:#{opts["livereload_port"]}" | ||
end | ||
end | ||
@thread.abort_on_exception = true | ||
|
@@ -96,11 +76,9 @@ def reload(pages) | |
: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.dump(msg) | ||
@websockets.each { |ws| ws.send(JSON.dump(msg)) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @awood does There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should be able to store the serialized JSON and re-use it. Not sure why I didn't do that in the first place. I believe |
||
end | ||
end | ||
|
||
|
@@ -110,7 +88,7 @@ def connect(ws, handshake) | |
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( | ||
|
@@ -134,18 +112,14 @@ def print_message(json_message) | |
# 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")) | ||
raise e, "LiveReload experienced an error. " \ | ||
"Run with --trace for more information." | ||
end | ||
end | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can remove this comment: