Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some small updates #31

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions CDEvents.h
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,44 @@ ignoreEventsFromSubDirs:(BOOL)ignoreEventsFromSubDirs
excludeURLs:(NSArray *)exludeURLs
streamCreationFlags:(CDEventsEventStreamCreationFlags)streamCreationFlags;

/**
* Returns an <code>CDEvents</code> object initialized with the given URLs to watch, URLs to exclude, whether events from sub-directories are ignored or not and schedules the watcher on the given run loop.
*
* @param URLs An array of URLs (<code>NSURL</code>) we want to watch.
* @param block The block which the CDEvents object executes when it recieves an event.
* @param queue Dispatch queue which the which the watcher should be schedueled on.
* @param sinceEventIdentifier Events that have happened after the given event identifier will be supplied.
* @param notificationLatency The (approximate) time intervall between notifications sent to the delegate.
* @param ignoreEventsFromSubDirs Wheter events from sub-directories of the watched URLs should be ignored or not.
* @param exludeURLs An array of URLs that we should ignore events from. Pass <code>nil</code> if none should be excluded.
* @param streamCreationFlags The event stream creation flags.
* @return An CDEvents object initialized with the given URLs to watch, URLs to exclude, whether events from sub-directories are ignored or not and run on the given run loop.
* @throws NSInvalidArgumentException if the parameter URLs is empty or points to <code>nil</code>.
* @throws NSInvalidArgumentException if <em>delegate</em>is <code>nil</code>.
* @throws CDEventsEventStreamCreationFailureException if we failed to create a event stream.
*
* @see initWithURLs:delegate:
* @see initWithURLs:delegate:onRunLoop:
* @see ignoreEventsFromSubDirectories
* @see excludedURLs
* @see CDEventsEventBlock
* @see FSEventStreamCreateFlags
*
* @discussion To ask for events "since now" pass the return value of
* currentEventIdentifier as the parameter <code>sinceEventIdentifier</code>.
* CDEventStreamCreationFailureException should be extremely rare.
*
* @since head
*/
- (id)initWithURLs:(NSArray *)URLs
block:(CDEventsEventBlock)block
queue:(dispatch_queue_t)queue
sinceEventIdentifier:(CDEventIdentifier)sinceEventIdentifier
notificationLantency:(CFTimeInterval)notificationLatency
ignoreEventsFromSubDirs:(BOOL)ignoreEventsFromSubDirs
excludeURLs:(NSArray *)exludeURLs
streamCreationFlags:(CDEventsEventStreamCreationFlags)streamCreationFlags;

#pragma mark Flush methods
/** @name Flushing Events */
/**
Expand Down
97 changes: 78 additions & 19 deletions CDEvents.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ @interface CDEvents () {

FSEventStreamRef _eventStream;
CDEventsEventStreamCreationFlags _eventStreamCreationFlags;

dispatch_queue_t _queue;
}

// Redefine the properties that should be writeable.
Expand Down Expand Up @@ -238,19 +240,72 @@ - (id)initWithURLs:(NSArray *)URLs
return self;
}

- (id)initWithURLs:(NSArray *)URLs
block:(CDEventsEventBlock)block
queue:(dispatch_queue_t)queue
sinceEventIdentifier:(CDEventIdentifier)sinceEventIdentifier
notificationLantency:(CFTimeInterval)notificationLatency
ignoreEventsFromSubDirs:(BOOL)ignoreEventsFromSubDirs
excludeURLs:(NSArray *)exludeURLs
streamCreationFlags:(CDEventsEventStreamCreationFlags)streamCreationFlags
{
if (block == NULL || URLs == nil || [URLs count] == 0) {
[NSException raise:NSInvalidArgumentException
format:@"Invalid arguments passed to CDEvents init-method."];
}

if ((self = [super init])) {
_watchedURLs = [URLs copy];
_excludedURLs = [exludeURLs copy];
_eventBlock = block;

_sinceEventIdentifier = sinceEventIdentifier;
_eventStreamCreationFlags = streamCreationFlags;

_notificationLatency = notificationLatency;
_ignoreEventsFromSubDirectories = ignoreEventsFromSubDirs;

_lastEvent = nil;

_queue = queue;

[self createEventStream];

FSEventStreamSetDispatchQueue(_eventStream, _queue);
if (!FSEventStreamStart(_eventStream)) {
[NSException raise:CDEventsEventStreamCreationFailureException
format:@"Failed to create event stream."];
}
}

return self;
}


#pragma mark NSCopying method
- (id)copyWithZone:(NSZone *)zone
{
CDEvents *copy = [[CDEvents alloc] initWithURLs:[self watchedURLs]
block:[self eventBlock]
onRunLoop:[NSRunLoop currentRunLoop]
sinceEventIdentifier:[self sinceEventIdentifier]
notificationLantency:[self notificationLatency]
ignoreEventsFromSubDirs:[self ignoreEventsFromSubDirectories]
excludeURLs:[self excludedURLs]
streamCreationFlags:_eventStreamCreationFlags];

CDEvents *copy;
if (!_queue) {
copy = [[CDEvents alloc] initWithURLs:[self watchedURLs]
block:[self eventBlock]
onRunLoop:[NSRunLoop currentRunLoop]
sinceEventIdentifier:[self sinceEventIdentifier]
notificationLantency:[self notificationLatency]
ignoreEventsFromSubDirs:[self ignoreEventsFromSubDirectories]
excludeURLs:[self excludedURLs]
streamCreationFlags:_eventStreamCreationFlags];
}
else {
copy = [[CDEvents alloc] initWithURLs:[self watchedURLs]
block:[self eventBlock]
queue:_queue
sinceEventIdentifier:[self sinceEventIdentifier]
notificationLantency:[self notificationLatency]
ignoreEventsFromSubDirs:[self ignoreEventsFromSubDirectories]
excludeURLs:[self excludedURLs]
streamCreationFlags:_eventStreamCreationFlags];
}
return copy;
}

Expand Down Expand Up @@ -351,16 +406,20 @@ static void CDEventsCallback(
// contain any trailing slash.
NSURL *eventURL = [NSURL fileURLWithPath:[[eventPathsArray objectAtIndex:i] stringByStandardizingPath]];
NSString *eventPath = [eventURL path];

// Ignore all events except for the URLs we are explicitly watching.
if ([watcher ignoreEventsFromSubDirectories]) {
shouldIgnore = YES;
for (NSURL *url in watchedURLs) {
if ([[url path] isEqualToString:eventPath]) {
shouldIgnore = NO;
break;
}
}
NSString *eventDirectoryPath = [eventPath stringByDeletingLastPathComponent];

// Check if is directory
BOOL isDirectory = CFURLHasDirectoryPath((__bridge CFURLRef)eventURL);

// Ignore all events except for the URLs we are explicitly watching.
if ([watcher ignoreEventsFromSubDirectories]) {
shouldIgnore = YES;
for (NSURL *url in watchedURLs) {
if ([[url path] isEqualToString:eventDirectoryPath] && !isDirectory) {
shouldIgnore = NO;
break;
}
}
// Ignore all explicitly excludeded URLs (not required to check if we
// ignore all events from sub-directories).
} else if (excludedURLs != nil) {
Expand Down
4 changes: 2 additions & 2 deletions CDEvents.podspec
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Pod::Spec.new do |s|
s.name = "CDEvents"
s.version = "1.2.2"
s.version = "1.2.3"
s.summary = "An Objective-C wrapper for Mac OS X’s FSEvents C API."
s.homepage = "http://rastersize.github.com/CDEvents"
s.license = "MIT"
s.author = { "Aron Cedercrantz" => "[email protected]" }
s.source = { :git => "https://github.com/rastersize/CDEvents.git", :tag => "1.2.2" }
s.source = { :git => "https://github.com/rastersize/CDEvents.git", :tag => "1.2.3" }
s.platform = :osx, "10.6"
s.source_files = "*.{h,m}"
s.framework = "CoreServices"
Expand Down
Empty file added OLD
Empty file.