Skip to content

Commit

Permalink
Merge pull request #1 from faustinoaq/self
Browse files Browse the repository at this point in the history
Add self methods
  • Loading branch information
faustinoaq authored Apr 1, 2017
2 parents b76d08c + 2ddcd3f commit 3f182d7
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 66 deletions.
88 changes: 24 additions & 64 deletions src/kemal-watcher.cr
Original file line number Diff line number Diff line change
@@ -1,31 +1,25 @@
require "./kemal-watcher/*"
require "secure_random"
require "kemal"
require "watcher"
require "secure_random"

# Uses Watcher.watch shard to guard files
private def watcher(files)
puts " Your KemalBot is vigilant. beep-boop..."
spawn do
Watcher.watch files do |event|
event.on_change do |files|
files.each do |file, time|
puts " watching file: #{file}"
end
Kemal.handle_change
end
end
end
end
require "./kemal-watcher/*"

module Kemal
SOCKETS = [] of HTTP::WebSocket
WEBSOCKETPATH = SecureRandom.hex 4

# Handle change when event.on_change and
# send reload message to all clients
def self.handle_change
SOCKETS.each do |socket|
socket.send "reload"
# Uses Watcher.watch shard to guard files
def self.watcher(files)
spawn do
puts " Your KemalBot is vigilant. beep-boop..."
watch files do |event|
event.on_change do |files|
files.each do |file, time|
puts " watching file: #{file}"
end
handle_change
end
end
end
end

Expand All @@ -34,53 +28,19 @@ module Kemal
if Kemal.config.env == "production" || ENV["KEMAL_ENV"]? == "production"
puts "Kemal.watch is intended for use in a development environment."
end
watcher files
# add_handler WatcherHandler.new
websocket_server
filter_handler
watcher files
end

# Start WebSocket server
ws "/" + WEBSOCKETPATH do |socket|
SOCKETS << socket
socket.on_close do
SOCKETS.delete socket
end
end

# Instead of Kemal::Handler I'm using after_get
# to check content_type == "text/html"
# However, http://kemalcr.com/docs/filters/ says:
# Important note: This should not be used by
# plugins/addons, instead they should do all their
# work in their own middleware.
# In future releases I could improve the WatcherHandler
# but for now it works.
after_get do |env|
if env.response.headers["Content-Type"] == "text/html"
env.response.print <<-HTML
\n<!-- Code injected by kemal-watcher -->
<script type="text/javascript">
// <![CDATA[ <-- For SVG support
if ('WebSocket' in window) {
(() => {
var ws = new WebSocket("ws://" + location.host + "/#{WEBSOCKETPATH}");
ws.onopen = () => {
console.log("Connected to kemal-watcher");
};
ws.onmessage = (msg) => {
if (msg.data == "reload") {
window.location.reload();
}
};
ws.onclose = () => {
setTimeout(() => {
window.location.reload();
}, 2000);
};
})();
}
// ]]>
</script>\n
HTML
def self.websocket_server
ws "/" + WEBSOCKETPATH do |socket|
SOCKETS << socket
socket.on_close do
SOCKETS.delete socket
end
end
end
end
50 changes: 48 additions & 2 deletions src/kemal-watcher/watcher_handler.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require "kemal"

module Kemal
# The right way to handle context is Kemal::Handler
# but I can not check response content type easily
Expand All @@ -9,4 +7,52 @@ module Kemal
# call_next context
# end
# end

# Handle change when event.on_change and
# send reload message to all clients
def self.handle_change
SOCKETS.each do |socket|
socket.send "reload"
end
end

# Instead of Kemal::Handler I'm using a filter handler
# to check content_type == "text/html"
# However, http://kemalcr.com/docs/filters/ says:
# Important note: This should not be used by
# plugins/addons, instead they should do all their
# work in their own middleware.
# In future releases I could improve the WatcherHandler
# but for now it works.
def self.filter_handler
after_get do |env|
if env.response.headers["Content-Type"] == "text/html"
env.response.print <<-HTML
\n<!-- Code injected by kemal-watcher -->
<script type="text/javascript">
// <![CDATA[ <-- For SVG support
if ('WebSocket' in window) {
(() => {
var ws = new WebSocket("ws://" + location.host + "/#{WEBSOCKETPATH}");
ws.onopen = () => {
console.log("Connected to kemal-watcher");
};
ws.onmessage = (msg) => {
if (msg.data == "reload") {
window.location.reload();
}
};
ws.onclose = () => {
setTimeout(() => {
window.location.reload();
}, 2000);
};
})();
}
// ]]>
</script>\n
HTML
end
end
end
end

0 comments on commit 3f182d7

Please sign in to comment.