Skip to content

Commit

Permalink
Merge pull request #9 from johnste/watch-config-file-changes
Browse files Browse the repository at this point in the history
Add path watcher and set it up to watch the config file
  • Loading branch information
johnste committed Jul 7, 2015
2 parents 8ab91cd + 16370ea commit d3cc8c7
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Finicky/Finicky.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
548F11CE1B2A43F700FDDC3C /* FNAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = 548F11CD1B2A43F700FDDC3C /* FNAPI.swift */; };
54B0E7031B4678CE003F8AEE /* FNShortUrlResolver.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54B0E7021B4678CE003F8AEE /* FNShortUrlResolver.swift */; };
54E33FD71B24E27000998E13 /* FNConfigLoader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54E33FD61B24E27000998E13 /* FNConfigLoader.swift */; };
54F2EEE41B4C68E300638DAE /* FNPathWatcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54F2EEE31B4C68E300638DAE /* FNPathWatcher.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -48,6 +49,7 @@
548F11CD1B2A43F700FDDC3C /* FNAPI.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FNAPI.swift; sourceTree = "<group>"; };
54B0E7021B4678CE003F8AEE /* FNShortUrlResolver.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FNShortUrlResolver.swift; sourceTree = "<group>"; };
54E33FD61B24E27000998E13 /* FNConfigLoader.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FNConfigLoader.swift; sourceTree = "<group>"; };
54F2EEE31B4C68E300638DAE /* FNPathWatcher.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FNPathWatcher.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -102,6 +104,7 @@
54E33FD61B24E27000998E13 /* FNConfigLoader.swift */,
548F11CD1B2A43F700FDDC3C /* FNAPI.swift */,
54B0E7021B4678CE003F8AEE /* FNShortUrlResolver.swift */,
54F2EEE31B4C68E300638DAE /* FNPathWatcher.swift */,
);
path = Finicky;
sourceTree = "<group>";
Expand Down Expand Up @@ -244,6 +247,7 @@
files = (
54E33FD71B24E27000998E13 /* FNConfigLoader.swift in Sources */,
54899CD61B20D5BC00647101 /* AppDelegate.swift in Sources */,
54F2EEE41B4C68E300638DAE /* FNPathWatcher.swift in Sources */,
54B0E7031B4678CE003F8AEE /* FNShortUrlResolver.swift in Sources */,
548F11CE1B2A43F700FDDC3C /* FNAPI.swift in Sources */,
);
Expand Down
12 changes: 12 additions & 0 deletions Finicky/Finicky/FNConfigLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ var FNConfigPath: String = "~/.finicky.js"
class FNConfigLoader {

var configPaths: NSMutableSet;
var configWatcher: FNPathWatcher?;
var monitor : FNPathWatcher!;

init() {
self.configPaths = NSMutableSet()
Expand All @@ -25,6 +27,15 @@ class FNConfigLoader {
configPaths.addObject(FNConfigPath)
}

func setupConfigWatcher() {

let url = NSURL(fileURLWithPath: FNConfigPath.stringByExpandingTildeInPath)!
monitor = FNPathWatcher(url: url, handler: {
self.reload()
})
monitor.start()
}

func reload() {
self.resetConfigPaths()
var error:NSError?
Expand All @@ -49,6 +60,7 @@ class FNConfigLoader {

self.setupAPI(ctx)
ctx.evaluateScript(config!)
setupConfigWatcher()
}

func setupAPI(ctx: JSContext) {
Expand Down
59 changes: 59 additions & 0 deletions Finicky/Finicky/FNPathWatcher.swift
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)
}
}

0 comments on commit d3cc8c7

Please sign in to comment.