Skip to content

Instantly share code, notes, and snippets.

@JRHeaton
Created May 16, 2014 21:32
Show Gist options
  • Save JRHeaton/940529db23bbb8ff58bc to your computer and use it in GitHub Desktop.
Save JRHeaton/940529db23bbb8ff58bc to your computer and use it in GitHub Desktop.
//
// GSAppDelegate.m
// Fart
//
// Created by John Heaton on 5/16/14.
// Copyright (c) 2014 John Heaton. All rights reserved.
//
#import "GSAppDelegate.h"
#import <AFNetworking/AFNetworking.h>
#import <ReactiveCocoa/ReactiveCocoa.h>
#import <CoreServices/CoreServices.h>
static void FSCB(ConstFSEventStreamRef streamRef,
void *clientCallBackInfo,
size_t numEvents,
void *eventPaths,
const FSEventStreamEventFlags eventFlags[],
const FSEventStreamEventId eventIds[]) {
// get the subscriber reference back
id<RACSubscriber> s = (__bridge id<RACSubscriber>)(clientCallBackInfo);
// get first path
NSString *path = @(*(char **)eventPaths);
// send tuple with path + flags
[s sendNext:[RACTuple tupleWithObjects:path, @(*eventFlags), nil]];
}
@implementation GSAppDelegate
- (RACSignal *)watchPath:(NSString *)path {
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
// create context with ref to this signal's subscriber
FSEventStreamContext c;
bzero(&c, sizeof(c));
c.info = (__bridge void *)(subscriber);
// create event stream
FSEventStreamRef s =
FSEventStreamCreate(NULL, FSCB, &c, (__bridge CFArrayRef)(@[ path ]), kFSEventStreamEventIdSinceNow, 0, kFSEventStreamCreateFlagFileEvents);
// add to the current run loop and listen
FSEventStreamScheduleWithRunLoop(s, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
if(!FSEventStreamStart(s)) {
// if it fails, remove from run loop and error
FSEventStreamUnscheduleFromRunLoop(s, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
[subscriber sendError:[NSError errorWithDomain:NSCocoaErrorDomain code:1 userInfo:@{ NSLocalizedFailureReasonErrorKey : @"Failed to start event stream" }]];
}
return [RACDisposable disposableWithBlock:^{
// clean up
FSEventStreamInvalidate(s);
FSEventStreamStop(s);
FSEventStreamRelease(s);
}];
}];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// subscribe to signal
[[self watchPath:@"/tmp"]
subscribeNext:^(RACTuple *update) {
NSLog(@"path: \'%@\', flags: 0x%x", update.first, [update.second unsignedIntValue]);
}
error:^(NSError *error) {
NSLog(@"fail: %@", error);
}];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment