Class: FSEvents::Stream

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

Defined Under Namespace

Classes: StreamError

Constant Summary collapse

MODES =
[:mtime, :cache]

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)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fsevents/stream.rb', line 12

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)
  
  @mode = options[:mode] || :mtime
  raise ArgumentError, "Mode '#{mode}' unknown" unless MODES.include?(@mode)
  @dirs = {}
  
  paths = Dir.pwd if paths.empty?
  paths = [paths].flatten.collect { |path|  path.sub(%r%/$%, '') }
  
  @allocator = options[:allocator] || OSX::KCFAllocatorDefault
  @context   = options[:context]   || nil
  @paths     = paths
  @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

#dirsObject (readonly)

Returns the value of attribute dirs.



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

def dirs
  @dirs
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

#modeObject (readonly)

Returns the value of attribute mode.



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

def mode
  @mode
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



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

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

.watch(*args, &block) ⇒ Object



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

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

Instance Method Details

#createObject

Raises:



34
35
36
37
# File 'lib/fsevents/stream.rb', line 34

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

#invalidateObject



102
103
104
# File 'lib/fsevents/stream.rb', line 102

def invalidate
  OSX.FSEventStreamInvalidate(stream)
end

#releaseObject



106
107
108
109
# File 'lib/fsevents/stream.rb', line 106

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

#runObject



117
118
119
# File 'lib/fsevents/stream.rb', line 117

def run
  OSX.CFRunLoopRun
end

#scheduleObject



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

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

#shutdownObject



111
112
113
114
115
# File 'lib/fsevents/stream.rb', line 111

def shutdown
  stop
  invalidate
  release
end

#startObject



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

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

#startupObject



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

def startup
  schedule
  start
end

#stopObject



98
99
100
# File 'lib/fsevents/stream.rb', line 98

def stop
  OSX.FSEventStreamStop(stream)
end

#stream_callbackObject



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/fsevents/stream.rb', line 39

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

#update_last_eventObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/fsevents/stream.rb', line 62

def update_last_event
  case mode
  when :mtime
    @last_event = Time.now
  when :cache
    cache_paths = paths.dup
    cache_paths.each do |path|
      dirs[path] = {}
      Dir["#{path}/*"].each do |file|
        stat = File::Stat.new(file)
        dirs[path][file] = stat
        cache_paths.push(file) if stat.directory?
      end
    end
  end
end