Class: Listener

Inherits:
Object
  • Object
show all
Defined in:
lib/listener.rb

Instance Method Summary collapse

Constructor Details

#initialize(path = nil, &block) ⇒ Listener

Returns a new instance of Listener.



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/listener.rb', line 2

def initialize(path = nil, &block)
  require 'osx/foundation'

  OSX.require_framework '/System/Library/Frameworks/CoreServices.framework/Frameworks/CarbonCore.framework'

  callback = proc do |stream, context, count, paths, marks, events|
    yield
  end

  flags     = 0
  path      = [path || Dir.pwd]
  since     = OSX::KFSEventStreamEventIdSinceNow
  context   = nil
  latency   = 1.0
  allocator = OSX::KCFAllocatorDefault

  unless stream = OSX::FSEventStreamCreate(allocator, callback, context, path, since, latency, flags)
    puts 'Failed to create stream...'
    exit
  end

  OSX::FSEventStreamScheduleWithRunLoop(stream, OSX::CFRunLoopGetCurrent(), OSX::KCFRunLoopDefaultMode)

  unless OSX::FSEventStreamStart(stream)
    puts 'Failed to start stream...'
    exit
  end

  OSX::CFRunLoopRun()
rescue Interrupt
  OSX::FSEventStreamStop(stream)
  OSX::FSEventStreamInvalidate(stream)
  OSX::FSEventStreamRelease(stream)
end