Class: FSEvents::Stream

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

Defined Under Namespace

Classes: StreamError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*paths, &callback) ⇒ Stream

Returns a new instance of Stream.

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/fsevents/stream.rb', line 10

def initialize(*paths, &callback)
  raise ArgumentError, 'A callback block is required' if callback.nil?
  @callback = callback
  
  options = {}
  options = paths.pop if paths.last.is_a?(Hash)
  
  paths = Dir.pwd if paths.empty?
  
  @allocator = options[:allocator] || OSX::KCFAllocatorDefault
  @context   = options[:context]   || nil
  @paths     = [paths].flatten
  @since     = options[:since]     || OSX::KFSEventStreamEventIdSinceNow
  @latency   = options[:latency]   || 1.0
  @flags     = options[:flags]     || 0
end

Instance Attribute Details

#allocatorObject (readonly)

Returns the value of attribute allocator.



6
7
8
# File 'lib/fsevents/stream.rb', line 6

def allocator
  @allocator
end

#callbackObject (readonly)

Returns the value of attribute callback.



6
7
8
# File 'lib/fsevents/stream.rb', line 6

def callback
  @callback
end

#contextObject (readonly)

Returns the value of attribute context.



6
7
8
# File 'lib/fsevents/stream.rb', line 6

def context
  @context
end

#flagsObject (readonly)

Returns the value of attribute flags.



6
7
8
# File 'lib/fsevents/stream.rb', line 6

def flags
  @flags
end

#last_eventObject (readonly)

Returns the value of attribute last_event.



5
6
7
# File 'lib/fsevents/stream.rb', line 5

def last_event
  @last_event
end

#latencyObject (readonly)

Returns the value of attribute latency.



6
7
8
# File 'lib/fsevents/stream.rb', line 6

def latency
  @latency
end

#pathsObject (readonly)

Returns the value of attribute paths.



6
7
8
# File 'lib/fsevents/stream.rb', line 6

def paths
  @paths
end

#sinceObject (readonly)

Returns the value of attribute since.



6
7
8
# File 'lib/fsevents/stream.rb', line 6

def since
  @since
end

#streamObject (readonly)

Returns the value of attribute stream.



5
6
7
# File 'lib/fsevents/stream.rb', line 5

def stream
  @stream
end

Class Method Details

.create(*args, &block) ⇒ Object



64
65
66
67
68
# File 'lib/fsevents/stream.rb', line 64

def create(*args, &block)
  stream = new(*args, &block)
  stream.create
  stream
end

.watch(*args, &block) ⇒ Object



70
71
72
73
74
# File 'lib/fsevents/stream.rb', line 70

def watch(*args, &block)
  stream = create(*args, &block)
  stream.startup
  stream
end

Instance Method Details

#createObject

Raises:



27
28
29
30
# File 'lib/fsevents/stream.rb', line 27

def create
  @stream = OSX.FSEventStreamCreate(allocator, stream_callback, context, paths, since, latency, flags)
  raise StreamError, 'Unable to create FSEvents stream.' unless @stream
end

#invalidateObject



81
82
83
# File 'lib/fsevents/stream.rb', line 81

def invalidate
  OSX.FSEventStreamInvalidate(stream)
end

#releaseObject



85
86
87
88
# File 'lib/fsevents/stream.rb', line 85

def release
  OSX.FSEventStreamRelease(stream)
  @stream = nil
end

#runObject



96
97
98
# File 'lib/fsevents/stream.rb', line 96

def run
  OSX.CFRunLoopRun
end

#scheduleObject



45
46
47
# File 'lib/fsevents/stream.rb', line 45

def schedule
  OSX.FSEventStreamScheduleWithRunLoop(stream, OSX.CFRunLoopGetCurrent, OSX::KCFRunLoopDefaultMode)
end

#shutdownObject



90
91
92
93
94
# File 'lib/fsevents/stream.rb', line 90

def shutdown
  stop
  invalidate
  release
end

#startObject



49
50
51
52
# File 'lib/fsevents/stream.rb', line 49

def start
  OSX.FSEventStreamStart(stream) or raise StreamError, 'Could not start stream'
  update_last_event
end

#startupObject



58
59
60
61
# File 'lib/fsevents/stream.rb', line 58

def startup
  schedule
  start
end

#stopObject



77
78
79
# File 'lib/fsevents/stream.rb', line 77

def stop
  OSX.FSEventStreamStop(stream)
end

#stream_callbackObject



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fsevents/stream.rb', line 32

def stream_callback
  lambda do |stream, context, event_count, paths, event_flags, event_IDs|
    paths.regard_as('*')
    
    events = []
    event_count.times { |i|  events << Event.new(event_IDs[i], paths[i], self) }
    
    callback.call(events)
    
    update_last_event
  end
end

#update_last_eventObject



54
55
56
# File 'lib/fsevents/stream.rb', line 54

def update_last_event
  @last_event = Time.now
end