Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 80 lines (52 sloc) 2.036 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
//
// JSTFileWatcher.m
// jstalk
//
// Created by August Mueller on 3/15/09.
// Copyright 2009 Flying Meat Inc. All rights reserved.
//

#import "JSTFileWatcher.h"

@interface JSTFileWatcher (Private)
- (void)setupEventStreamRef;
@end



@implementation JSTFileWatcher
@synthesize delegate=_delegate;
@synthesize path=_path;


+ (id) fileWatcherWithPath:(NSString*)filePath delegate:(id)delegate {
    
    JSTFileWatcher *fw = [[[self alloc] init] autorelease];
    
    fw.path = filePath;
    fw.delegate = delegate;
    
    [fw setupEventStreamRef];
    
    return fw;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    if (streamRef) {
        FSEventStreamStop(streamRef);
        FSEventStreamInvalidate(streamRef);
        FSEventStreamRelease(streamRef);
    }
    
    
    [_path release];
    [super dealloc];
}


static void fsevents_callback(FSEventStreamRef streamRef, JSTFileWatcher *fw, int numEvents, const char *const eventPaths[], const FSEventStreamEventFlags *eventMasks, const uint64_t *eventIDs)
{
    id delegate = [fw delegate];
    
    if (delegate && [delegate respondsToSelector:@selector(fileWatcherDidRecieveFSEvent:)]) {
        [delegate fileWatcherDidRecieveFSEvent:fw];
    }
    
}

- (void)setupEventStreamRef {
    
    FSEventStreamContext context = {0, (void *)self, NULL, NULL, NULL};
    NSArray *pathsToWatch = [NSArray arrayWithObject:[_path stringByDeletingLastPathComponent]];
    
    streamRef = FSEventStreamCreate(kCFAllocatorDefault,
                                    (FSEventStreamCallback)&fsevents_callback,
                                    &context,
                                    (CFArrayRef)pathsToWatch,
                                    kFSEventStreamEventIdSinceNow,
                                    2,
                                    0);
    
    FSEventStreamScheduleWithRunLoop(streamRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
    
    FSEventStreamStart(streamRef);
    
}

@end

Something went wrong with that request. Please try again.