Sha256: bba76552da649ba266d98448563d70715af0d3b30356aa9407246993d0489151

Contents?: true

Size: 1.5 KB

Versions: 2

Compression:

Stored size: 1.5 KB

Contents

Thread.abort_on_exception = true

module Slinky
  class Listener
    def initialize manifest, livereload
      @manifest = manifest
      @livereload = livereload
    end

    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
              path
            end
          }

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

    def handle_mod files
      begin
        @manifest.update_all_by_path files
      rescue
        puts "Unable to update file: #{$!}"
      end
    end

    def handle_add files
      begin
        @manifest.add_all_by_path files
      rescue
        puts "Unable to add file: #{$!}"
      end
    end

    def handle_rem files
      begin
        @manifest.remove_all_by_path files
      rescue
        puts "Unable to remove file: #{$1}"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
slinky-0.8.0 lib/slinky/listener.rb
slinky-0.7.3 lib/slinky/listener.rb