Class: Aniview::Util::FolderListen::Listener

Inherits:
Object
  • Object
show all
Defined in:
lib/aniview/util/folder_listen.rb

Instance Method Summary collapse

Constructor Details

#initialize(dirs, &block) ⇒ Listener

Returns a new instance of Listener.



18
19
20
21
22
23
# File 'lib/aniview/util/folder_listen.rb', line 18

def initialize(dirs, &block)
  @dirs           = dirs
  @block          = block
  @sleep_duration = 1
  @dir_hash       = dir_hash
end

Instance Method Details

#changed?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/aniview/util/folder_listen.rb', line 29

def changed?
  @dir_hash != dir_hash
end

#dir_hashObject



25
26
27
# File 'lib/aniview/util/folder_listen.rb', line 25

def dir_hash
  @dirs.map { |dir| [dir, File.exist?(dir)]}.to_h
end

#startObject



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/aniview/util/folder_listen.rb', line 37

def start
  Thread.abort_on_exception = true
  @listen_thread = Thread.new do
    while true
      if changed?
        @block.call
        @dir_hash = dir_hash
      end
      sleep 1
    end
  end
end

#stopObject



33
34
35
# File 'lib/aniview/util/folder_listen.rb', line 33

def stop
  @listen_thread.exit if @listen_thread
end