-
-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from johnste/watch-config-file-changes
Add path watcher and set it up to watch the config file
- Loading branch information
Showing
3 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// | ||
// FNPathWatcher.swift | ||
// Finicky | ||
// | ||
// Created by John Sterling on 07/07/15. | ||
// Copyright (c) 2015 John sterling. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
public class FNPathWatcher { | ||
|
||
enum State { | ||
case On, Off | ||
} | ||
|
||
private let source: dispatch_source_t | ||
private let descriptor: CInt | ||
private var state: State = .Off | ||
|
||
/// Creates a folder monitor object with monitoring enabled. | ||
public init(url: NSURL, handler: ()->Void) { | ||
|
||
state = .Off | ||
descriptor = open(url.fileSystemRepresentation, O_EVTONLY) | ||
let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) | ||
source = dispatch_source_create( | ||
DISPATCH_SOURCE_TYPE_VNODE, | ||
UInt(descriptor), | ||
DISPATCH_VNODE_DELETE | DISPATCH_VNODE_WRITE | DISPATCH_VNODE_EXTEND | DISPATCH_VNODE_ATTRIB | DISPATCH_VNODE_LINK | DISPATCH_VNODE_RENAME | DISPATCH_VNODE_REVOKE, | ||
queue | ||
) | ||
|
||
dispatch_source_set_event_handler(source, handler) | ||
//dispatch_source_set_cancel_handler({}) | ||
start() | ||
} | ||
|
||
/// Starts sending notifications if currently stopped | ||
public func start() { | ||
if state == .Off { | ||
state = .On | ||
dispatch_resume(source) | ||
} | ||
} | ||
|
||
/// Stops sending notifications if currently enabled | ||
public func stop() { | ||
if state == .On { | ||
state = .Off | ||
dispatch_suspend(source) | ||
} | ||
} | ||
|
||
deinit { | ||
close(descriptor) | ||
dispatch_source_cancel(source) | ||
} | ||
} |