Class: FSEvents::Stream
- Inherits:
-
Object
- Object
- FSEvents::Stream
- Defined in:
- lib/fsevents/stream.rb
Defined Under Namespace
Classes: StreamError
Constant Summary collapse
- MODES =
[:mtime, :cache]
Instance Attribute Summary collapse
-
#allocator ⇒ Object
readonly
Returns the value of attribute allocator.
-
#callback ⇒ Object
readonly
Returns the value of attribute callback.
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#dirs ⇒ Object
readonly
Returns the value of attribute dirs.
-
#flags ⇒ Object
readonly
Returns the value of attribute flags.
-
#last_event ⇒ Object
readonly
Returns the value of attribute last_event.
-
#latency ⇒ Object
readonly
Returns the value of attribute latency.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
-
#paths ⇒ Object
readonly
Returns the value of attribute paths.
-
#since ⇒ Object
readonly
Returns the value of attribute since.
-
#stream ⇒ Object
readonly
Returns the value of attribute stream.
Class Method Summary collapse
Instance Method Summary collapse
- #create ⇒ Object
-
#initialize(*paths, &callback) ⇒ Stream
constructor
A new instance of Stream.
- #invalidate ⇒ Object
- #release ⇒ Object
- #run ⇒ Object
- #schedule ⇒ Object
- #shutdown ⇒ Object
- #start ⇒ Object
- #startup ⇒ Object
- #stop ⇒ Object
- #stream_callback ⇒ Object
- #update_last_event ⇒ Object
Constructor Details
#initialize(*paths, &callback) ⇒ Stream
Returns a new instance of Stream.
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 = {} = paths.pop if paths.last.is_a?(Hash) @mode = [: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 = [:allocator] || OSX::KCFAllocatorDefault @context = [:context] || nil @paths = paths @since = [:since] || OSX::KFSEventStreamEventIdSinceNow @latency = [:latency] || 1.0 @flags = [:flags] || 0 end |
Instance Attribute Details
#allocator ⇒ Object (readonly)
Returns the value of attribute allocator.
6 7 8 |
# File 'lib/fsevents/stream.rb', line 6 def allocator @allocator end |
#callback ⇒ Object (readonly)
Returns the value of attribute callback.
6 7 8 |
# File 'lib/fsevents/stream.rb', line 6 def callback @callback end |
#context ⇒ Object (readonly)
Returns the value of attribute context.
6 7 8 |
# File 'lib/fsevents/stream.rb', line 6 def context @context end |
#dirs ⇒ Object (readonly)
Returns the value of attribute dirs.
5 6 7 |
# File 'lib/fsevents/stream.rb', line 5 def dirs @dirs end |
#flags ⇒ Object (readonly)
Returns the value of attribute flags.
6 7 8 |
# File 'lib/fsevents/stream.rb', line 6 def flags @flags end |
#last_event ⇒ Object (readonly)
Returns the value of attribute last_event.
5 6 7 |
# File 'lib/fsevents/stream.rb', line 5 def last_event @last_event end |
#latency ⇒ Object (readonly)
Returns the value of attribute latency.
6 7 8 |
# File 'lib/fsevents/stream.rb', line 6 def latency @latency end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
5 6 7 |
# File 'lib/fsevents/stream.rb', line 5 def mode @mode end |
#paths ⇒ Object (readonly)
Returns the value of attribute paths.
6 7 8 |
# File 'lib/fsevents/stream.rb', line 6 def paths @paths end |
#since ⇒ Object (readonly)
Returns the value of attribute since.
6 7 8 |
# File 'lib/fsevents/stream.rb', line 6 def since @since end |
#stream ⇒ Object (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
#create ⇒ Object
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 |
#invalidate ⇒ Object
102 103 104 |
# File 'lib/fsevents/stream.rb', line 102 def invalidate OSX.FSEventStreamInvalidate(stream) end |
#release ⇒ Object
106 107 108 109 |
# File 'lib/fsevents/stream.rb', line 106 def release OSX.FSEventStreamRelease(stream) @stream = nil end |
#run ⇒ Object
117 118 119 |
# File 'lib/fsevents/stream.rb', line 117 def run OSX.CFRunLoopRun end |
#schedule ⇒ Object
53 54 55 |
# File 'lib/fsevents/stream.rb', line 53 def schedule OSX.FSEventStreamScheduleWithRunLoop(stream, OSX.CFRunLoopGetCurrent, OSX::KCFRunLoopDefaultMode) end |
#shutdown ⇒ Object
111 112 113 114 115 |
# File 'lib/fsevents/stream.rb', line 111 def shutdown stop invalidate release end |
#start ⇒ Object
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 |
#startup ⇒ Object
79 80 81 82 |
# File 'lib/fsevents/stream.rb', line 79 def startup schedule start end |
#stop ⇒ Object
98 99 100 |
# File 'lib/fsevents/stream.rb', line 98 def stop OSX.FSEventStreamStop(stream) end |
#stream_callback ⇒ Object
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_event ⇒ Object
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 |