Module: FSEvent

Defined in:
ext/fs_event/fs_event.c

Class Method Summary collapse

Class Method Details

.fs_event_watch(path, processor) ⇒ Object

Calls the block with the changed paths when something changes in the specified path. This method blocks the thread forever.

FSEvent.watch('/tmp', Proc.new { |files| p files })


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'ext/fs_event/fs_event.c', line 60

static VALUE fs_event_watch(VALUE self, VALUE path, VALUE proc)
{
  processor = proc;

  CFStringRef cfPaths[1];
  cfPaths[0] = CFStringCreateWithCString(
    kCFAllocatorSystemDefault,
    RSTRING_PTR(path),
    kCFStringEncodingUTF8
  );
  CFArrayRef paths = CFArrayCreate(kCFAllocatorSystemDefault, (const void **)cfPaths, 1, NULL);

  FSEventStreamRef streamRef = FSEventStreamCreate(
    NULL,
    &fs_event_callback,
    NULL,
    paths,
    kFSEventStreamEventIdSinceNow,
    (CFTimeInterval)0.5,
    kFSEventStreamCreateFlagWatchRoot | kFSEventStreamCreateFlagFileEvents
  );

#ifdef DEBUG
  FSEventStreamShow(streamRef);
#endif

  FSEventStreamScheduleWithRunLoop(streamRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
  FSEventStreamStart(streamRef);

  CFRunLoopRun();

  FSEventStreamFlushSync(streamRef);
  FSEventStreamStop(streamRef);

  return Qnil;
}