class FChange::Notifier
notifier.run
# Nothing happens until you run the notifier!
end
puts “foo was accessed!”
notifier.watch(“path/to/foo/”, :all_events) do
# Run this callback whenever the file path/to/foo.txt is read
notifier = FChange::Notifier.new
# Create the notifier
@example
but usually unnecessary.
It’s possible to have more than one instance,
Notifier wraps a single instance of FChange.
def add_watcher(watcher)
def add_watcher(watcher) @watchers[watcher.id] = watcher @dwChangeHandles.push watcher.id # Pack event handles into newly created storage area # to be used for Win32 call @lp_dwChangeHandles = dwChangeHandles.pack("L" * dwChangeHandles.count) end
def close
def close end
def initialize
-
(Notifier)
-
def initialize @watchers = {} @dwChangeHandles = [] @lp_dwChangeHandles = 0 end
def process
- See: #run -
def process read_events.each {|event| event.callback!} end
def read_events
- Private: -
def read_events # can return WAIT_TIMEOUT = 0x00000102 dwWaitStatus = Native.WaitForMultipleObjects(@dwChangeHandles.count, @lp_dwChangeHandles, 0, 500) events = [] # this call blocks all threads completely. @dwChangeHandles.each_index do |index| if dwWaitStatus == WAIT_OBJECT_0 + index ev = Event.new(@watchers[@dwChangeHandles[index]]) events << ev r = Native.FindNextChangeNotification(@dwChangeHandles[index]) if r == 0 raise SystemCallError.new("Failed to watch", r) end end end events end
def run
- See: #process -
def run @stop = false process until @stop end
def stop
That is, if we're in a \{#run} loop,
Stop watching for filesystem events.
def stop @stop = true end
def watch(path, *flags, &callback)
-
(SystemCallError)
- if the file or directory can't be watched,
Returns:
-
(Watcher)
- A Watcher set up to watch this path for these events
Other tags:
- Yieldparam: event - The Event object containing information
Other tags:
- Yield: - A block that will be called
Parameters:
-
flags
(Array
) -- Which events to watch for -
path
(String
) -- The path to the file or directory
def watch(path, *flags, &callback) recursive = flags.include?(:recursive) #:latency = 0.5 flags = flags - [:recursive] if flags.empty? @flags = [:all_events] else @flags = flags.freeze end Watcher.new(self, path, recursive, *@flags, &callback) end