[MacRuby-devel] Monitoring file system changes

stephen horne fatste at gmail.com
Thu Oct 18 10:07:51 PDT 2012


I've cobbled together a class that wraps FSEventStreamCreate so that I can monitor a settings.json file and update my application upon its change - à la Sublime Text 2. I refactored the code I found on the subject in Matt Aimonetti's book.

So far, it seems to be doing what I expect, but it would ease my mind a bit if someone who know something of the subject could tell me if I'm setting myself up for problems, as I don't really understand what's happening in the background.

Here's an example of it in use:

framework 'cocoa'

class FolderWatch
  def initialize(paths, callback, object)
    @callback = callback
    @paths = [paths].flatten
    
    callback = Proc.new do |stream, client_callback_info, number_of_events, paths_pointer, event_flags, event_ids|
      paths_pointer.cast!("*") # cast as a string pointer
      stuff = {
        stream:stream,
        client_callback_info:client_callback_info,
        number_of_events:number_of_events,
        paths_pointer:paths_pointer,
        event_flags:event_flags,
        event_ids:event_ids
      }
      object.send(@callback, stuff)
    end
    @stream = FSEventStreamCreate(KCFAllocatorDefault, callback, nil, @paths, KFSEventStreamEventIdSinceNow, 0.0, 0)
    FSEventStreamScheduleWithRunLoop(@stream, CFRunLoopGetCurrent(), KCFRunLoopDefaultMode)
  end
  
  def start
    FSEventStreamStart(@stream)    
  end
  
  def stop
    FSEventStreamStop(@stream)    
  end
end

class Klass
  def callback(hash)
    puts hash[:paths_pointer][0]
  end
end

callback_object = Klass.new

f = FolderWatch.new("/Users/fatboy/Desktop", :callback, callback_object)

f.start



--
Stephen Horne

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-devel/attachments/20121018/95ab9a1c/attachment.html>


More information about the MacRuby-devel mailing list