{"id":7032,"date":"2014-05-17T00:23:26","date_gmt":"2014-05-17T00:23:26","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/05\/17\/fsevents-in-macruby-wrong-callback-is-executed-collection-of-common-programming-errors\/"},"modified":"2014-05-17T00:23:26","modified_gmt":"2014-05-17T00:23:26","slug":"fsevents-in-macruby-wrong-callback-is-executed-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/05\/17\/fsevents-in-macruby-wrong-callback-is-executed-collection-of-common-programming-errors\/","title":{"rendered":"FSEvents in MacRuby: wrong callback is executed-Collection of common programming errors"},"content":{"rendered":"<p>Yeah, this is really weird. I also have this behavior. It seems like the latest registered callback always gets called. But on the bright side, it is possible, from the fourth argument to the callback, to get the path to the directory that actually got called. I had to use a construct like this.<\/p>\n<p>I&#8217;m not much of a rubyist, but this code works for me.<\/p>\n<pre><code>framework 'Cocoa'\nframework 'CoreServices'\n\n\nclass Monitor\n  @@registry = {}\n  def self.register(dir, other_data)\n    @@registry[dir] = other_data\n  end\n\n  def initialize(dir, other_data)\n      @dir = dir\n\n      self.class.register(dir, other_data)\n      callback = Proc.new do |stream, context, count, paths, flags, ids|\n          paths.cast!('*')\n\n          p \"the callback that triggered has closure variable @dir=#{@dir}\"\n          p \"but the actual callback said the dir was #{paths[0]}\"\n          p \"the metadata that I stored associated with that directory is #{@@registry[paths[0]]}\"\n      end\n\n\n      @stream = FSEventStreamCreate(KCFAllocatorDefault, callback, nil, [@dir], KFSEventStreamEventIdSinceNow, 0.0, 0)\n      FSEventStreamScheduleWithRunLoop(@stream, CFRunLoopGetCurrent(), KCFRunLoopDefaultMode)\n      FSEventStreamStart(@stream)\n  end\nend\n\nMonitor.new(Dir.pwd + \"\/dir1\/\", 'dir1 data')\nMonitor.new(Dir.pwd + \"\/dir2\/\", 'dir2 data')\nMonitor.new(Dir.pwd + \"\/dir3\/\", 'dir3 data')\n\napp = NSApplication.sharedApplication\napp.run\n<\/code><\/pre>\n<p>Here&#8217;s the output I see:<\/p>\n<pre><code>rmcgibbo@Roberts-MacBook-Pro-2 ~\/local\/fsync\n$ macruby fsevents.rb &amp;\n[1] 14638\n\nrmcgibbo@Roberts-MacBook-Pro-2 ~\/local\/fsync\n$ touch dir1\/mao\n\nrmcgibbo@Roberts-MacBook-Pro-2 ~\/local\/fsync\n$ \"the callback that triggered has closure variable @dir=\/Users\/rmcgibbo\/local\/fsync\/dir3\/\"\n\"but the actual callback said the dir was \/Users\/rmcgibbo\/local\/fsync\/dir1\/\"\n\"the metadata that I stored associated with that directory is dir1 data\"\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Yeah, this is really weird. I also have this behavior. It seems like the latest registered callback always gets called. But on the bright side, it is possible, from the fourth argument to the callback, to get the path to the directory that actually got called. I had to use a construct like this. I&#8217;m [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7032","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7032","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7032"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7032\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7032"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7032"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7032"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}