Permalink
Browse files

Merge pull request #5 from collin/master

integrate Listener and Turbolinks
  • Loading branch information...
2 parents e056e63 + cac5aa0 commit 0bf9e4798c99e771acc08ddc46edb63a3feaaee2 @tenderlove committed Oct 16, 2012
View
@@ -7,9 +7,12 @@ gem 'activerecord-deprecated_finders', github: 'rails/activerecord-deprecated_fi
gem 'sqlite3'
gem 'puma'
-gem 'rb-fsevent'
gem 'redis'
gem 'tusk'
+gem 'listen'
+gem 'rb-inotify' if RUBY_PLATFORM.downcase.include?("linux")
+gem 'rb-fsevent' if RUBY_PLATFORM.downcase.include?("darwin")
+gem 'turbolinks', github: 'collin/turbolinks', branch: 'patch-1'
# Gems used only for assets and not required
# in production environments by default.
View
@@ -1,4 +1,12 @@
GIT
+ remote: git://github.com/collin/turbolinks.git
+ revision: 2e8b320a7e4a7bb6faf7cf73a8fd34d0ed8d9ff3
+ branch: patch-1
+ specs:
+ turbolinks (0.5.1)
+ coffee-rails
+
+GIT
remote: git://github.com/rails/activerecord-deprecated_finders.git
revision: fe150f26f009cef370658b7c19db1629b2448952
specs:
@@ -101,6 +109,7 @@ GEM
railties (>= 3.1.0, < 5.0)
thor (~> 0.14)
json (1.7.5)
+ listen (0.5.2)
mail (2.4.4)
i18n (>= 0.4.0)
mime-types (~> 1.16)
@@ -148,6 +157,7 @@ DEPENDENCIES
coffee-rails!
journey!
jquery-rails
+ listen
puma
rack-cache (~> 1.2)
rails!
@@ -156,5 +166,6 @@ DEPENDENCIES
sass-rails!
sprockets-rails!
sqlite3
+ turbolinks!
tusk
uglifier (>= 1.0.3)
@@ -13,14 +13,31 @@
//= require jquery
//= require jquery_ujs
//= require_tree .
+//= require turbolinks
jQuery(document).ready(function() {
setTimeout(function() {
var source = new EventSource('/control');
// if we get a reload command, reload the page
source.addEventListener('reload', function(e) {
- window.location.reload();
+ Turbolinks.visit(window.location);
});
+
+ // if we get a reload-styles command, reload the css
+ source.addEventListener('reload-styles', function(e) {
+ var sheets = $("[rel=stylesheet]");
+ sheets.each(function() {
+ var sheet = $(this);
+ var clone = sheet.clone()
+ clone.appendTo(document.head);
+ clone.on('load', function() {
+ setTimeout(20, function() {
+ sheet.remove();
+ });
+ });
+ });
+ });
+
}, 1);
});
@@ -1,3 +1,7 @@
// Place all the styles related to the users controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
+body {
+ background-color: pink;
+ font-size: 20px;
+}
@@ -1,4 +1,3 @@
-require 'rb-fsevent'
require 'tusk/observable/drb'
# Start the DRb server for notifications
@@ -41,22 +40,30 @@ def index
watch_tables
# Watch the filesystem for changes
- @fsevent = FSEvent.new
paths = ['views', 'assets'].map { |d| File.join(Rails.root, 'app', d) }
- @fsevent.watch(paths) { |dir|
+ @listener = Listen::MultiListener.new(*paths)
+ @listener.latency(0.5)
+ @listener.change do |modified, added, removed|
# When something changes, send an SSE
- @sse.write({ 'changed' => dir }, :event => 'reload')
- }
- @fsevent.run
+ modified.each do |modification|
+ event = if modification =~ /stylesheets/
+ 'reload-styles'
+ else
+ 'reload'
+ end
+ @sse.write({ 'changed' => modification }, :event => event)
+ end
+ end
+ @listener.start
ensure
@sse.close
end
private
def cleanup
- logger.info "Cleaning up FSEvent"
- @fsevent.stop
+ logger.info "Cleaning up listener"
+ @listener.stop
end
TableChannel = Struct.new(:channel) do
@@ -83,8 +90,8 @@ def heartbeat
Thread.new do
begin
loop do
+ sleep 5
@sse.write({ 'ping' => Time.now }, :event => 'ping')
- sleep 1
end
rescue IOError
cleanup
View
@@ -11,6 +11,7 @@
module Lolwut
class Application < Rails::Application
+ config.middleware.delete Rack::Lock
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.

0 comments on commit 0bf9e47

Please sign in to comment.