Class: EMDirWatcher::Platform::Mac::FSEventStream

Inherits:
Object
  • Object
show all
Defined in:
lib/em-dir-watcher/platform/mac/rubycocoa_watcher.rb,
lib/em-dir-watcher/platform/mac/ffi_fsevents_watcher.rb

Defined Under Namespace

Classes: StreamError

Constant Summary collapse

KFSEventStreamEventFlagMustScanSubDirs =
0x1

Instance Method Summary collapse

Constructor Details

#initialize(paths, &block) ⇒ FSEventStream

Returns a new instance of FSEventStream.

Raises:

  • (ArgumentError)

16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/em-dir-watcher/platform/mac/rubycocoa_watcher.rb', line 16

def initialize(paths, &block)
    raise ArgumentError, 'No callback block was specified.' unless block_given?
    paths.each { |path| raise ArgumentError, "The specified path (#{path}) does not exist." unless File.exist?(path) }

    callback = Proc.new do |stream, client_callback_info, number_of_events, paths_pointer, event_flags, event_ids|
        paths_pointer.regard_as('*')
        # event_flags.regard_as('*')
        events = []
        number_of_events.times {|i|
            flags = event_flags[i]
            code = if (flags & KFSEventStreamEventFlagMustScanSubDirs) == KFSEventStreamEventFlagMustScanSubDirs then '>' else '-' end
            events << code + paths_pointer[i].to_s
        }
        block.call(events)
    end
    latency = 0.0
    flags = 0
    @stream = OSX.FSEventStreamCreate(OSX::KCFAllocatorDefault, callback, nil, paths, OSX::KFSEventStreamEventIdSinceNow, latency, flags)
    raise(StreamError, 'Unable to create FSEvents stream.') unless @stream
    OSX.FSEventStreamScheduleWithRunLoop(@stream, OSX.CFRunLoopGetCurrent, OSX::KCFRunLoopDefaultMode)
    ok = OSX.FSEventStreamStart(@stream)
    raise(StreamError, 'Unable to start FSEvents stream.') unless ok
end

Instance Method Details

#run_loopObject


40
41
42
# File 'lib/em-dir-watcher/platform/mac/rubycocoa_watcher.rb', line 40

def run_loop
    OSX.CFRunLoopRun
end

#stopObject


44
45
46
# File 'lib/em-dir-watcher/platform/mac/rubycocoa_watcher.rb', line 44

def stop
    OSX.FSEventStreamStop(@stream)
end