From 16370ea1a1c9fcdc81e80e1459d06a26116857fc Mon Sep 17 00:00:00 2001 From: johnste Date: Tue, 7 Jul 2015 23:42:11 +0200 Subject: [PATCH] Add path watcher and set it up to watch the config file --- Finicky/Finicky.xcodeproj/project.pbxproj | 4 ++ Finicky/Finicky/FNConfigLoader.swift | 12 +++++ Finicky/Finicky/FNPathWatcher.swift | 59 +++++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 Finicky/Finicky/FNPathWatcher.swift diff --git a/Finicky/Finicky.xcodeproj/project.pbxproj b/Finicky/Finicky.xcodeproj/project.pbxproj index cb9b64c..5a1084d 100644 --- a/Finicky/Finicky.xcodeproj/project.pbxproj +++ b/Finicky/Finicky.xcodeproj/project.pbxproj @@ -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 */ @@ -48,6 +49,7 @@ 548F11CD1B2A43F700FDDC3C /* FNAPI.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FNAPI.swift; sourceTree = ""; }; 54B0E7021B4678CE003F8AEE /* FNShortUrlResolver.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FNShortUrlResolver.swift; sourceTree = ""; }; 54E33FD61B24E27000998E13 /* FNConfigLoader.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FNConfigLoader.swift; sourceTree = ""; }; + 54F2EEE31B4C68E300638DAE /* FNPathWatcher.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FNPathWatcher.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -102,6 +104,7 @@ 54E33FD61B24E27000998E13 /* FNConfigLoader.swift */, 548F11CD1B2A43F700FDDC3C /* FNAPI.swift */, 54B0E7021B4678CE003F8AEE /* FNShortUrlResolver.swift */, + 54F2EEE31B4C68E300638DAE /* FNPathWatcher.swift */, ); path = Finicky; sourceTree = ""; @@ -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 */, ); diff --git a/Finicky/Finicky/FNConfigLoader.swift b/Finicky/Finicky/FNConfigLoader.swift index b86fa3e..cd7572f 100644 --- a/Finicky/Finicky/FNConfigLoader.swift +++ b/Finicky/Finicky/FNConfigLoader.swift @@ -14,6 +14,8 @@ var FNConfigPath: String = "~/.finicky.js" class FNConfigLoader { var configPaths: NSMutableSet; + var configWatcher: FNPathWatcher?; + var monitor : FNPathWatcher!; init() { self.configPaths = NSMutableSet() @@ -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? @@ -49,6 +60,7 @@ class FNConfigLoader { self.setupAPI(ctx) ctx.evaluateScript(config!) + setupConfigWatcher() } func setupAPI(ctx: JSContext) { diff --git a/Finicky/Finicky/FNPathWatcher.swift b/Finicky/Finicky/FNPathWatcher.swift new file mode 100644 index 0000000..fc99407 --- /dev/null +++ b/Finicky/Finicky/FNPathWatcher.swift @@ -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) + } +} \ No newline at end of file