class Slinky::Listener

Public Class Methods

new(manifest, livereload) click to toggle source
# File lib/slinky/listener.rb, line 5
def initialize manifest, livereload
  @manifest = manifest
  @livereload = livereload
end

Public Instance Methods

handle_add(files) click to toggle source
# File lib/slinky/listener.rb, line 49
def handle_add files
  begin
    @manifest.add_all_by_path files
  rescue
    puts "Unable to add file: #{$!}"
  end
end
handle_mod(files) click to toggle source
# File lib/slinky/listener.rb, line 41
def handle_mod files
  begin
    @manifest.update_all_by_path files
  rescue
    puts "Unable to update file: #{$!}"
  end
end
handle_rem(files) click to toggle source
# File lib/slinky/listener.rb, line 57
def handle_rem files
  begin
    @manifest.remove_all_by_path files
  rescue
    puts "Unable to remove file: #{$1}"
  end
end
run() click to toggle source
# File lib/slinky/listener.rb, line 10
def run
  manifest_md5 = @manifest.md5
  listener = Listen.to(@manifest.dir) do |mod, add, rem|

    EM.next_tick {
      handle_mod(mod) if mod.size > 0
      handle_add(add) if add.size > 0
      handle_rem(rem) if rem.size > 0
      
      files = (mod + add + rem).map{|path|
        mpath = Pathname.new(path)\
          .relative_path_from(Pathname.new(@manifest.dir).expand_path).to_s
        mf = @manifest.find_by_path(mpath, false).first
        if mf
          mf.output_path
        else
          nil
        end
      }.compact

      # only reload if something's actually changed
      if manifest_md5 != @manifest.md5 && files.size > 0
        manifest_md5 = @manifest.md5
        @livereload.reload_browser(files)
      end
    } if @livereload
  end
  listener.start
  listener
end