Skip to content
Newer
Older
100644 80 lines (52 sloc) 1.99 KB
bdf823d @ccgus [NEW] Added support for "External Editors". It's small, but it's some…
authored Mar 15, 2009
1 //
2 // JSTFileWatcher.m
3 // jstalk
4 //
5 // Created by August Mueller on 3/15/09.
6 // Copyright 2009 Flying Meat Inc. All rights reserved.
7 //
8
9 #import "JSTFileWatcher.h"
10
11 @interface JSTFileWatcher (Private)
12 - (void)setupEventStreamRef;
13 @end
14
15
16
17 @implementation JSTFileWatcher
18 @synthesize delegate=_delegate;
19 @synthesize path=_path;
20
21
22 + (id) fileWatcherWithPath:(NSString*)filePath delegate:(id)delegate {
23
24 JSTFileWatcher *fw = [[[self alloc] init] autorelease];
25
26 fw.path = filePath;
27 fw.delegate = delegate;
28
29 [fw setupEventStreamRef];
30
31 return fw;
32 }
33
34 - (void)dealloc {
35 [[NSNotificationCenter defaultCenter] removeObserver:self];
36
37 if (streamRef) {
38 FSEventStreamStop(streamRef);
39 FSEventStreamInvalidate(streamRef);
40 FSEventStreamRelease(streamRef);
41 }
42
43
44 [_path release];
45 [super dealloc];
46 }
47
48
49 static void fsevents_callback(FSEventStreamRef streamRef, JSTFileWatcher *fw, int numEvents, const char *const eventPaths[], const FSEventStreamEventFlags *eventMasks, const uint64_t *eventIDs)
50 {
51 id delegate = [fw delegate];
52
53 if (delegate && [delegate respondsToSelector:@selector(fileWatcherDidRecieveFSEvent:)]) {
54 [delegate fileWatcherDidRecieveFSEvent:fw];
55 }
56
57 }
58
59 - (void)setupEventStreamRef {
60
61 FSEventStreamContext context = {0, (void *)self, NULL, NULL, NULL};
62 NSArray *pathsToWatch = [NSArray arrayWithObject:[_path stringByDeletingLastPathComponent]];
63
64 streamRef = FSEventStreamCreate(kCFAllocatorDefault,
65 (FSEventStreamCallback)&fsevents_callback,
66 &context,
67 (CFArrayRef)pathsToWatch,
68 kFSEventStreamEventIdSinceNow,
69 2,
70 0);
71
72 FSEventStreamScheduleWithRunLoop(streamRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
73
74 FSEventStreamStart(streamRef);
75
76 }
77
78 @end
79
Something went wrong with that request. Please try again.