Module: EMDirWatcher

Defined in:
lib/em-dir-watcher.rb,
lib/em-dir-watcher/tree.rb,
lib/em-dir-watcher/monitor.rb,
lib/em-dir-watcher/platform/mac.rb,
lib/em-dir-watcher/platform/linux.rb,
lib/em-dir-watcher/platform/windows.rb,
lib/em-dir-watcher/invokers/subprocess_invoker.rb,
lib/em-dir-watcher/platform/mac/rubycocoa_watcher.rb,
lib/em-dir-watcher/platform/mac/ffi_fsevents_watcher.rb,
lib/em-dir-watcher/platform/windows/path_to_ruby_exe.rb

Defined Under Namespace

Modules: Invokers, Platform Classes: Entry, NameGlobMatcher, PathGlobMatcher, RegexpMatcher, Tree

Constant Summary collapse

PLATFORM =
ENV['EM_DIR_WATCHER_PLATFORM'] ||
case Config::CONFIG['target_os']
    when /mswin|mingw/ then 'Windows'
    when /darwin/      then 'Mac'
    when /linux/       then 'Linux'
    else                    'NIX'
end
Watcher =
Platform.const_get(PLATFORM)::Watcher
DEFAULT_OPTIONS =
{
    :exclude => [],
    :include_only => nil,
    :grace_period => 0.0,
}.freeze
INFINITELY_SMALL_PERIOD =
0.001

Class Method Summary collapse

Class Method Details

.watch(path, options = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/em-dir-watcher/monitor.rb', line 14

def self.watch path, options={}
    unless (invalid_keys = options.keys - DEFAULT_OPTIONS.keys).empty?
        raise StandardError, "Unsupported options given to EMDirWatcher.watch: " + invalid_keys.join(", ")
    end
    options = DEFAULT_OPTIONS.merge(options)
    grace_period = options[:grace_period]

    tree = Tree.new path, options[:include_only], options[:exclude]

    pending_refresh_requests = Set.new
    process_pending_refresh_requests_scheduled = false

    process_pending_refresh_requests = lambda do
        process_pending_refresh_requests_scheduled = false
        changed_paths = Set.new
        pending_refresh_requests.each do |change_scope, refresh_subtree|
            changed_paths += tree.refresh! change_scope, refresh_subtree
        end
        yield changed_paths.to_a unless changed_paths.empty?
    end

    Watcher.new path, options[:include_only], options[:exclude] do |change_scope, refresh_subtree|
        pending_refresh_requests << [change_scope, refresh_subtree]
        if grace_period <= INFINITELY_SMALL_PERIOD
            process_pending_refresh_requests.call
        else
            unless process_pending_refresh_requests_scheduled
                EM.add_timer grace_period, &process_pending_refresh_requests
                process_pending_refresh_requests_scheduled = true
            end
        end
    end
end